@adamlui/minify.js 1.4.2 → 1.4.3

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.
Files changed (3) hide show
  1. package/README.md +34 -29
  2. package/minify.js +39 -23
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -20,7 +20,7 @@
20
20
 
21
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
22
  <a href="#%EF%B8%8F-mit-license"><img height=31 src="https://img.shields.io/badge/License-MIT-red.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.2-fc7811.svg?logo=icinga&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.3-fc7811.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
 
@@ -114,20 +114,25 @@ minify-js input_folder output_folder
114
114
 
115
115
  ### Command line options
116
116
 
117
- ```
118
- Config options:
119
- -n, --dry-run Don't actually minify the file(s), just show
120
- if they will be processed.
121
- -d, --include-dotfolders Include dotfolders in file search.
122
- -D, --include-dotfiles Include dotfiles in file search.
123
- -R, --no-recursion Disable recursive file searching.
124
- -M, --no-mangle Disable mangling names.
125
- -q, --quiet Suppress all logging except errors.
117
+ #### Config options:
126
118
 
127
- Info commands:
128
- -h, --help Display help screen.
129
- -v, --version Show version number.
130
- ```
119
+ Flag | Description
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.
127
+
128
+ #### Info commands:
129
+
130
+ Command | Description
131
+ --------------------|----------------------
132
+ `-h` or `--help` | Display help screen.
133
+ `-v` or `--version` | Show version number.
134
+
135
+ <br>
131
136
 
132
137
  ## 🔌 API reference
133
138
 
@@ -178,14 +183,15 @@ minifyJS.minify(input, { dotFiles: true });
178
183
 
179
184
  Possible parameters (and their default settings) are:
180
185
 
181
- ```
182
- recursive (true) Recursively search for nested files if dir path
183
- passed.
184
- verbose (true) Show logging in console/terminal.
185
- dotFolders (false) Include dotfolders in file search.
186
- dotFiles (false) Include dotfiles in file search.
187
- mangle (true) Shorten variable names (typically to one character).
188
- ```
186
+ Name | Desciption | Default value
187
+ -------------|---------------------------------------------------------|---------------
188
+ `recursive` | Recursively search for nested files if dir path passed. | `true`
189
+ `verbose` | Show logging in console/terminal. | `true`
190
+ `dotFolders` | Include dotfolders in file search. | `false`
191
+ `dotFiles` | Include dotfiles in file search. | `false`
192
+ `mangle` | Shorten variable names (typically to one character). | `true`
193
+
194
+ <br>
189
195
 
190
196
  ### findJS(searchDir, options)
191
197
 
@@ -200,13 +206,12 @@ minifyJS.findJS(searchDir, { recursive: false });
200
206
 
201
207
  Possible parameters (and their default settings) are:
202
208
 
203
- ```
204
- recursive (true) Recursively search for nested files if dir path
205
- passed.
206
- verbose (false) Show logging in console/terminal.
207
- dotFolders (false) Include dotfolders in file search.
208
- dotFiles (false) Include dotfiles in file search.
209
- ```
209
+ Name | Desciption | Default value
210
+ -------------|---------------------------------------------------------|---------------
211
+ `recursive` | Recursively search for nested files if dir path passed. | `true`
212
+ `verbose` | Show logging in console/terminal. | `true`
213
+ `dotFolders` | Include dotfolders in file search. | `false`
214
+ `dotFiles` | Include dotfiles in file search. | `false`
210
215
 
211
216
  <br>
212
217
 
package/minify.js CHANGED
@@ -8,31 +8,47 @@ const fs = require('fs'),
8
8
  // Define MAIN functions
9
9
 
10
10
  function findJS(searchDir, options = {}) {
11
- const defaultOptions = { recursive: true, verbose: false, dotFolders: false, dotFiles: false };
11
+
12
+ // Init options
13
+ const defaultOptions = { recursive: true, verbose: true, dotFolders: false, dotFiles: false };
12
14
  options = { ...defaultOptions, ...options };
15
+
16
+ // Search for unminified JS
13
17
  const dirFiles = fs.readdirSync(searchDir), jsFiles = [];
18
+ if (options.verbose && !options.isRecursing) console.info('\nSearching for unminified JS files...');
14
19
  dirFiles.forEach(file => {
15
20
  const filePath = path.resolve(searchDir, file);
16
21
  if (fs.statSync(filePath).isDirectory() && file != 'node_modules'
17
- && (options.dotFolders || !file.startsWith('.')) && options.recursive) {
18
- if (options.verbose) console.info(`Searching for unminified JS files in: ${filePath}...`);
22
+ && (options.dotFolders || !file.startsWith('.')) && options.recursive)
19
23
  jsFiles.push( // recursively find unminified JS in eligible dir
20
24
  ...findJS(filePath, { ...options, isRecursing: true }));
21
- } else if (/\.js(?<!\.min\.js)$/.test(file)
25
+ else if (/\.js(?<!\.min\.js)$/.test(file)
22
26
  && (options.dotFiles || !file.startsWith('.')))
23
27
  jsFiles.push(filePath); // store eligible unminified JS file for minification
24
28
  });
25
- if (options.isRecursing || jsFiles.length > 0) return jsFiles;
26
- else if (options.verbose) console.info('\nNo unminified JavaScript files found.');
29
+
30
+ // Log/return final result
31
+ if (!options.isRecursing && options.verbose) {
32
+ console.info('Search complete. '
33
+ + ( jsFiles.length === 0 ? 'No' : jsFiles.length )
34
+ + ` file${ jsFiles.length > 1 ? 's' : '' } found.`);
35
+ }
36
+ return options.isRecursing || jsFiles.length > 0 ? jsFiles : [];
27
37
  }
28
38
 
29
39
  function minify(input, options = {}) {
40
+
41
+ // Init options
30
42
  const defaultOptions = {
31
43
  recursive: true, verbose: true, dotFolders: false, dotFiles: false, mangle: true };
32
44
  options = { ...defaultOptions, ...options };
45
+
46
+ // Validate input
33
47
  if (typeof input !== 'string')
34
48
  return console.error('ERROR:'
35
49
  + ' First argument must be a string of source code or file/folder path.');
50
+
51
+ // Minify JS based on input
36
52
  const minifyOptions = { mangle: options.mangle ? { toplevel: true } : false };
37
53
  if (fs.existsSync(input)) { // minify based on path arg
38
54
  if (input.endsWith('.js')) { // file path passed
@@ -41,10 +57,9 @@ function minify(input, options = {}) {
41
57
  if (minifyResult.error) console.error(`ERROR: ${ minifyResult.error.message }`);
42
58
  return { code: minifyResult.code, srcPath: input, error: minifyResult.error };
43
59
  } else { // dir path passed
44
- if (options.verbose) console.info('Searching for unminified JS files...');
45
- return findJS(input, { recursive: options.recursive,
46
- dotFolders: options.dotFolders, dotFiles: options.dotFiles })
47
- ?.map(jsPath => { // minify found JS files
60
+ return findJS(input, { recursive: options.recursive, verbosity: options.verbose,
61
+ dotFolders: options.dotFolders, dotFiles: options.dotFiles
62
+ })?.map(jsPath => { // minify found JS files
48
63
  if (options.verbose) console.info(`Minifying ${ jsPath }...`);
49
64
  const srcCode = fs.readFileSync(jsPath, 'utf8'),
50
65
  minifyResult = uglifyJS.minify(srcCode, minifyOptions);
@@ -66,7 +81,7 @@ if (require.main !== module) module.exports = { minify, findJS };
66
81
  else { // run as CLI tool
67
82
 
68
83
  // Init UI colors
69
- const nc = '\x1b[0m', // no color
84
+ const nc = '\x1b[0m', // no color
70
85
  br = '\x1b[1;91m', // bright red
71
86
  by = '\x1b[1;33m', // bright yellow
72
87
  bg = '\x1b[1;92m'; // bright green
@@ -75,14 +90,14 @@ else { // run as CLI tool
75
90
  const config = {
76
91
  dryRun: process.argv.some(arg => /^--?(?:n|dry-?run)$/.test(arg)),
77
92
  includeDotFolders: process.argv.some(arg =>
78
- /^--?(?:dd?|(?:include-?)?dot-?(?:folder|dir(?:ector(?:y|ie))?)s?)$/.test(arg)),
93
+ /^--?(?:dd?|(?:include-?)?dot-?(?:folder|dir(?:ector(?:y|ie))?)s?=?(?:true|1)?)$/.test(arg)),
79
94
  includeDotFiles: process.argv.some(arg =>
80
- /^--?(?:df|D|(?:include-?)?dot-?files?)$/.test(arg)),
95
+ /^--?(?:df|D|(?:include-?)?dot-?files?=?(?:true|1)?)$/.test(arg)),
81
96
  noRecursion: process.argv.some(arg =>
82
- /^--?(?:R|(?:disable|no)-?recursion)$/.test(arg)),
97
+ /^--?(?:R|(?:disable|no)-?recursion|recursion=(?:false|0))$/.test(arg)),
83
98
  noMangle: process.argv.some(arg =>
84
- /^--?(?:M|(?:disable|no)-?mangle)$/.test(arg)),
85
- quietMode: process.argv.some(arg => /^--?q(?:uiet)?$/.test(arg))
99
+ /^--?(?:M|(?:disable|no)-?mangle|mangle=(?:false|0))$/.test(arg)),
100
+ quietMode: process.argv.some(arg => /^--?q(?:uiet)?(?:-?mode)?$/.test(arg))
86
101
  };
87
102
 
88
103
  // Show HELP screen if -h or --help passed
@@ -131,19 +146,20 @@ else { // run as CLI tool
131
146
  }
132
147
 
133
148
  // Find all eligible JavaScript files or arg-passed file
134
- printIfNotQuiet('Searching for unminified JS files...');
135
149
  const unminnedJSfiles = inputArg.endsWith('.js') ? [inputPath]
136
- : findJS(inputPath, { recursive: !config.noRecursion });
150
+ : findJS(inputPath, { recursive: !config.noRecursion, verbose: !config.quietMode });
137
151
 
138
- if (config.dryRun && unminnedJSfiles?.length > 0) { // print files to be processed
139
- console.info(`\n${by}JS files to be minified:${nc}`);
140
- unminnedJSfiles?.forEach(file => console.info(file));
152
+ if (config.dryRun) { // -n or --dry-run passed
153
+ if (unminnedJSfiles.length > 0) { // print files to be processed
154
+ console.info(`\n${by}JS files to be minified:${nc}`);
155
+ unminnedJSfiles.forEach(file => console.info(file));
156
+ } else console.info(`${by}\nNo JS files will be minified.${nc}`);
141
157
 
142
158
  } else { // actually minify JavaScript files
143
159
 
144
160
  // Build array of minification data
145
161
  const failedPaths = [];
146
- const minifyData = unminnedJSfiles?.map(jsPath => {
162
+ const minifyData = unminnedJSfiles.map(jsPath => {
147
163
  const minifyResult = minify(jsPath, { verbose: !config.quietMode, mangle: !config.noMangle });
148
164
  if (minifyResult.error) failedPaths.push(jsPath);
149
165
  return minifyResult;
@@ -172,7 +188,7 @@ else { // run as CLI tool
172
188
  printIfNotQuiet(`\n${bg}Minification complete!${nc}`);
173
189
  printIfNotQuiet(
174
190
  `${ minifyData.length } file${ minifyData.length > 1 ? 's' : '' } minified.`);
175
- } else printIfNotQuiet(`${by}No unminified JavaScript files processed successfully.${nc}`);
191
+ } else printIfNotQuiet(`${by}No unminified JavaScript files processed.${nc}`);
176
192
  if (failedPaths.length > 0) {
177
193
  printIfNotQuiet(`\n${br}`
178
194
  + `${ failedPaths.length } file${ failedPaths.length > 1 ? 's' : '' }`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adamlui/minify.js",
3
- "version": "1.4.2",
3
+ "version": "1.4.3",
4
4
  "description": "Recursively minify all JavaScript files",
5
5
  "author": {
6
6
  "name": "Adam Lui",