@adamlui/minify.js 1.4.3 → 1.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +67 -51
- package/minify.js +118 -63
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
|
|
19
19
|
### Recursively minify all JavaScript files.
|
|
20
20
|
|
|
21
|
-
<a href="https://www.npmjs.com/package/@adamlui/minify.js"><img height=31 src="https://img.shields.io/npm/dt/%40adamlui%2Fminify.js?logo=npm&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
22
|
-
<a href="#%EF%B8%8F-mit-license"><img height=31 src="https://img.shields.io/badge/License-MIT-
|
|
23
|
-
<a href="https://www.npmjs.com/package/@adamlui/minify.js?activeTab=versions"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.4.
|
|
21
|
+
<a href="https://www.npmjs.com/package/@adamlui/minify.js"><img height=31 src="https://img.shields.io/npm/dt/%40adamlui%2Fminify.js?logo=npm&color=af68ff&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
22
|
+
<a href="#%EF%B8%8F-mit-license"><img height=31 src="https://img.shields.io/badge/License-MIT-orange.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
23
|
+
<a href="https://www.npmjs.com/package/@adamlui/minify.js?activeTab=versions"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.4.5-44cc11.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
24
24
|
<a href="https://www.npmjs.com/package/@adamlui/minify.js?activeTab=code"><img height=31 src="https://img.shields.io/npm/unpacked-size/%40adamlui%2Fminify.js?style=for-the-badge&logo=ebox&logoColor=white&labelColor=464646&color=blue"></a>
|
|
25
25
|
<a href="https://sonarcloud.io/component_measures?metric=new_vulnerabilities&id=adamlui_js-utils:minify.js/minify.js"><img height=31 src="https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fsonarcloud.io%2Fapi%2Fmeasures%2Fcomponent%3Fcomponent%3Dadamlui_js-utils%3Aminify.js%2Fminify.js%26metricKeys%3Dvulnerabilities&query=%24.component.measures.0.value&style=for-the-badge&logo=sonarcloud&logoColor=white&labelColor=464646&label=Vulnerabilities&color=gold"></a>
|
|
26
26
|
|
|
@@ -28,34 +28,38 @@
|
|
|
28
28
|
|
|
29
29
|
<br>
|
|
30
30
|
|
|
31
|
-
<img height=
|
|
31
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
32
32
|
|
|
33
33
|
## ⚡ Installation
|
|
34
34
|
|
|
35
35
|
As a **global utility**:
|
|
36
36
|
|
|
37
37
|
```
|
|
38
|
-
npm install -g @adamlui/minify.js
|
|
38
|
+
$ npm install -g @adamlui/minify.js
|
|
39
39
|
```
|
|
40
40
|
|
|
41
41
|
As a **dev dependency** (e.g. for build scripts), from your project root:
|
|
42
42
|
|
|
43
43
|
```
|
|
44
|
-
npm install -D @adamlui/minify.js
|
|
44
|
+
$ npm install -D @adamlui/minify.js
|
|
45
45
|
```
|
|
46
46
|
|
|
47
47
|
As a **runtime dependency** (e.g. for on-the-fly minification), from your project root:
|
|
48
48
|
|
|
49
49
|
```
|
|
50
|
-
npm install @adamlui/minify.js
|
|
50
|
+
$ npm install @adamlui/minify.js
|
|
51
51
|
```
|
|
52
52
|
|
|
53
|
+
<br>
|
|
54
|
+
|
|
55
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
56
|
+
|
|
53
57
|
## 💻 Command line usage
|
|
54
58
|
|
|
55
59
|
The basic **global command** is:
|
|
56
60
|
|
|
57
61
|
```
|
|
58
|
-
minify-js
|
|
62
|
+
$ minify-js
|
|
59
63
|
```
|
|
60
64
|
|
|
61
65
|
**💡 Note:** Pass `-n` or `--dry-run` to only see what files will be processed.
|
|
@@ -65,7 +69,7 @@ minify-js
|
|
|
65
69
|
To specify **input/output** paths:
|
|
66
70
|
|
|
67
71
|
```
|
|
68
|
-
minify-js [input_path] [output_path]
|
|
72
|
+
$ minify-js [input_path] [output_path]
|
|
69
73
|
```
|
|
70
74
|
|
|
71
75
|
- `[input_path]`: Path to JS file or directory containing JS files to be minified, relative to the current working directory.
|
|
@@ -84,57 +88,59 @@ To use as a **package script**, in your project's `package.json`:
|
|
|
84
88
|
```
|
|
85
89
|
|
|
86
90
|
Replace `<minify-js-cmd>` with `minify-js` + optional args. Then, `npm run build:js` can be used to run the command.
|
|
87
|
-
|
|
91
|
+
|
|
92
|
+
#
|
|
88
93
|
|
|
89
94
|
### Example commands
|
|
90
95
|
|
|
91
96
|
Minify all JavaScript files in the **current directory** (outputs to `min/`):
|
|
92
97
|
|
|
93
98
|
```
|
|
94
|
-
minify-js
|
|
99
|
+
$ minify-js
|
|
95
100
|
```
|
|
96
101
|
|
|
97
102
|
Minify all JavaScript files in a **specific directory** (outputs to `path/to/your/directory/min/`):
|
|
98
103
|
|
|
99
104
|
```
|
|
100
|
-
minify-js path/to/your/directory
|
|
105
|
+
$ minify-js path/to/your/directory
|
|
101
106
|
```
|
|
102
107
|
|
|
103
108
|
Minify a **specific file** (outputs to `path/to/your/min/file.min.js`):
|
|
104
109
|
|
|
105
110
|
```
|
|
106
|
-
minify-js path/to/your/file.js
|
|
111
|
+
$ minify-js path/to/your/file.js
|
|
107
112
|
```
|
|
108
113
|
|
|
109
114
|
Specify both **input and output** directories (outputs to `output_folder/`):
|
|
110
115
|
|
|
111
116
|
```
|
|
112
|
-
minify-js input_folder output_folder
|
|
117
|
+
$ minify-js input_folder output_folder
|
|
113
118
|
```
|
|
114
119
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
#### Config options:
|
|
120
|
+
#
|
|
118
121
|
|
|
119
|
-
|
|
120
|
-
-------------------------------|-------------------------------------------------------------------------
|
|
121
|
-
`-n` or `--dry-run` | Don't actually minify the file(s), just show if they will be processed.
|
|
122
|
-
`-d` or `--include-dotfolders` | Include dotfolders in file search.
|
|
123
|
-
`-D` or `--include-dotfiles` | Include dotfiles in file search.
|
|
124
|
-
`-R` or `--no-recursion` | Disable recursive file searching.
|
|
125
|
-
`-M` or `--no-mangle` | Disable mangling names.
|
|
126
|
-
`-q` or `--quiet` | Suppress all logging except errors.
|
|
122
|
+
### Command line options
|
|
127
123
|
|
|
128
|
-
|
|
124
|
+
```
|
|
125
|
+
Config options:
|
|
126
|
+
-n, --dry-run Don't actually minify the file(s), just show
|
|
127
|
+
if they will be processed.
|
|
128
|
+
-d, --include-dotfolders Include dotfolders in file search.
|
|
129
|
+
-D, --include-dotfiles Include dotfiles in file search.
|
|
130
|
+
-R, --no-recursion Disable recursive file searching.
|
|
131
|
+
-M, --no-mangle Disable mangling names.
|
|
132
|
+
-q, --quiet Suppress all logging except errors.
|
|
129
133
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
+
Info commands:
|
|
135
|
+
-h, --help Display help screen.
|
|
136
|
+
-v, --version Show version number.
|
|
137
|
+
```
|
|
134
138
|
|
|
135
139
|
<br>
|
|
136
140
|
|
|
137
|
-
|
|
141
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
142
|
+
|
|
143
|
+
## 🔌 API usage
|
|
138
144
|
|
|
139
145
|
You can also import **minify.js** into your app to use its API methods, both as an ECMAScript module or a CommonJS module.
|
|
140
146
|
|
|
@@ -150,17 +156,20 @@ import * as minifyJS from '@adamlui/minify.js';
|
|
|
150
156
|
const minifyJS = require('@adamlui/minify.js');
|
|
151
157
|
```
|
|
152
158
|
|
|
153
|
-
|
|
159
|
+
#
|
|
160
|
+
|
|
161
|
+
### `minify(input[, options])`
|
|
154
162
|
|
|
155
|
-
|
|
163
|
+
Minifies JavaScript code based on the string input supplied.
|
|
156
164
|
|
|
157
165
|
If **source code** is passed, it is directly minified, then an object containing `srcPath` + `code` + `error` is returned:
|
|
158
166
|
|
|
159
167
|
```js
|
|
160
168
|
const srcCode = 'function add(first, second) { return first + second; }',
|
|
161
169
|
minifyResult = minifyJS.minify(srcCode);
|
|
162
|
-
|
|
163
|
-
console.log(minifyResult.
|
|
170
|
+
|
|
171
|
+
console.log(minifyResult.error); // outputs runtime error, or `undefined` if no error
|
|
172
|
+
console.log(minifyResult.code); // outputs minified JS: function add(n,d){return n+d}
|
|
164
173
|
```
|
|
165
174
|
|
|
166
175
|
If a **file path** is passed, the file's code is loaded then minified, returning an object like above.
|
|
@@ -168,20 +177,22 @@ If a **file path** is passed, the file's code is loaded then minified, returning
|
|
|
168
177
|
If a **directory path** is passed, JavaScript files are searched for (recursively by default), each one's code is loaded then minified, then an array of objects containing `srcPath` + `code` + `error` is returned:
|
|
169
178
|
|
|
170
179
|
```js
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
180
|
+
// Outputs paths to source JS files in working directory + all nested directories
|
|
181
|
+
const minifyResults = minifyJS.minify('.');
|
|
182
|
+
minifyResults.forEach(result => console.log(result.srcPath));
|
|
183
|
+
|
|
184
|
+
// Outputs minified code of 2nd JS file if found, or `undefined` if not found
|
|
185
|
+
console.log(minifyResults[1].code);
|
|
175
186
|
```
|
|
176
187
|
|
|
177
188
|
Options are boolean, passed as object properties. For example:
|
|
178
189
|
|
|
179
190
|
```js
|
|
191
|
+
// Returns data object where dotfiles are also processed if `input` is a path
|
|
180
192
|
minifyJS.minify(input, { dotFiles: true });
|
|
181
|
-
// returns data object where dotfiles are also processed if `input` is a path
|
|
182
193
|
```
|
|
183
194
|
|
|
184
|
-
|
|
195
|
+
Available parameters (and their default settings) are:
|
|
185
196
|
|
|
186
197
|
Name | Desciption | Default value
|
|
187
198
|
-------------|---------------------------------------------------------|---------------
|
|
@@ -191,20 +202,28 @@ Name | Desciption | Default
|
|
|
191
202
|
`dotFiles` | Include dotfiles in file search. | `false`
|
|
192
203
|
`mangle` | Shorten variable names (typically to one character). | `true`
|
|
193
204
|
|
|
194
|
-
|
|
205
|
+
#
|
|
195
206
|
|
|
196
|
-
### findJS(searchDir, options)
|
|
207
|
+
### `findJS(searchDir[, options])`
|
|
197
208
|
|
|
198
|
-
|
|
209
|
+
Searches for all unminified JavaScript files within the `searchDir` string passed (useful for discovering what files [`minify()`](#minifyinput-options) will process) and returns an array containing their filepaths.
|
|
199
210
|
|
|
200
211
|
Options are boolean, passed as object properties. For example:
|
|
201
212
|
|
|
202
213
|
```js
|
|
203
|
-
|
|
204
|
-
|
|
214
|
+
// Search for unminified JS files in exactly `assets/js`:
|
|
215
|
+
const searchResults = minifyJS.findJS('assets/js', { recursive: false });
|
|
216
|
+
console.log(searchResults);
|
|
217
|
+
|
|
218
|
+
/* outputs:
|
|
219
|
+
Searching for unminified JS files...
|
|
220
|
+
Search complete. 2 files found.
|
|
221
|
+
[ 'E:\\js\\utils\\minify.js\\assets\\js\\foo.js',
|
|
222
|
+
'E:\\js\\utils\\minify.js\\assets\\js\\bar.js' ]
|
|
223
|
+
*/
|
|
205
224
|
```
|
|
206
225
|
|
|
207
|
-
|
|
226
|
+
Available parameters (and their default settings) are:
|
|
208
227
|
|
|
209
228
|
Name | Desciption | Default value
|
|
210
229
|
-------------|---------------------------------------------------------|---------------
|
|
@@ -215,10 +234,7 @@ Name | Desciption | Default
|
|
|
215
234
|
|
|
216
235
|
<br>
|
|
217
236
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
Please consider [giving a GitHub ⭐](https://github.com/adamlui/js-utils) if this helped you!
|
|
221
|
-
<br><br>
|
|
237
|
+
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
222
238
|
|
|
223
239
|
## 🏛️ MIT License
|
|
224
240
|
|
package/minify.js
CHANGED
|
@@ -13,6 +13,28 @@ function findJS(searchDir, options = {}) {
|
|
|
13
13
|
const defaultOptions = { recursive: true, verbose: true, dotFolders: false, dotFiles: false };
|
|
14
14
|
options = { ...defaultOptions, ...options };
|
|
15
15
|
|
|
16
|
+
// Validate searchDir
|
|
17
|
+
if (!searchDir) return console.error(
|
|
18
|
+
'findJS() error: Please supply a `searchDir` arg.');
|
|
19
|
+
else if (typeof searchDir !== 'string') return console.error(
|
|
20
|
+
'findJS() error: Arg `searchDir` must be a string.');
|
|
21
|
+
else { // verify searchDir path existence
|
|
22
|
+
const searchPath = path.resolve(process.cwd(), searchDir);
|
|
23
|
+
if (!fs.existsSync(searchPath)) return console.error(
|
|
24
|
+
'findJS() error: Arg `searchDir` must be an existing directory.'
|
|
25
|
+
+ `\n'${ searchPath }' does not exist.`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Validate options
|
|
29
|
+
for (const key of Object.keys(options)) {
|
|
30
|
+
if (!Object.prototype.hasOwnProperty.call(defaultOptions, key))
|
|
31
|
+
if (key !== 'isRecursing') return console.error(
|
|
32
|
+
`findJS() error: \`${ key }\` is an invalid option.`
|
|
33
|
+
+ `\nValid options: [ ${Object.keys(defaultOptions).join(', ')} ]`);
|
|
34
|
+
else if (typeof options[key] !== 'boolean') return console.error(
|
|
35
|
+
`findJS() error: \`${ key }\` option must be set to \`true\` or \`false\`.`);
|
|
36
|
+
}
|
|
37
|
+
|
|
16
38
|
// Search for unminified JS
|
|
17
39
|
const dirFiles = fs.readdirSync(searchDir), jsFiles = [];
|
|
18
40
|
if (options.verbose && !options.isRecursing) console.info('\nSearching for unminified JS files...');
|
|
@@ -44,9 +66,17 @@ function minify(input, options = {}) {
|
|
|
44
66
|
options = { ...defaultOptions, ...options };
|
|
45
67
|
|
|
46
68
|
// Validate input
|
|
47
|
-
if (typeof input !== 'string')
|
|
48
|
-
|
|
49
|
-
|
|
69
|
+
if (typeof input !== 'string') return console.error(
|
|
70
|
+
'minify() error: Arg `inputPath` must be a string.');
|
|
71
|
+
|
|
72
|
+
// Validate options
|
|
73
|
+
for (const key of Object.keys(options)) {
|
|
74
|
+
if (!Object.prototype.hasOwnProperty.call(defaultOptions, key)) return console.error(
|
|
75
|
+
`findJS() error: \`${ key }\` is an invalid option.`
|
|
76
|
+
+ `\nValid options: [ ${Object.keys(defaultOptions).join(', ')} ]`);
|
|
77
|
+
else if (typeof options[key] !== 'boolean') return console.error(
|
|
78
|
+
`findJS() error: \`${ key }\` option must be set to \`true\` or \`false\`.`);
|
|
79
|
+
}
|
|
50
80
|
|
|
51
81
|
// Minify JS based on input
|
|
52
82
|
const minifyOptions = { mangle: options.mangle ? { toplevel: true } : false };
|
|
@@ -55,9 +85,10 @@ function minify(input, options = {}) {
|
|
|
55
85
|
if (options.verbose) console.info(`Minifying ${ input }...`);
|
|
56
86
|
const minifyResult = uglifyJS.minify(fs.readFileSync(input, 'utf8'), minifyOptions);
|
|
57
87
|
if (minifyResult.error) console.error(`ERROR: ${ minifyResult.error.message }`);
|
|
58
|
-
return { code: minifyResult.code, srcPath: input,
|
|
88
|
+
return { code: minifyResult.code, srcPath: path.resolve(process.cwd(), input),
|
|
89
|
+
error: minifyResult.error };
|
|
59
90
|
} else { // dir path passed
|
|
60
|
-
return findJS(input, { recursive: options.recursive,
|
|
91
|
+
return findJS(input, { recursive: options.recursive, verbose: options.verbose,
|
|
61
92
|
dotFolders: options.dotFolders, dotFiles: options.dotFiles
|
|
62
93
|
})?.map(jsPath => { // minify found JS files
|
|
63
94
|
if (options.verbose) console.info(`Minifying ${ jsPath }...`);
|
|
@@ -71,14 +102,14 @@ function minify(input, options = {}) {
|
|
|
71
102
|
if (options.verbose) console.info('Minifying passed source code...');
|
|
72
103
|
const minifyResult = uglifyJS.minify(input, minifyOptions);
|
|
73
104
|
if (minifyResult.error) console.error(`ERROR: ${ minifyResult.error.message }`);
|
|
74
|
-
return { code: minifyResult.code, srcPath:
|
|
105
|
+
return { code: minifyResult.code, srcPath: undefined, error: minifyResult.error };
|
|
75
106
|
}
|
|
76
107
|
}
|
|
77
108
|
|
|
78
|
-
// EXPORT functions if script was required
|
|
109
|
+
// EXPORT main functions if script was required
|
|
79
110
|
if (require.main !== module) module.exports = { minify, findJS };
|
|
80
111
|
|
|
81
|
-
else { // run as CLI
|
|
112
|
+
else { // run as CLI utility
|
|
82
113
|
|
|
83
114
|
// Init UI colors
|
|
84
115
|
const nc = '\x1b[0m', // no color
|
|
@@ -87,46 +118,36 @@ else { // run as CLI tool
|
|
|
87
118
|
bg = '\x1b[1;92m'; // bright green
|
|
88
119
|
|
|
89
120
|
// Load FLAG settings
|
|
90
|
-
const config = {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
includeDotFiles:
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
quietMode: process.argv.some(arg => /^--?q(?:uiet)?(?:-?mode)?$/.test(arg))
|
|
121
|
+
const config = {};
|
|
122
|
+
const argRegex = {
|
|
123
|
+
'dryRun': /^--?(?:n|dry-?run)$/,
|
|
124
|
+
'includeDotFolders': /^--?(?:dd?|(?:include-?)?dot-?(?:folder|dir(?:ector(?:y|ie))?)s?=?(?:true|1)?)$/,
|
|
125
|
+
'includeDotFiles': /^--?(?:df|D|(?:include-?)?dot-?files?=?(?:true|1)?)$/,
|
|
126
|
+
'noRecursion': /^--?(?:R|(?:disable|no)-?recursion|recursion=(?:false|0))$/,
|
|
127
|
+
'noMangle': /^--?(?:M|(?:disable|no)-?mangle|mangle=(?:false|0))$/,
|
|
128
|
+
'quietMode': /^--?q(?:uiet)?(?:-?mode)?$/,
|
|
129
|
+
'help': /^--?h(?:elp)?$/,
|
|
130
|
+
'version': /^--?ve?r?s?i?o?n?$/
|
|
101
131
|
};
|
|
132
|
+
process.argv.forEach(arg => {
|
|
133
|
+
if (!arg.startsWith('-')) return;
|
|
134
|
+
const matchedFlag = Object.keys(argRegex).find(flag => argRegex[flag].test(arg));
|
|
135
|
+
if (matchedFlag) config[matchedFlag] = true;
|
|
136
|
+
else {
|
|
137
|
+
console.error(`\n${br}ERROR: Arg [${ arg }] not recognized.${nc}`);
|
|
138
|
+
console.info(`\n${by}Valid arguments are below.${nc}`);
|
|
139
|
+
printHelpSections(['configOptions', 'infoCmds']);
|
|
140
|
+
process.exit(1);
|
|
141
|
+
}});
|
|
102
142
|
|
|
103
143
|
// Show HELP screen if -h or --help passed
|
|
104
|
-
if (process.argv.some(arg =>
|
|
105
|
-
printHelp(`\n${by}minify-js [inputPath] [outputPath] [options]${nc}`);
|
|
106
|
-
printHelp('\nPath arguments:');
|
|
107
|
-
printHelp(' [inputPath] '
|
|
108
|
-
+ 'Path to JS file or directory containing JS files to be minified,'
|
|
109
|
-
+ ' relative to the current working directory.');
|
|
110
|
-
printHelp(' [outputPath] '
|
|
111
|
-
+ 'Path to file or directory where minified files will be stored,'
|
|
112
|
-
+ ' relative to original file location (if not provided, min/ is used).');
|
|
113
|
-
printHelp('\nConfig options:');
|
|
114
|
-
printHelp(' -n, --dry-run Don\'t actually minify the file(s),'
|
|
115
|
-
+ ' just show if they will be processed.');
|
|
116
|
-
printHelp(' -d, --include-dotfolders Include dotfolders in file search.');
|
|
117
|
-
printHelp(' -D, --include-dotfiles Include dotfiles in file search.');
|
|
118
|
-
printHelp(' -R, --no-recursion Disable recursive file searching.');
|
|
119
|
-
printHelp(' -M, --no-mangle Disable mangling names.');
|
|
120
|
-
printHelp(' -q, --quiet Suppress all logging except errors.');
|
|
121
|
-
printHelp('\nInfo commands:');
|
|
122
|
-
printHelp(' -h, --help Display this help screen.');
|
|
123
|
-
printHelp(' -v, --version Show version number.');
|
|
144
|
+
if (process.argv.some(arg => argRegex.help.test(arg))) printHelpSections();
|
|
124
145
|
|
|
125
146
|
// Show VERSION number if -v or --version passed
|
|
126
|
-
|
|
147
|
+
else if (process.argv.some(arg => argRegex.version.test(arg)))
|
|
127
148
|
console.info('v' + require('./package.json').version);
|
|
128
149
|
|
|
129
|
-
|
|
150
|
+
else { // run MAIN routine
|
|
130
151
|
|
|
131
152
|
// Init I/O args
|
|
132
153
|
const [inputArg = '', outputArg = ''] = ( // default to empty strings for error-less handling
|
|
@@ -139,7 +160,7 @@ else { // run as CLI tool
|
|
|
139
160
|
const inputPath = path.resolve(process.cwd(), inputArg);
|
|
140
161
|
if (inputArg && !fs.existsSync(inputPath)) {
|
|
141
162
|
console.error(`\n${br}Error: First argument must be an existing file or directory.`
|
|
142
|
-
+
|
|
163
|
+
+ `\n'${ inputPath }' does not exist.${nc}`
|
|
143
164
|
+ `\n\n${bg}Example valid command: \n>> minify-js . output.min.js${nc}`
|
|
144
165
|
+ `\n\n${by}For all command options: \n>> minify-js --help${nc}`);
|
|
145
166
|
process.exit(1);
|
|
@@ -200,27 +221,61 @@ else { // run as CLI tool
|
|
|
200
221
|
|
|
201
222
|
// Define LOGGING functions
|
|
202
223
|
|
|
203
|
-
function
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
+
function printHelpSections(includeSections = ['cmdFormat', 'pathArgs', 'configOptions', 'infoCmds']) {
|
|
225
|
+
const helpSections = {
|
|
226
|
+
'cmdFormat': [
|
|
227
|
+
`\n${by}minify-js [inputPath] [outputPath] [options]${nc}`
|
|
228
|
+
],
|
|
229
|
+
'pathArgs': [
|
|
230
|
+
'\nPath arguments:',
|
|
231
|
+
' [inputPath] '
|
|
232
|
+
+ 'Path to JS file or directory containing JS files to be minified,'
|
|
233
|
+
+ ' relative to the current working directory.',
|
|
234
|
+
' [outputPath] '
|
|
235
|
+
+ 'Path to file or directory where minified files will be stored,'
|
|
236
|
+
+ ' relative to original file location (if not provided, min/ is used).'
|
|
237
|
+
],
|
|
238
|
+
'configOptions': [
|
|
239
|
+
'\nConfig options:',
|
|
240
|
+
' -n, --dry-run Don\'t actually minify the file(s),'
|
|
241
|
+
+ ' just show if they will be processed.',
|
|
242
|
+
' -d, --include-dotfolders Include dotfolders in file search.',
|
|
243
|
+
' -D, --include-dotfiles Include dotfiles in file search.',
|
|
244
|
+
' -R, --no-recursion Disable recursive file searching.',
|
|
245
|
+
' -M, --no-mangle Disable mangling names.',
|
|
246
|
+
' -q, --quiet Suppress all logging except errors.'
|
|
247
|
+
],
|
|
248
|
+
'infoCmds': [
|
|
249
|
+
'\nInfo commands:',
|
|
250
|
+
' -h, --help Display help screen.',
|
|
251
|
+
' -v, --version Show version number.'
|
|
252
|
+
]
|
|
253
|
+
};
|
|
254
|
+
includeSections.forEach(section => { // print valid arg elems
|
|
255
|
+
helpSections[section]?.forEach(line => printHelpMsg(line)); });
|
|
256
|
+
|
|
257
|
+
function printHelpMsg(msg) { // wrap msg + indent 2nd+ lines (for --help screen)
|
|
258
|
+
const terminalWidth = process.stdout.columns || 80,
|
|
259
|
+
indentation = 29, lines = [], words = msg.match(/\S+|\s+/g);
|
|
260
|
+
|
|
261
|
+
// Split msg into lines of appropriate lengths
|
|
262
|
+
let currentLine = '';
|
|
263
|
+
words.forEach(word => {
|
|
264
|
+
const lineLength = terminalWidth - ( lines.length === 0 ? 0 : indentation );
|
|
265
|
+
if (currentLine.length + word.length > lineLength) { // cap/store it
|
|
266
|
+
lines.push(lines.length === 0 ? currentLine : currentLine.trimStart());
|
|
267
|
+
currentLine = '';
|
|
268
|
+
}
|
|
269
|
+
currentLine += word;
|
|
270
|
+
});
|
|
271
|
+
lines.push(lines.length === 0 ? currentLine : currentLine.trimStart());
|
|
272
|
+
|
|
273
|
+
// Print formatted msg
|
|
274
|
+
lines.forEach((line, index) => console.info(
|
|
275
|
+
index === 0 ? line // print 1st line unindented
|
|
276
|
+
: ' '.repeat(indentation) + line // print subsequent lines indented
|
|
277
|
+
));
|
|
278
|
+
}
|
|
224
279
|
}
|
|
225
280
|
|
|
226
281
|
function printIfNotQuiet(msg) { if (!config.quietMode) console.info(msg); }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adamlui/minify.js",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.5",
|
|
4
4
|
"description": "Recursively minify all JavaScript files",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Adam Lui",
|
|
@@ -15,10 +15,11 @@
|
|
|
15
15
|
"minify-js": "minify.js"
|
|
16
16
|
},
|
|
17
17
|
"scripts": {
|
|
18
|
-
"test": "bash utils/test/minify.test.sh",
|
|
18
|
+
"test": "bash utils/test/minify-cli.test.sh",
|
|
19
19
|
"bump:patch": "bash utils/bump.sh patch",
|
|
20
20
|
"bump:minor": "bash utils/bump.sh minor",
|
|
21
21
|
"bump:major": "bash utils/bump.sh major",
|
|
22
|
+
"prepublish": "npm test",
|
|
22
23
|
"publish:patch": "bash utils/bump.sh patch --publish",
|
|
23
24
|
"publish:minor": "bash utils/bump.sh minor --publish",
|
|
24
25
|
"publish:major": "bash utils/bump.sh major --publish"
|