/
index.js
166 lines (135 loc) Β· 4.07 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
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
'use strict';
const path = require('path');
const buildMinimistOptions = require('minimist-options');
const yargs = require('yargs-parser');
const camelcaseKeys = require('camelcase-keys');
const decamelizeKeys = require('decamelize-keys');
const trimNewlines = require('trim-newlines');
const redent = require('redent');
const readPkgUp = require('read-pkg-up');
const hardRejection = require('hard-rejection');
const normalizePackageData = require('normalize-package-data');
// Prevent caching of this module so module.parent is always accurate
delete require.cache[__filename];
const parentDir = path.dirname(module.parent.filename);
const meow = (helpText, options) => {
if (typeof helpText !== 'string') {
options = helpText;
helpText = '';
}
options = {
pkg: readPkgUp.sync({
cwd: parentDir,
normalize: false
}).packageJson || {},
argv: process.argv.slice(2),
inferType: false,
input: 'string',
help: helpText,
autoHelp: true,
autoVersion: true,
booleanDefault: false,
hardRejection: true,
...options
};
if (options.hardRejection) {
hardRejection();
}
const minimistFlags = options.flags && typeof options.booleanDefault !== 'undefined' ? Object.keys(options.flags).reduce(
(flags, flag) => {
if (flags[flag].type === 'boolean' && !Object.prototype.hasOwnProperty.call(flags[flag], 'default')) {
flags[flag].default = options.booleanDefault;
}
return flags;
},
options.flags
) : options.flags;
// Get a list of required flags
const requiredFlags = [];
if (typeof options.flags !== 'undefined') {
for (const flagName of Object.keys(options.flags)) {
if (options.flags[flagName].isRequired) {
requiredFlags.push(flagName);
}
}
}
let minimistoptions = {
arguments: options.input,
...minimistFlags
};
minimistoptions = decamelizeKeys(minimistoptions, '-', {exclude: ['stopEarly', '--']});
if (options.inferType) {
delete minimistoptions.arguments;
}
minimistoptions = buildMinimistOptions(minimistoptions);
if (minimistoptions['--']) {
minimistoptions.configuration = {
...minimistoptions.configuration,
'populate--': true
};
}
const {pkg} = options;
const argv = yargs(options.argv, minimistoptions);
let help = redent(trimNewlines((options.help || '').replace(/\t+\n*$/, '')), 2);
normalizePackageData(pkg);
process.title = pkg.bin ? Object.keys(pkg.bin)[0] : pkg.name;
let {description} = options;
if (!description && description !== false) {
({description} = pkg);
}
help = (description ? `\n ${description}\n` : '') + (help ? `\n${help}\n` : '\n');
const showHelp = code => {
console.log(help);
process.exit(typeof code === 'number' ? code : 2);
};
const showVersion = () => {
console.log(typeof options.version === 'string' ? options.version : pkg.version);
process.exit();
};
if (argv._.length === 0 && options.argv.length === 1) {
if (argv.version === true && options.autoVersion) {
showVersion();
}
if (argv.help === true && options.autoHelp) {
showHelp(0);
}
}
const input = argv._;
delete argv._;
const flags = camelcaseKeys(argv, {exclude: ['--', /^\w$/]});
const unnormalizedFlags = {...flags};
if (options.flags !== undefined) {
for (const flagValue of Object.values(options.flags)) {
delete flags[flagValue.alias];
}
}
// Get a list of missing flags, that are required
const missing = [];
for (const requiredFlag of requiredFlags) {
let requiredByFunction = true;
if (typeof options.flags[requiredFlag].isRequired === 'function') {
requiredByFunction = options.flags[requiredFlag].isRequired(flags, input);
}
if (typeof flags[requiredFlag] === 'undefined' && requiredByFunction) {
missing.push({key: requiredFlag, ...options.flags[requiredFlag]});
}
}
// Print error message for missing flags
if (missing.length > 0) {
console.log(`Missing required option${missing.length > 1 ? 's' : ''}`);
for (const flag of missing) {
console.log(`\t--${flag.key}${flag.alias ? `, -${flag.alias}` : ''}`);
}
process.exit(2);
}
return {
input,
flags,
unnormalizedFlags,
pkg,
help,
showHelp,
showVersion
};
};
module.exports = meow;