-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
cli.js
executable file
·180 lines (153 loc) · 6.09 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* eslint-disable no-console, unicorn/no-process-exit */
import chalk from 'chalk';
import meow from 'meow';
import { messageWithCauses, stackWithCauses } from 'pony-cause';
import { installedCheck, ROOT } from 'installed-check-core';
const EXIT_CODE_ERROR_RESULT = 1;
const EXIT_CODE_INVALID_INPUT = 2;
const EXIT_CODE_UNEXPECTED_ERROR = 4;
const cli = meow(`
Usage
$ installed-check <path to module folder>
Defaults to current folder and to perform all checks.
Checks
-e, --engine-check Override default checks and explicitly request an engine range check
-p, --peer-check Override default checks and explicitly request a peer dependency range check
-c, --version-check Override default checks and explicitly request a check of installed versions
Check options
-i ARG, --ignore=ARG Excludes the named dependency from non-version checks.(Supports globs)
-d, --ignore-dev Excludes dev dependencies from non-version checks
-s, --strict Treat warnings as errors
Fix options
--fix Tries to apply all suggestions and write them back to disk
Workspace options
--no-include-workspace-root Will exclude the workspace root package
--no-workspaces Will exclude workspace packages
-w ARG, --workspace=ARG Excludes all workspace packages not matching these names / paths
--workspace-ignore=ARG Excludes the specified paths from workspace lookup. (Supports globs)
Options
--debug Prints debug info
--help Print this help and exits
--version Prints current version and exits
-v, --verbose Shows warnings
Examples
$ installed-check
`, {
flags: {
debug: { type: 'boolean' },
engineCheck: { shortFlag: 'e', type: 'boolean' },
engineIgnore: { type: 'string', isMultiple: true },
engineNoDev: { type: 'boolean' },
fix: { type: 'boolean' },
ignore: { shortFlag: 'i', type: 'string', isMultiple: true },
ignoreDev: { shortFlag: 'd', type: 'boolean' },
includeWorkspaceRoot: { type: 'boolean', 'default': true },
peerCheck: { shortFlag: 'p', type: 'boolean' },
strict: { shortFlag: 's', type: 'boolean' },
verbose: { shortFlag: 'v', type: 'boolean' },
versionCheck: { shortFlag: 'c', type: 'boolean' },
workspace: { shortFlag: 'w', type: 'string', isMultiple: true },
workspaceIgnore: { type: 'string', isMultiple: true },
workspaces: { type: 'boolean', 'default': true },
},
importMeta: import.meta,
});
if (cli.input.length > 1) {
console.error(chalk.bgRed('Invalid input:') + ` Can only handle a single folder path, but received ${cli.input.length} paths: "${cli.input.join('", "')}"` + '\n');
process.exit(EXIT_CODE_INVALID_INPUT);
}
const {
debug,
engineCheck,
engineIgnore, // deprecated
engineNoDev, // deprecated
fix = false,
includeWorkspaceRoot,
peerCheck,
strict,
verbose,
versionCheck,
workspace,
workspaceIgnore,
workspaces,
} = cli.flags;
let {
ignore,
ignoreDev,
} = cli.flags;
// Handle deprecated flags
if (engineIgnore?.length) {
ignore = [...ignore || [], ...engineIgnore];
console.error(chalk.bgRed.black('DEPRECATED:') + ' --engine-ignore is replace by --ignore');
}
if (engineNoDev) {
ignoreDev = engineNoDev;
console.error(chalk.bgRed.black('DEPRECATED:') + ' --engine-no-dev is replace by --ignore-dev');
}
/** @type {import('installed-check-core').InstalledChecks[]} */
let checks = [
...engineCheck ? /** @type {const} */ (['engine']) : [],
...peerCheck ? /** @type {const} */ (['peer']) : [],
...versionCheck ? /** @type {const} */ (['version']) : [],
];
/** @type {import('installed-check-core').LookupOptions} */
const lookupOptions = {
cwd: cli.input[0],
ignorePaths: workspaceIgnore,
includeWorkspaceRoot,
skipWorkspaces: !workspaces,
workspace,
};
/** @type {import('installed-check-core').InstalledCheckOptions} */
const checkOptions = {
noDev: ignoreDev,
ignore,
strict,
};
if (checks.length === 0) {
checks = ['engine', 'peer', 'version'];
}
if (debug) {
const { inspect } = await import('node:util');
console.log(chalk.blue('Checks:') + ' ' + inspect(checks, { colors: true, compact: true }));
console.log(chalk.blue('Lookup options:') + ' ' + inspect(lookupOptions, { colors: true, compact: true }));
console.log(chalk.blue('Check options:') + ' ' + inspect(checkOptions, { colors: true, compact: true }));
}
try {
const result = await installedCheck(checks, lookupOptions, { ...checkOptions, fix });
if (verbose && result.warnings.length) {
console.log('\n' + chalk.bgYellow.black('Warnings:') + '\n\n' + result.warnings.join('\n') + '\n');
} else if (result.errors.length) {
console.log('');
}
if (result.errors.length) {
console.error(chalk.bgRed.black('Errors:') + '\n\n' + result.errors.join('\n') + '\n');
}
if (result.suggestions.length) {
console.error(chalk.bgCyanBright.black('Suggestions:') + '\n\n' + result.suggestions.join('\n') + '\n');
}
const workspaceSuccess = /** @type {const} */ ([
...Object.entries(result.workspaceSuccess),
...(result.workspaceSuccess[ROOT] === undefined ? [] : /** @type {const} */ ([['root', result.workspaceSuccess[ROOT]]])),
]);
if (verbose && workspaceSuccess.length) {
if (result.errors.length === 0 && workspaceSuccess.length === 1 && result.workspaceSuccess[ROOT]) {
console.log(chalk.bgGreen.black('Successful!') + '\n');
} else {
const success = workspaceSuccess.filter(([, value]) => value);
const failure = workspaceSuccess.filter(([, value]) => !value);
if (success.length) {
console.log(chalk.bgGreen.black('Successful workspaces:') + ' ' + success.map(([key]) => key).join(', ') + '\n');
}
if (failure.length) {
console.log(chalk.bgRed.black('Unsuccessful workspaces:') + ' ' + failure.map(([key]) => key).join(', ') + '\n');
}
}
}
if (result.errors.length) {
process.exit(EXIT_CODE_ERROR_RESULT);
}
} catch (err) {
console.error(chalk.bgRed('Unexpected error:') + ' ' + (err instanceof Error ? messageWithCauses(err) + '\n\n' + stackWithCauses(err) : err) + '\n');
process.exit(EXIT_CODE_UNEXPECTED_ERROR);
}