This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-cli/src/actions.ts

93 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-02-05 16:35:52 +08:00
import { Shell } from "./shell";
import { createMergedSourceMapFromFiles } from "source-map-merger"
2021-02-23 10:55:00 +08:00
import fs from "fs";
2021-02-05 16:35:52 +08:00
import { glob } from "./util";
2021-02-23 10:55:00 +08:00
import path from "path";
2021-02-05 16:35:52 +08:00
export async function build() {
let ret = await Shell.exec("node", ["node_modules/.bin/tsc", "-p", "."], {
env: process.env,
consoleHandler: (info) => {
console.log(info);
}
});
if (ret !== 0) {
console.log("Compile error".red);
return;
}
ret = await Shell.exec("node", ["node_modules/.bin/rollup", "-c",], {
env: process.env,
consoleHandler: (info) => {
console.log(info);
}
});
if (ret !== 0) {
console.log("Compile error".red);
return;
}
2021-02-05 16:35:52 +08:00
const bundleFiles = await glob("bundle/**/*.js");
for (let bundleFile of bundleFiles) {
await doMerge(bundleFile);
}
2021-02-23 10:55:00 +08:00
if (fs.existsSync("assets")) {
const assets = await fs.promises.readdir("assets")
for (let asset of assets) {
const assetFile = path.resolve("assets", asset);
const stat = await fs.promises.stat(assetFile);
await Shell.exec("cp", ["-rf", assetFile, "bundle"]);
if (stat.isDirectory()) {
console.log(`Asset -> ${asset.yellow}`);
} else {
console.log(`Asset -> ${asset.green}`);
}
}
}
2021-02-05 16:35:52 +08:00
}
export async function clean() {
await Shell.exec("rm", ["-rf", "build"]);
await Shell.exec("rm", ["-rf", "bundle"]);
}
2021-02-05 18:20:42 +08:00
async function doMerge(jsFile: string) {
const mapFile = `${jsFile}.map`;
2021-02-05 16:35:52 +08:00
console.log(`Bundle -> ${jsFile.green}`);
2021-02-05 18:20:42 +08:00
if (!fs.existsSync(mapFile)) {
2021-02-05 16:35:52 +08:00
return;
}
2021-02-05 18:20:42 +08:00
console.log(` -> ${mapFile.green}`);
await mergeMap(mapFile);
}
export async function mergeMap(mapFile: string) {
const mapContent = await fs.promises.readFile(mapFile, "utf-8");
2021-03-03 14:39:32 +08:00
try {
if (JSON.parse(mapContent).merged) {
console.log("Already merged");
return;
}
} catch (err) {
console.error(err);
}
const lockFile = `${mapFile}.lock`;
if (fs.existsSync(lockFile)) {
console.log("In mergeMap,skip")
return;
}
await fs.promises.writeFile(lockFile, (new Date).toString(), "utf-8")
try {
const buildMap = mapFile.replace(/bundle\//, 'build/')
if (fs.existsSync(buildMap)) {
const mergedMap = createMergedSourceMapFromFiles([
buildMap,
mapFile,
], true) as string;
const mapObj = JSON.parse(mergedMap);
mapObj.merged = true
await fs.promises.writeFile(mapFile, JSON.stringify(mapObj), "utf-8");
}
} finally {
await fs.promises.unlink(lockFile)
2021-02-07 16:33:31 +08:00
}
2021-02-05 18:20:42 +08:00
}