forked from beshanoe/update-by-scope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
83 lines (69 loc) · 1.94 KB
/
index.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const Path = require("path");
const fs = require("fs");
const ChildProcess = require("child_process");
const args = require("args");
const hasYarn = require("has-yarn");
const [defaultNpmClient, defaultNpmCommand] = hasYarn()
? ["yarn", "upgrade"]
: ["npm", "install"];
args
.option(["n", "npmClient"], "NPM client", defaultNpmClient)
.option(
["c", "npmClientCommand"],
"NPM client command to execute",
defaultNpmCommand
)
.option(["t", "tag"], "NPM tag", "latest")
.option("v", "Show version")
.option("h", "Show help");
const flags = args.parse(process.argv, {
value: `<scope> [npmClient=${defaultNpmClient}] [npmClientCommand=${defaultNpmCommand}]`,
help: false,
version: false
});
if (flags.h) {
args.showHelp();
}
if (flags.v) {
console.log(require('../package.json').version)
process.exit(0)
}
const [
scope,
legacyNpmClient = defaultNpmClient,
legacyClientCommand = defaultNpmCommand
] = args.sub;
const npmClient = legacyNpmClient || flags.npmClient;
const clientCommand = legacyClientCommand || flags.npmClientCommand;
if (!scope) {
args.showHelp();
}
if (scope[0] !== "@") {
console.error(`Scope should start with "@"`);
return;
}
const { dependencies = {}, devDependencies = {} } = JSON.parse(
fs.readFileSync(Path.join(process.cwd(), "./package.json"))
);
const packageNames = Array.from(
new Set(
[...Object.keys(dependencies), ...Object.keys(devDependencies)].filter(_ =>
_.startsWith(scope)
)
)
).sort();
if (!packageNames.length) {
console.log(`Found 0 packages with scope "${scope}"`);
return;
}
const packageNamesWithVersion = packageNames.map(_ => `${_}@${flags.tag}`);
console.log(`Found ${packageNames.length} with scope "${scope}":`);
console.log(packageNames);
console.log(
`Executing "${npmClient} ${clientCommand} ${packageNamesWithVersion.join(
" "
)}"`
);
ChildProcess.spawnSync(npmClient, [clientCommand, ...packageNamesWithVersion], {
stdio: "inherit"
});