@node-minify/utils 6.4.0 → 7.1.0

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 (2) hide show
  1. package/lib/utils.js +22 -43
  2. package/package.json +3 -3
package/lib/utils.js CHANGED
@@ -4,13 +4,9 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.utils = void 0;
7
-
8
7
  var _fs = _interopRequireDefault(require("fs"));
9
-
10
8
  var _gzipSize = _interopRequireDefault(require("gzip-size"));
11
-
12
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
-
14
10
  /*!
15
11
  * node-minify
16
12
  * Copyright(c) 2011-2022 Rodolphe Stoclin
@@ -20,17 +16,18 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
20
16
  /**
21
17
  * Module dependencies.
22
18
  */
19
+
23
20
  const utils = {};
21
+
24
22
  /**
25
23
  * Read content from file.
26
24
  *
27
25
  * @param {String} file
28
26
  * @returns {String}
29
27
  */
30
-
31
28
  exports.utils = utils;
32
-
33
29
  utils.readFile = file => _fs.default.readFileSync(file, 'utf8');
30
+
34
31
  /**
35
32
  * Write content into file.
36
33
  *
@@ -39,110 +36,102 @@ utils.readFile = file => _fs.default.readFileSync(file, 'utf8');
39
36
  * @param {Number} index - index of the file being processed
40
37
  * @returns {String}
41
38
  */
42
-
43
-
44
39
  utils.writeFile = ({
45
40
  file,
46
41
  content,
47
42
  index
48
43
  }) => {
49
44
  const _file = index !== undefined ? file[index] : file;
50
-
51
45
  if (!_fs.default.existsSync(_file) || _fs.default.existsSync(_file) && !_fs.default.lstatSync(_file).isDirectory()) {
52
46
  _fs.default.writeFileSync(_file, content, 'utf8');
53
47
  }
54
-
55
48
  return content;
56
49
  };
50
+
51
+ /**
52
+ * Delete file.
53
+ *
54
+ * @param {String} file
55
+ * @returns {String}
56
+ */
57
+ utils.deleteFile = file => _fs.default.unlinkSync(file);
58
+
57
59
  /**
58
60
  * Builds arguments array based on an object.
59
61
  *
60
62
  * @param {Object} options
61
63
  * @returns {Array}
62
64
  */
63
-
64
-
65
65
  utils.buildArgs = options => {
66
66
  const args = [];
67
67
  Object.keys(options).forEach(key => {
68
68
  if (options[key] && options[key] !== false) {
69
69
  args.push('--' + key);
70
70
  }
71
-
72
71
  if (options[key] && options[key] !== true) {
73
72
  args.push(options[key]);
74
73
  }
75
74
  });
76
75
  return args;
77
76
  };
77
+
78
78
  /**
79
79
  * Clone an object.
80
80
  *
81
81
  * @param {Object} obj
82
82
  * @returns {Object}
83
83
  */
84
-
85
-
86
84
  utils.clone = obj => JSON.parse(JSON.stringify(obj));
85
+
87
86
  /**
88
87
  * Get the file size in bytes.
89
88
  *
90
89
  * @returns {String}
91
90
  */
92
-
93
-
94
91
  utils.getFilesizeInBytes = file => {
95
92
  const stats = _fs.default.statSync(file);
96
-
97
93
  const fileSizeInBytes = stats.size;
98
94
  return utils.prettyBytes(fileSizeInBytes);
99
95
  };
96
+
100
97
  /**
101
98
  * Get the gzipped file size in bytes.
102
99
  *
103
100
  * @returns {Promise.<String>}
104
101
  */
105
-
106
-
107
102
  utils.getFilesizeGzippedInBytes = file => {
108
103
  return new Promise(resolve => {
109
104
  const source = _fs.default.createReadStream(file);
110
-
111
105
  source.pipe(_gzipSize.default.stream()).on('gzip-size', size => {
112
106
  resolve(utils.prettyBytes(size));
113
107
  });
114
108
  });
115
109
  };
110
+
116
111
  /**
117
112
  * Get the size in human readable.
118
113
  * From https://github.com/sindresorhus/pretty-bytes
119
114
  *
120
115
  * @returns {String}
121
116
  */
122
-
123
-
124
117
  utils.prettyBytes = num => {
125
118
  const UNITS = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
126
-
127
119
  if (!Number.isFinite(num)) {
128
120
  throw new TypeError(`Expected a finite number, got ${typeof num}: ${num}`);
129
121
  }
130
-
131
122
  const neg = num < 0;
132
-
133
123
  if (neg) {
134
124
  num = -num;
135
125
  }
136
-
137
126
  if (num < 1) {
138
127
  return (neg ? '-' : '') + num + ' B';
139
128
  }
140
-
141
129
  const exponent = Math.min(Math.floor(Math.log(num) / Math.log(1000)), UNITS.length - 1);
142
130
  const numStr = Number((num / Math.pow(1000, exponent)).toPrecision(3));
143
131
  const unit = UNITS[exponent];
144
132
  return (neg ? '-' : '') + numStr + ' ' + unit;
145
133
  };
134
+
146
135
  /**
147
136
  * Set the file name as minified.
148
137
  * eg. file.js returns file.min.js
@@ -153,30 +142,24 @@ utils.prettyBytes = num => {
153
142
  * @param {Boolean} replaceInPlace
154
143
  * @returns {String}
155
144
  */
156
-
157
-
158
145
  utils.setFileNameMin = (file, output, publicFolder, replaceInPlace) => {
159
146
  const filePath = file.substr(0, file.lastIndexOf('/') + 1);
160
147
  const fileWithoutPath = file.substr(file.lastIndexOf('/') + 1);
161
148
  let fileWithoutExtension = fileWithoutPath.substr(0, fileWithoutPath.lastIndexOf('.'));
162
-
163
149
  if (publicFolder) {
164
150
  fileWithoutExtension = publicFolder + fileWithoutExtension;
165
151
  }
166
-
167
152
  if (replaceInPlace) {
168
153
  fileWithoutExtension = filePath + fileWithoutExtension;
169
154
  }
170
-
171
155
  return output.replace('$1', fileWithoutExtension);
172
156
  };
157
+
173
158
  /**
174
159
  * Compress a single file.
175
160
  *
176
161
  * @param {Object} settings
177
162
  */
178
-
179
-
180
163
  utils.compressSingleFile = settings => {
181
164
  const content = settings.content ? settings.content : utils.getContentFromFiles(settings.input);
182
165
  return settings.sync ? utils.runSync({
@@ -187,21 +170,20 @@ utils.compressSingleFile = settings => {
187
170
  content
188
171
  });
189
172
  };
173
+
190
174
  /**
191
175
  * Concatenate all input files and get the data.
192
176
  *
193
177
  * @param {String|Array} input
194
178
  * @return {String}
195
179
  */
196
-
197
-
198
180
  utils.getContentFromFiles = input => {
199
181
  if (!Array.isArray(input)) {
200
182
  return _fs.default.readFileSync(input, 'utf8');
201
183
  }
202
-
203
184
  return input.map(path => !_fs.default.existsSync(path) || _fs.default.existsSync(path) && !_fs.default.lstatSync(path).isDirectory() ? _fs.default.readFileSync(path, 'utf8') : '').join('\n');
204
185
  };
186
+
205
187
  /**
206
188
  * Run compressor in sync.
207
189
  *
@@ -210,8 +192,6 @@ utils.getContentFromFiles = input => {
210
192
  * @param {Number} index - index of the file being processed
211
193
  * @return {String}
212
194
  */
213
-
214
-
215
195
  utils.runSync = ({
216
196
  settings,
217
197
  content,
@@ -222,6 +202,7 @@ utils.runSync = ({
222
202
  callback: null,
223
203
  index
224
204
  });
205
+
225
206
  /**
226
207
  * Run compressor in async.
227
208
  *
@@ -230,8 +211,6 @@ utils.runSync = ({
230
211
  * @param {Number} index - index of the file being processed
231
212
  * @return {Promise}
232
213
  */
233
-
234
-
235
214
  utils.runAsync = ({
236
215
  settings,
237
216
  content,
@@ -245,13 +224,13 @@ utils.runAsync = ({
245
224
  if (err) {
246
225
  return reject(err);
247
226
  }
248
-
249
227
  resolve(min);
250
228
  },
251
229
  index
252
230
  });
253
231
  });
254
232
  };
233
+
255
234
  /**
256
235
  * Expose `utils`.
257
236
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@node-minify/utils",
3
- "version": "6.4.0",
3
+ "version": "7.1.0",
4
4
  "description": "utils for @node-minify",
5
5
  "keywords": [
6
6
  "compressor",
@@ -12,7 +12,7 @@
12
12
  "license": "MIT",
13
13
  "main": "lib/utils.js",
14
14
  "engines": {
15
- "node": ">=12.0.0"
15
+ "node": ">=14.0.0"
16
16
  },
17
17
  "directories": {
18
18
  "lib": "lib",
@@ -34,5 +34,5 @@
34
34
  "dependencies": {
35
35
  "gzip-size": "6.0.0"
36
36
  },
37
- "gitHead": "d529747a661487597e9be510d3d9e2df1bc0f556"
37
+ "gitHead": "94cef2d5d653c3bddc3e603b4e25c135b0b6f4b3"
38
38
  }