-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.js
executable file
·64 lines (56 loc) · 1.45 KB
/
bin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env node
// @ts-check
const fs = require("fs");
const os = require("os");
const path = require("path");
const rimraf = require("rimraf");
const minimist = require("minimist");
const { calcPackageJson } = require("./lib");
/**
* 清空计算缓存
*/
function cleanCache() {
const cacheDir = path.resolve(os.tmpdir(), "calc-pkg-size");
rimraf(cacheDir, (err) => {
if (err) {
console.error(err);
} else {
console.log("clean cache success");
}
});
}
/**
* 计算包大小
* @param {Record<string, any>} packageJson
* @param {string} [command]
* @param {string} [output]
*/
async function calcSize(
packageJson,
command = 'yarn --prod --prefer-offline', // or "npm install --omit=dev --no-audit --no-fund"
output
) {
const result = await calcPackageJson({
packageJson,
command,
});
if (output) {
fs.writeFileSync(output, JSON.stringify(result, null, 2));
} else {
console.log(result);
}
}
async function main() {
const argv = minimist(process.argv.slice(2));
if (argv._ && argv._[0] === "clean") {
cleanCache();
} else {
const pkgJsonPath = argv.pkg || argv.p || "./package.json";
const command = argv.command || argv.c;
const outputPath = argv.output || argv.o;
const packageJson = require(path.resolve(process.cwd(), pkgJsonPath));
const output = outputPath && path.resolve(process.cwd(), outputPath);
await calcSize(packageJson, command, output);
}
}
main();