@node-minify/utils 7.0.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 +15 -45
  2. package/package.json +2 -2
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,119 +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
+
57
51
  /**
58
52
  * Delete file.
59
53
  *
60
54
  * @param {String} file
61
55
  * @returns {String}
62
56
  */
63
-
64
-
65
57
  utils.deleteFile = file => _fs.default.unlinkSync(file);
58
+
66
59
  /**
67
60
  * Builds arguments array based on an object.
68
61
  *
69
62
  * @param {Object} options
70
63
  * @returns {Array}
71
64
  */
72
-
73
-
74
65
  utils.buildArgs = options => {
75
66
  const args = [];
76
67
  Object.keys(options).forEach(key => {
77
68
  if (options[key] && options[key] !== false) {
78
69
  args.push('--' + key);
79
70
  }
80
-
81
71
  if (options[key] && options[key] !== true) {
82
72
  args.push(options[key]);
83
73
  }
84
74
  });
85
75
  return args;
86
76
  };
77
+
87
78
  /**
88
79
  * Clone an object.
89
80
  *
90
81
  * @param {Object} obj
91
82
  * @returns {Object}
92
83
  */
93
-
94
-
95
84
  utils.clone = obj => JSON.parse(JSON.stringify(obj));
85
+
96
86
  /**
97
87
  * Get the file size in bytes.
98
88
  *
99
89
  * @returns {String}
100
90
  */
101
-
102
-
103
91
  utils.getFilesizeInBytes = file => {
104
92
  const stats = _fs.default.statSync(file);
105
-
106
93
  const fileSizeInBytes = stats.size;
107
94
  return utils.prettyBytes(fileSizeInBytes);
108
95
  };
96
+
109
97
  /**
110
98
  * Get the gzipped file size in bytes.
111
99
  *
112
100
  * @returns {Promise.<String>}
113
101
  */
114
-
115
-
116
102
  utils.getFilesizeGzippedInBytes = file => {
117
103
  return new Promise(resolve => {
118
104
  const source = _fs.default.createReadStream(file);
119
-
120
105
  source.pipe(_gzipSize.default.stream()).on('gzip-size', size => {
121
106
  resolve(utils.prettyBytes(size));
122
107
  });
123
108
  });
124
109
  };
110
+
125
111
  /**
126
112
  * Get the size in human readable.
127
113
  * From https://github.com/sindresorhus/pretty-bytes
128
114
  *
129
115
  * @returns {String}
130
116
  */
131
-
132
-
133
117
  utils.prettyBytes = num => {
134
118
  const UNITS = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
135
-
136
119
  if (!Number.isFinite(num)) {
137
120
  throw new TypeError(`Expected a finite number, got ${typeof num}: ${num}`);
138
121
  }
139
-
140
122
  const neg = num < 0;
141
-
142
123
  if (neg) {
143
124
  num = -num;
144
125
  }
145
-
146
126
  if (num < 1) {
147
127
  return (neg ? '-' : '') + num + ' B';
148
128
  }
149
-
150
129
  const exponent = Math.min(Math.floor(Math.log(num) / Math.log(1000)), UNITS.length - 1);
151
130
  const numStr = Number((num / Math.pow(1000, exponent)).toPrecision(3));
152
131
  const unit = UNITS[exponent];
153
132
  return (neg ? '-' : '') + numStr + ' ' + unit;
154
133
  };
134
+
155
135
  /**
156
136
  * Set the file name as minified.
157
137
  * eg. file.js returns file.min.js
@@ -162,30 +142,24 @@ utils.prettyBytes = num => {
162
142
  * @param {Boolean} replaceInPlace
163
143
  * @returns {String}
164
144
  */
165
-
166
-
167
145
  utils.setFileNameMin = (file, output, publicFolder, replaceInPlace) => {
168
146
  const filePath = file.substr(0, file.lastIndexOf('/') + 1);
169
147
  const fileWithoutPath = file.substr(file.lastIndexOf('/') + 1);
170
148
  let fileWithoutExtension = fileWithoutPath.substr(0, fileWithoutPath.lastIndexOf('.'));
171
-
172
149
  if (publicFolder) {
173
150
  fileWithoutExtension = publicFolder + fileWithoutExtension;
174
151
  }
175
-
176
152
  if (replaceInPlace) {
177
153
  fileWithoutExtension = filePath + fileWithoutExtension;
178
154
  }
179
-
180
155
  return output.replace('$1', fileWithoutExtension);
181
156
  };
157
+
182
158
  /**
183
159
  * Compress a single file.
184
160
  *
185
161
  * @param {Object} settings
186
162
  */
187
-
188
-
189
163
  utils.compressSingleFile = settings => {
190
164
  const content = settings.content ? settings.content : utils.getContentFromFiles(settings.input);
191
165
  return settings.sync ? utils.runSync({
@@ -196,21 +170,20 @@ utils.compressSingleFile = settings => {
196
170
  content
197
171
  });
198
172
  };
173
+
199
174
  /**
200
175
  * Concatenate all input files and get the data.
201
176
  *
202
177
  * @param {String|Array} input
203
178
  * @return {String}
204
179
  */
205
-
206
-
207
180
  utils.getContentFromFiles = input => {
208
181
  if (!Array.isArray(input)) {
209
182
  return _fs.default.readFileSync(input, 'utf8');
210
183
  }
211
-
212
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');
213
185
  };
186
+
214
187
  /**
215
188
  * Run compressor in sync.
216
189
  *
@@ -219,8 +192,6 @@ utils.getContentFromFiles = input => {
219
192
  * @param {Number} index - index of the file being processed
220
193
  * @return {String}
221
194
  */
222
-
223
-
224
195
  utils.runSync = ({
225
196
  settings,
226
197
  content,
@@ -231,6 +202,7 @@ utils.runSync = ({
231
202
  callback: null,
232
203
  index
233
204
  });
205
+
234
206
  /**
235
207
  * Run compressor in async.
236
208
  *
@@ -239,8 +211,6 @@ utils.runSync = ({
239
211
  * @param {Number} index - index of the file being processed
240
212
  * @return {Promise}
241
213
  */
242
-
243
-
244
214
  utils.runAsync = ({
245
215
  settings,
246
216
  content,
@@ -254,13 +224,13 @@ utils.runAsync = ({
254
224
  if (err) {
255
225
  return reject(err);
256
226
  }
257
-
258
227
  resolve(min);
259
228
  },
260
229
  index
261
230
  });
262
231
  });
263
232
  };
233
+
264
234
  /**
265
235
  * Expose `utils`.
266
236
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@node-minify/utils",
3
- "version": "7.0.0",
3
+ "version": "7.1.0",
4
4
  "description": "utils for @node-minify",
5
5
  "keywords": [
6
6
  "compressor",
@@ -34,5 +34,5 @@
34
34
  "dependencies": {
35
35
  "gzip-size": "6.0.0"
36
36
  },
37
- "gitHead": "8b5bda6f1ac9fe7180006f2a19ec3253e8fff4ec"
37
+ "gitHead": "94cef2d5d653c3bddc3e603b4e25c135b0b6f4b3"
38
38
  }