-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
118 lines (101 loc) · 3.43 KB
/
cli.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env node
const meow = require("meow");
const checkPackageLatestTag = require("./index");
const {
getNpmConfig,
getAllPackagesByName,
getPackagesAndDependenciesByName,
noop
} = require("./utils");
const cli = meow(
`
Ensures the youngest (newest) monarch (version) within kingdoms (packages)
holds the royal office (is taggeed as latest).
Usage
$ crown [<kingdom>...] [options]
Options
--verbose Indulge in verbosity.
--dry-run Run without updating.
--no-info Hide info output.
--no-warn Hide warnings.
--no-error Hide errors.
--help Display this text.
--version Display the version.
Configuration
crown adheres to the the .npmrc file within your home directory.
Terminology
Imagine the npm package registry as a world of aristocracies.
Each package is regarded as a kingdom, and its dependencies are its
neighboring kingdoms. Each kingdom (if it's a proper aristocracy) has one
or more monarchs (the versions of the package). The reigning monarch (the
version tagged with latest), is known as the Queen. Each kingdom (package)
must have exactly one queen (latest version) at any given point. Opposed to
the usual aristocracy systems, the youngest (newest) monarch (version)
should always rule (be tagged as latest).
Why do I need this?
Not all queens gives the throne away when a younger monarch comes along.
Hence, a system to regularly inspect the queen of a kingdom and abdicates
her if she's not the youngest monarch of the kingdom is useful. This
library does just that. It inspect one or more kingdoms and their
neighboring kingsoms (recursivly). If a kingdom has a monarch younger than
the queen, the queen is abdicated and the younger monarch is crowned. If
for some reason the kingdom does not have a queen (no latest tag), the
youngest monarch is immidiety crowned to recover from the aristocratic
collapse. Also, if the queen is deceased (the version tagged as latest does
not exist), the youngest monarch is also immidietly crowned.
`,
{
flags: {
verbose: {
type: "boolean",
default: false
},
info: {
type: "boolean",
default: true
},
warn: {
type: "boolean",
default: true
},
error: {
type: "boolean",
default: true
},
"dry-run": {
type: "boolean",
default: false
}
},
description: false
}
);
async function main(input, { dryRun, verbose, info, warn, error }) {
const npmConfig = getNpmConfig();
const logger = {
verbose: verbose ? console.log : noop,
info: info ? console.info : noop,
warn: warn ? console.warn : noop,
error: error ? console.error : noop
};
logger.info("Resolving kingdoms...");
try {
const packages =
input.length === 0
? await getAllPackagesByName(npmConfig)
: await getPackagesAndDependenciesByName(input, npmConfig);
logger.info("Inspecting reigning monarchs...");
for (pkg of packages) {
await checkPackageLatestTag(pkg, npmConfig, { logger, dryRun });
}
logger.info("Done.");
} catch (e) {
console.error(e.message);
if (e.message.includes("401")) {
console.log("There is likely something wrong with your credentials.");
console.log("Use `npm adduser` to add credentials.");
}
process.exit(1);
}
}
main(cli.input, cli.flags);