-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathnpm.js
executable file
·63 lines (54 loc) · 1.73 KB
/
npm.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
const fs = require("fs");
const { spawn } = require("child_process");
const { platformize, ANSIInvert } = require("./utils.js");
const { logDetail, logSuccess } = require("./log.js");
function executeProcess(executable, args, env = process.env) {
const cmd = platformize(executable);
const spawnedProcess = spawn(cmd, args, { env: env });
spawnedProcess.stdout.on("data", (data) => {
console.log(data.toString());
});
spawnedProcess.stderr.on("data", (data) => {
console.error(data.toString());
});
return new Promise((resolve, reject) => {
spawnedProcess.on("exit", (code) => {
if (code === 0) resolve();
else reject(`Process exited with ${code}`);
});
});
}
const npm = async (...args) => {
const start = new Date();
const result = await executeProcess(
"npm",
["--no-update-notifier", "--no-fund", ...args],
{
...process.env,
FORCE_COLOR: 1,
}
);
const elapsed = new Date() - start;
const realCommand = args[args.length - 1];
logDetail(` ${ANSIInvert("npm " + realCommand)} finished in ${elapsed} ms`);
};
const npx = async (...args) => {
await executeProcess("npx", ...args);
};
async function npmInstall(name, offline = false) {
let args = ["--no-audit"];
if (offline) {
args.push("--prefer-offline");
}
args.push("ci");
if (fs.existsSync("package-lock.json")) {
logDetail(`${name}: Installing dependencies ...`);
await npm(...args);
}
}
async function runCommand(commandName, emojis, callback) {
logDetail(`${emojis} ...`);
await callback();
logSuccess(commandName);
}
module.exports = { npm, npx, npmInstall, runCommand };