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

71 lines
2.1 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) {
2021-02-07 16:33:31 +08:00
const buildMap = mapFile.replace(/bundle\//, 'build/')
if (fs.existsSync(buildMap)) {
const mergedMap = createMergedSourceMapFromFiles([
buildMap,
mapFile,
], true);
await fs.promises.writeFile(mapFile, mergedMap);
}
2021-02-05 18:20:42 +08:00
}