Skip to content

Commit d43b42e

Browse files
adamluikudo-sync-bot
authored andcommitted
Added doc URL to all invalid arg errors ↞ [auto-sync from adamlui/minify.js]
1 parent c92e33f commit d43b42e

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

minify.js/node.js/src/cli.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env node
22

3+
const docURL = 'https://docs.minify-js.org/#-command-line-usage';
4+
35
// Import LIBS
46
const minifyJS = require(__dirname.match(/src/) ? './minify' : './minify.min'),
57
fs = require('fs'),
@@ -37,16 +39,15 @@ process.argv.forEach(arg => {
3739
else if (matchedParamOption) {
3840
if (!arg.includes('=')) {
3941
console.error(`\n${br}ERROR: Arg [--${arg.replace(/-/g, '')}] requires '=' followed by a value.${nc}`);
40-
console.error(`\n${by}For more help, type 'minify-js --help'.${nc}`);
41-
process.exit(1);
42+
printHelpCmdAndDocURL(); process.exit(1);
4243
}
4344
const value = arg.split('=')[1];
4445
config[matchedParamOption] = parseInt(value) || value;
4546
} else if (!matchedInfoCmd) {
4647
console.error(`\n${br}ERROR: Arg [${ arg }] not recognized.${nc}`);
4748
console.info(`\n${by}Valid arguments are below.${nc}`);
4849
printHelpSections(['paramOptions', 'flags', 'infoCmds']);
49-
process.exit(1);
50+
printHelpCmdAndDocURL(); process.exit(1);
5051
}});
5152

5253
// Show HELP screen if -h or --help passed
@@ -70,9 +71,8 @@ else { // run MAIN routine
7071
if (inputArg && !fs.existsSync(inputPath)) {
7172
console.error(`\n${br}Error: First argument can only be an existing file or directory.`
7273
+ `\n${ inputPath } does not exist.${nc}`
73-
+ `\n\n${bg}Example valid command: \n» minify-js . output.min.js${nc}`
74-
+ `\n\n${by}For all command options: \n» minify-js --help${nc}`);
75-
process.exit(1);
74+
+ `\n\n${bg}Example valid command: \n» minify-js . output.min.js${nc}`);
75+
printHelpCmdAndDocURL(); process.exit(1);
7676
}
7777

7878
// Find all eligible JavaScript files or arg-passed file
@@ -193,4 +193,7 @@ function printHelpSections(
193193
}
194194
}
195195

196+
function printHelpCmdAndDocURL() {
197+
console.info(`\n${by}For more help, type 'minify-js --help' or visit\n${docURL + nc}`); }
198+
196199
function printIfNotQuiet(msg) { if (!config.quietMode) console.info(msg); }

minify.js/node.js/src/minify.js

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ const fs = require('fs'),
1313

1414
function findJS(searchDir, options = {}) {
1515

16-
const exampleCall = 'findJS(\'assets/js\', { verbose: false, dotFolders: true })';
16+
const docURL = 'https://docs.minify-js.org/#findjssearchdir-options',
17+
exampleCall = 'findJS(\'assets/js\', { verbose: false, dotFoldes: true })';
18+
1719
const defaultOptions = {
1820
recursive: true, // recursively search for nested files in searchDir passed
1921
verbose: true, // enable logging
@@ -22,13 +24,16 @@ function findJS(searchDir, options = {}) {
2224
};
2325

2426
// Validate searchDir
25-
if (typeof searchDir !== 'string')
26-
return console.error('findJS() » ERROR: 1st arg <searchDir> must be a string.');
27-
else { // verify searchDir path existence
27+
if (typeof searchDir !== 'string') {
28+
console.error('findJS() » ERROR: 1st arg <searchDir> must be a string.');
29+
console.info('findJS() » For more help, please visit ' + docURL);
30+
return;
31+
} else { // verify searchDir path existence
2832
const searchPath = path.resolve(process.cwd(), searchDir);
2933
if (!fs.existsSync(searchPath)) {
3034
console.error('findJS() » ERROR: 1st arg <searchDir> must be an existing directory.');
31-
console.error(`findJS() » ${ searchPath } does not exist.`);
35+
console.error(`findJS() » ${ searchPath } does not exist.`);
36+
console.info('findJS() » For more help, please visit ' + docURL);
3237
return;
3338
}}
3439

@@ -64,7 +69,9 @@ function findJS(searchDir, options = {}) {
6469

6570
function minify(input, options = {}) {
6671

67-
const exampleCall = 'minify(\'assets/js\', { recursive: false, mangle: false })';
72+
const docURL = 'https://docs.minify-js.org/#minifyinput-options',
73+
exampleCall = 'minify(\'assets/js\', { recursive: false, mangle: false })';
74+
6875
const defaultOptions = {
6976
recursive: true, // recursively search for nested files if dir path passed
7077
verbose: true, // enable logging
@@ -75,8 +82,11 @@ function minify(input, options = {}) {
7582
};
7683

7784
// Validate input
78-
if (typeof input !== 'string') return console.error(
79-
'minify() » ERROR: 1st arg <input> must be a string.');
85+
if (typeof input !== 'string') {
86+
console.error('minify() » ERROR: 1st arg <input> must be a string.');
87+
console.info('minify() » For more help, please visit ' + docURL);
88+
return;
89+
}
8090

8191
// Validate/init options
8292
if (!validateOptions(options, defaultOptions, exampleCall)) return;
@@ -127,37 +137,47 @@ function minify(input, options = {}) {
127137
// Define INTERNAL validation function
128138

129139
function validateOptions(options, defaultOptions, exampleCall) {
130-
const logPrefix = ( validateOptions.caller?.name || 'validateOptions' ) + '() » ';
140+
141+
const docURL = 'https://docs.minify-js.org/#-api-usage';
142+
143+
// Init option strings/types
131144
const strDefaultOptions = JSON.stringify(defaultOptions, null, 2)
132145
.replace(/"([^"]+)":/g, '$1:') // strip quotes from keys
133146
.replace(/"/g, '\'') // replace double quotes w/ single quotes
134147
.replace(/\n\s*/g, ' '); // condense to single line
135148
const strValidOptions = Object.keys(defaultOptions).join(', '),
136149
booleanOptions = Object.keys(defaultOptions).filter(key => typeof defaultOptions[key] === 'boolean'),
137150
integerOptions = Object.keys(defaultOptions).filter(key => Number.isInteger(defaultOptions[key]));
151+
152+
// Define print functions
153+
const logPrefix = ( validateOptions.caller?.name || 'validateOptions' ) + '() » ';
138154
const printValidOptions = () => {
139155
console.info(`${ logPrefix }Valid options: [ ${ strValidOptions } ]`);
140156
console.info(`${ logPrefix }If omitted, default settings are: ${ strDefaultOptions }`);
141157
};
158+
const printDocURL = () => {
159+
console.info(`${ logPrefix }For more help, please visit ${docURL}`); };
160+
161+
// Validate options
142162
if (typeof options != 'object') { // validate as obj
143163
console.error(`${ logPrefix }ERROR: [options] can only be an object of key/values.`);
144164
console.info(`${ logPrefix }Example valid call: ${ exampleCall }`);
145-
printValidOptions(); return false;
165+
printValidOptions(); printDocURL(); return false;
146166
}
147167
for (const key in options) { // validate each key
148168
if (key != 'isRecursing' && !Object.prototype.hasOwnProperty.call(defaultOptions, key)) {
149169
console.error(
150170
`${ logPrefix }ERROR: \`${ key }\` is an invalid option.`);
151-
printValidOptions(); return false;
171+
printValidOptions(); printDocURL(); return false;
152172
} else if (booleanOptions.includes(key) && typeof options[key] !== 'boolean') {
153173
console.error(
154174
`${ logPrefix }ERROR: [${ key }] option can only be \`true\` or \`false\`.`);
155-
return false;
175+
printDocURL(); return false;
156176
} else if (integerOptions.includes(key)) {
157177
options[key] = parseInt(options[key], 10);
158178
if (isNaN(options[key]) || options[key] < 1) {
159179
console.error(`${ logPrefix }ERROR: [${ key }] option can only be an integer > 0.`);
160-
return false;
180+
printDocURL(); return false;
161181
}
162182
}
163183
}

0 commit comments

Comments
 (0)