Skip to content

Commit a8b1026

Browse files
oidualcArcanemagus
authored andcommitted
Add arguments controlling the flags (#12)
* Add arguments controlling the flags * Add options usage help
1 parent 65bdc64 commit a8b1026

File tree

3 files changed

+173
-12
lines changed

3 files changed

+173
-12
lines changed

check-peer-deps.js

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,58 @@
11
const { exec } = require('sb-exec');
22
const semver = require('semver');
3-
4-
// Flags
5-
// Enable debug output
6-
const DEBUG = false;
7-
// Include development packages when checking whether a peerDependency has been
8-
// satisfied.
9-
const INCLUDE_DEV = true;
10-
// Maximum allowed retries for npm commands
11-
const MAX_RETRIES = 2;
3+
const commandLineArgs = require('command-line-args');
4+
const commandLineUsage = require('command-line-usage');
5+
6+
const optionDefinitions = [
7+
{
8+
name: 'help',
9+
description: 'Show this help',
10+
type: Boolean,
11+
alias: 'h',
12+
},
13+
{
14+
name: 'debug',
15+
description: 'Enable debug information',
16+
type: Boolean,
17+
alias: 'd',
18+
defaultValue: false,
19+
},
20+
{
21+
name: 'no-include-dev',
22+
description: "Don't include development packages when checking whether a " +
23+
'peerDependency has been satisfied',
24+
defaultValue: false,
25+
},
26+
{
27+
name: 'max-retries',
28+
description: 'Specify how many retries are allowed for [underline]{npm} commands',
29+
type: Number,
30+
typeLabel: '[underline]{retries}',
31+
defaultValue: 2,
32+
},
33+
];
34+
35+
const usageSections = [
36+
{
37+
header: 'check-peer-deps',
38+
content: 'Verifies that the peerDependency requirements of all top level ' +
39+
'dependencies are satisfied.',
40+
},
41+
{
42+
header: 'Options',
43+
optionList: optionDefinitions,
44+
},
45+
];
46+
47+
let options;
1248

1349
// Internal vars
1450
const deps = new Map();
1551
const npmVers = new Map();
1652
const peerDeps = new Map();
1753

1854
const log = (value) => {
19-
if (DEBUG) {
55+
if (options.debug) {
2056
console.log(value);
2157
}
2258
};
@@ -31,7 +67,7 @@ const addDeps = (dependencies) => {
3167
const npmView = async (name, keys) => {
3268
const opts = ['view', '--json', name].concat(keys);
3369
log(`Running 'npm ${opts.join(' ')}'`);
34-
let remainingTries = MAX_RETRIES;
70+
let remainingTries = options['max-retries'];
3571
let output;
3672
do {
3773
try {
@@ -169,6 +205,13 @@ const checkAllPeerDeps = async () => {
169205

170206
// Main function
171207
async function checkPeerDeps() {
208+
options = commandLineArgs(optionDefinitions);
209+
210+
if (options.help) {
211+
console.log(commandLineUsage(usageSections));
212+
process.exit(0);
213+
}
214+
172215
// eslint-disable-next-line import/no-dynamic-require
173216
const packageConfig = require(`${process.cwd()}/package.json`);
174217
if (!packageConfig.dependencies) {
@@ -178,7 +221,7 @@ async function checkPeerDeps() {
178221
// Get the dependencies to process
179222
addDeps(packageConfig.dependencies);
180223

181-
if (INCLUDE_DEV && packageConfig.devDependencies) {
224+
if (!options['no-include-dev'] && packageConfig.devDependencies) {
182225
addDeps(packageConfig.devDependencies);
183226
}
184227

package-lock.json

Lines changed: 116 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
"test": "echo \"Error: no test specified\" && exit 1"
2525
},
2626
"dependencies": {
27+
"command-line-args": "^4.0.7",
28+
"command-line-usage": "^4.0.2",
2729
"sb-exec": "^4.0.0",
2830
"semver": "^5.4.1"
2931
},

0 commit comments

Comments
 (0)