@adamlui/minify.js 1.4.4 → 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.
Files changed (3) hide show
  1. package/README.md +13 -5
  2. package/minify.js +70 -39
  3. package/package.json +3 -2
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&color=af68ff&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-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.4-44cc11.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.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
 
@@ -192,7 +192,7 @@ Options are boolean, passed as object properties. For example:
192
192
  minifyJS.minify(input, { dotFiles: true });
193
193
  ```
194
194
 
195
- Possible parameters (and their default settings) are:
195
+ Available parameters (and their default settings) are:
196
196
 
197
197
  Name | Desciption | Default value
198
198
  -------------|---------------------------------------------------------|---------------
@@ -211,11 +211,19 @@ Searches for all unminified JavaScript files within the `searchDir` string passe
211
211
  Options are boolean, passed as object properties. For example:
212
212
 
213
213
  ```js
214
- // Returns array containing filepaths to unminified JS files in exactly `searchDir`
215
- minifyJS.findJS(searchDir, { recursive: false });
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
+ */
216
224
  ```
217
225
 
218
- Possible parameters (and their default settings) are:
226
+ Available parameters (and their default settings) are:
219
227
 
220
228
  Name | Desciption | Default value
221
229
  -------------|---------------------------------------------------------|---------------
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
- return console.error('ERROR:'
49
- + ' First argument must be a string of source code or file/folder path.');
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, error: minifyResult.error };
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, verbosity: options.verbose,
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: input, error: minifyResult.error };
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 tool
112
+ else { // run as CLI utility
82
113
 
83
114
  // Init UI colors
84
115
  const nc = '\x1b[0m', // no color
@@ -103,14 +134,14 @@ else { // run as CLI tool
103
134
  const matchedFlag = Object.keys(argRegex).find(flag => argRegex[flag].test(arg));
104
135
  if (matchedFlag) config[matchedFlag] = true;
105
136
  else {
106
- console.error(`\n${br}ERROR: Arg '${ arg }' not recognized.${nc}`);
137
+ console.error(`\n${br}ERROR: Arg [${ arg }] not recognized.${nc}`);
107
138
  console.info(`\n${by}Valid arguments are below.${nc}`);
108
- printHelpScreen(['configOptions', 'infoCmds']);
139
+ printHelpSections(['configOptions', 'infoCmds']);
109
140
  process.exit(1);
110
141
  }});
111
142
 
112
143
  // Show HELP screen if -h or --help passed
113
- if (process.argv.some(arg => argRegex.help.test(arg))) printHelpScreen();
144
+ if (process.argv.some(arg => argRegex.help.test(arg))) printHelpSections();
114
145
 
115
146
  // Show VERSION number if -v or --version passed
116
147
  else if (process.argv.some(arg => argRegex.version.test(arg)))
@@ -129,7 +160,7 @@ else { // run as CLI tool
129
160
  const inputPath = path.resolve(process.cwd(), inputArg);
130
161
  if (inputArg && !fs.existsSync(inputPath)) {
131
162
  console.error(`\n${br}Error: First argument must be an existing file or directory.`
132
- + `'\n${ inputPath }' does not exist.${nc}`
163
+ + `\n'${ inputPath }' does not exist.${nc}`
133
164
  + `\n\n${bg}Example valid command: \n>> minify-js . output.min.js${nc}`
134
165
  + `\n\n${by}For all command options: \n>> minify-js --help${nc}`);
135
166
  process.exit(1);
@@ -190,32 +221,9 @@ else { // run as CLI tool
190
221
 
191
222
  // Define LOGGING functions
192
223
 
193
- function printHelpMsg(msg) { // wrap msg + indent 2nd+ lines (for --help screen)
194
- const terminalWidth = process.stdout.columns || 80,
195
- indentation = 29, lines = [], words = msg.match(/\S+|\s+/g);
196
-
197
- // Split msg into lines of appropriate lengths
198
- let currentLine = '';
199
- words.forEach(word => {
200
- const lineLength = terminalWidth - ( lines.length === 0 ? 0 : indentation );
201
- if (currentLine.length + word.length > lineLength) { // cap/store it
202
- lines.push(lines.length === 0 ? currentLine : currentLine.trimStart());
203
- currentLine = '';
204
- }
205
- currentLine += word;
206
- });
207
- lines.push(lines.length === 0 ? currentLine : currentLine.trimStart());
208
-
209
- // Print formatted msg
210
- lines.forEach((line, index) => console.info(
211
- index === 0 ? line // print 1st line unindented
212
- : ' '.repeat(indentation) + line // print subsequent lines indented
213
- ));
214
- }
215
-
216
- function printHelpScreen(includeSections = ['sampleCmd', 'pathArgs', 'configOptions', 'infoCmds']) {
217
- const sections = {
218
- 'sampleCmd': [
224
+ function printHelpSections(includeSections = ['cmdFormat', 'pathArgs', 'configOptions', 'infoCmds']) {
225
+ const helpSections = {
226
+ 'cmdFormat': [
219
227
  `\n${by}minify-js [inputPath] [outputPath] [options]${nc}`
220
228
  ],
221
229
  'pathArgs': [
@@ -244,7 +252,30 @@ else { // run as CLI tool
244
252
  ]
245
253
  };
246
254
  includeSections.forEach(section => { // print valid arg elems
247
- if (sections[section]) sections[section].forEach(line => printHelpMsg(line)); });
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
+ }
248
279
  }
249
280
 
250
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.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"