cli: doric build and doric clean

This commit is contained in:
pengfeizhou 2021-02-05 16:35:52 +08:00 committed by osborn
parent e9035858aa
commit a9cfd8b41a
6 changed files with 66 additions and 3 deletions

View File

@ -24,11 +24,13 @@
},
"homepage": "https://github.com/penfeizhou/doric#readme",
"dependencies": {
"@types/glob": "^7.1.3",
"@types/node": "^14.14.25",
"child_process": "^1.0.2",
"chokidar": "^3.4.1",
"colors": "^1.4.0",
"commander": "^6.0.0",
"glob": "^7.1.6",
"keypress": "^0.2.1",
"nodejs-websocket": "^1.7.2",
"qrcode-terminal": "^0.12.0",

View File

@ -30,7 +30,7 @@ function doMerge(startPath, fileName) {
if (filePath.indexOf('.es5.') >= 0){
return
}
console.log('File change:', filePath)
console.log('File changed:', filePath)
const mergedMap = SourceMapMerger.createMergedSourceMapFromFiles([
filePath.replace(/bundle\//, 'build/'),
filePath,
@ -38,6 +38,7 @@ function doMerge(startPath, fileName) {
fs.writeFileSync(filePath, mergedMap)
return mergedMap
}
function mergeMappings() {
fromDir("bundle", ".map")
}

32
doric-cli/src/actions.ts Normal file
View File

@ -0,0 +1,32 @@
import { Shell } from "./shell";
import { createMergedSourceMapFromFiles } from "source-map-merger"
import fs from "fs"
import { glob } from "./util";
export async function build() {
await Shell.exec("node", ["node_modules/.bin/tsc", "-p", "."]);
await Shell.exec("node", ["node_modules/.bin/rollup", "-c"]);
const bundleFiles = await glob("bundle/**/*.js");
for (let bundleFile of bundleFiles) {
await doMerge(bundleFile);
}
}
export async function clean() {
await Shell.exec("rm", ["-rf", "build"]);
await Shell.exec("rm", ["-rf", "bundle"]);
}
export async function doMerge(jsFile: string) {
const mappingFile = `${jsFile}.map`;
console.log(`Bundle -> ${jsFile.green}`);
if (!fs.existsSync(mappingFile)) {
return;
}
console.log(` -> ${mappingFile.green}`);
const mergedMap = createMergedSourceMapFromFiles([
mappingFile.replace(/bundle\//, 'build/'),
mappingFile,
], true);
await fs.promises.writeFile(mappingFile, mergedMap);
}

View File

@ -1,6 +1,7 @@
#!/usr/bin/env node
import commander from "commander"
import { build, clean } from "./actions";
import create from "./create"
commander
@ -19,10 +20,12 @@ commander
})
commander
.command('build')
.action(function () {
.action(async function () {
await build();
})
commander
.command('clean')
.action(function () {
.action(async function () {
await clean();
})
commander.parse(process.argv)

1
doric-cli/src/modules.d.ts vendored Normal file
View File

@ -0,0 +1 @@
declare module 'source-map-merger'

View File

@ -1,3 +1,27 @@
import globLib, { IOptions } from "glob";
export function getAssetsDir() {
return `${__dirname}/../assets`;
}
export async function glob(pattern: string, options?: IOptions) {
return new Promise((resolve, reject) => {
if (options) {
globLib(pattern, options, (err, ret) => {
if (err) {
reject(err);
} else {
resolve(ret);
}
});
} else {
globLib(pattern, (err, ret) => {
if (err) {
reject(err);
} else {
resolve(ret);
}
});
}
}) as Promise<string[]>;
}