cli.js
5.09 KB
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
#!/usr/bin/env node
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
const lodash_1 = __importDefault(require("lodash"));
const update_notifier_1 = __importDefault(require("update-notifier"));
const index_1 = __importDefault(require("../index"));
const package_json_1 = __importDefault(require("../../package.json"));
const cli_options_1 = __importStar(require("../cli-options"));
const getNcuRc_1 = __importDefault(require("../lib/getNcuRc"));
// check if a new version of ncu is available and print an update notification
const notifier = (0, update_notifier_1.default)({ pkg: package_json_1.default });
if (notifier.update && notifier.update.latest !== package_json_1.default.version) {
notifier.notify({ defer: false, isGlobal: true });
}
// manually detect option-specific help
// https://github.com/raineorshine/npm-check-updates/issues/787
const rawArgs = process.argv.slice(2);
if (rawArgs.includes('--help') && rawArgs.length > 1) {
const nonHelpArgs = rawArgs.filter(arg => arg !== '--help');
nonHelpArgs.forEach(arg => {
const option = cli_options_1.cliOptionsMap[arg.slice(2)];
if (option) {
console.log(`Usage: ncu --${option.long}`);
if (option.short) {
console.log(` ncu -${option.short}`);
}
if (option.default !== undefined && !(Array.isArray(option.default) && option.default.length === 0)) {
console.log(`Default: ${option.default}`);
}
if (option.help) {
console.log(`\n${option.help}`);
}
else if (option.description) {
console.log(`\n${option.description}`);
}
}
else {
console.log(`Unknown option: ${arg}`);
}
});
if (rawArgs.length - nonHelpArgs.length > 1) {
console.log('Would you like some help with your help?');
}
process.exit(0);
}
// start commander program
commander_1.program
.description('[filter] is a list or regex of package names to check (all others will be ignored).')
.usage('[options] [filter]');
// add cli options
cli_options_1.default.forEach(({ long, short, arg, description, default: defaultValue, parse }) =>
// handle 3rd/4th argument polymorphism
commander_1.program.option(`${short ? `-${short}, ` : ''}--${long}${arg ? ` <${arg}>` : ''}`, description, parse || defaultValue, parse ? defaultValue : undefined));
// set version option at the end
commander_1.program.version(package_json_1.default.version);
commander_1.program.parse(process.argv);
let programOpts = commander_1.program.opts();
const { configFileName, configFilePath, packageFile, mergeConfig } = programOpts;
// load .ncurc
// Do not load when global option is set
// Do not load when tests are running (an be overridden if configFilePath is set explicitly, or --mergeConfig option specified)
const rcResult = !programOpts.global && (!process.env.NCU_TESTS || configFilePath || mergeConfig)
? (0, getNcuRc_1.default)({ configFileName, configFilePath, packageFile })
: null;
// insert config arguments into command line arguments so they can all be parsed by commander
const combinedArguments = [...process.argv.slice(0, 2), ...((rcResult === null || rcResult === void 0 ? void 0 : rcResult.args) || []), ...process.argv.slice(2)];
commander_1.program.parse(combinedArguments);
programOpts = commander_1.program.opts();
// filter out undefined program options and combine cli options with config file options
const options = {
...(rcResult && Object.keys(rcResult.config).length > 0 ? { rcConfigPath: rcResult.filePath } : null),
...lodash_1.default.pickBy(commander_1.program.opts(), value => value !== undefined),
args: commander_1.program.args,
...(programOpts.filter ? { filter: programOpts.filter } : null),
};
// NOTE: Options handling and defaults go in initOptions in index.js
(0, index_1.default)(options, { cli: true });
//# sourceMappingURL=cli.js.map