@e-mc/module 0.9.21 → 0.9.23

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 +6 -6
  2. package/index.js +45 -63
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -9,7 +9,7 @@
9
9
 
10
10
  ## Interface
11
11
 
12
- * [View Source](https://www.unpkg.com/@e-mc/types@0.9.21/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.9.23/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { LogStatus } from "./squared";
@@ -394,11 +394,11 @@ type ForegroundColor = typeof IForegroundColor | `#${string}`;
394
394
 
395
395
  ## References
396
396
 
397
- - https://www.unpkg.com/@e-mc/types@0.9.21/lib/core.d.ts
398
- - https://www.unpkg.com/@e-mc/types@0.9.21/lib/logger.d.ts
399
- - https://www.unpkg.com/@e-mc/types@0.9.21/lib/module.d.ts
400
- - https://www.unpkg.com/@e-mc/types@0.9.21/lib/node.d.ts
401
- - https://www.unpkg.com/@e-mc/types@0.9.21/lib/settings.d.ts
397
+ - https://www.unpkg.com/@e-mc/types@0.9.23/lib/core.d.ts
398
+ - https://www.unpkg.com/@e-mc/types@0.9.23/lib/logger.d.ts
399
+ - https://www.unpkg.com/@e-mc/types@0.9.23/lib/module.d.ts
400
+ - https://www.unpkg.com/@e-mc/types@0.9.23/lib/node.d.ts
401
+ - https://www.unpkg.com/@e-mc/types@0.9.23/lib/settings.d.ts
402
402
 
403
403
  * https://www.npmjs.com/package/@types/node
404
404
 
package/index.js CHANGED
@@ -328,14 +328,14 @@ function tryCreateDir(value) {
328
328
  }
329
329
  return false;
330
330
  }
331
- function tryRemoveDir(value, empty, recursive) {
331
+ function tryRemoveDir(srcDir, empty, recursive) {
332
332
  const temp = typeof empty === 'number';
333
333
  let options;
334
334
  if (!temp) {
335
335
  try {
336
- fs.rmSync(value, { recursive, force: true });
336
+ fs.rmSync(srcDir, { recursive, force: true });
337
337
  if (empty) {
338
- fs.mkdirSync(value);
338
+ fs.mkdirSync(srcDir);
339
339
  }
340
340
  return [];
341
341
  }
@@ -343,30 +343,11 @@ function tryRemoveDir(value, empty, recursive) {
343
343
  }
344
344
  options = { recursive };
345
345
  }
346
- value = Module.normalizePath(value, 1);
347
346
  const failed = [];
348
- const nameMap = new WeakMap();
349
- let files;
350
- if (temp) {
351
- files = [];
352
- fs.readdirSync(value).forEach(name => {
353
- try {
354
- const stat = fs.statSync(value + name);
355
- nameMap.set(stat, name);
356
- files.push(stat);
357
- }
358
- catch {
359
- failed.push(value + name);
360
- }
361
- });
362
- }
363
- else {
364
- files = fs.readdirSync(value, { withFileTypes: true });
365
- }
366
347
  const current = Date.now();
367
- for (let i = 0, length = files.length; i < length; ++i) {
368
- const file = files[i];
369
- const pathname = value + (file.name || nameMap.get(file));
348
+ let entry;
349
+ fs.readdirSync(srcDir, { withFileTypes: true }).forEach(file => {
350
+ const pathname = path.join(srcDir, file.name);
370
351
  try {
371
352
  if (file.isDirectory()) {
372
353
  if (recursive) {
@@ -378,16 +359,16 @@ function tryRemoveDir(value, empty, recursive) {
378
359
  }
379
360
  }
380
361
  }
381
- else if (!temp || file.ctimeMs + empty <= current) {
362
+ else if (!temp || (entry = fs.statSync(pathname)) && entry.ctimeMs + empty <= current) {
382
363
  fs.unlinkSync(pathname);
383
364
  }
384
365
  }
385
366
  catch {
386
367
  failed.push(pathname);
387
368
  }
388
- }
369
+ });
389
370
  if (!empty && failed.length === 0) {
390
- fs.rmdirSync(value);
371
+ fs.rmdirSync(srcDir);
391
372
  }
392
373
  return failed;
393
374
  }
@@ -744,7 +725,7 @@ class Module extends EventEmitter {
744
725
  this[_f] = new AbortController();
745
726
  this[_g] = null;
746
727
  }
747
- static get VERSION() { return "0.9.21"; }
728
+ static get VERSION() { return "0.9.23"; }
748
729
  static get LOG_TYPE() { return types_1.LOG_TYPE; }
749
730
  static get STATUS_TYPE() { return types_1.STATUS_TYPE; }
750
731
  static get MAX_TIMEOUT() { return 2147483647; }
@@ -1263,35 +1244,35 @@ class Module extends EventEmitter {
1263
1244
  return cacheKey ? (0, types_1.generateUUID)() : '';
1264
1245
  }
1265
1246
  static asHash(data, algorithm, digest) {
1266
- if (!algorithm && !digest) {
1267
- return crypto.createHash("sha256").update(data).digest("hex");
1268
- }
1269
- let options;
1270
- if ((0, types_1.isObject)(algorithm)) {
1271
- options = { ...algorithm };
1272
- if ('algorithm' in options) {
1273
- algorithm = options.algorithm;
1274
- delete options.algorithm;
1247
+ try {
1248
+ if (!algorithm && !digest) {
1249
+ return crypto.createHash("sha256").update(data).digest("hex");
1275
1250
  }
1276
- else {
1277
- algorithm = undefined;
1251
+ let options;
1252
+ if ((0, types_1.isObject)(algorithm)) {
1253
+ options = { ...algorithm };
1254
+ if ('algorithm' in options) {
1255
+ algorithm = options.algorithm;
1256
+ delete options.algorithm;
1257
+ }
1258
+ else {
1259
+ algorithm = undefined;
1260
+ }
1261
+ if ('digest' in options) {
1262
+ digest = options.digest;
1263
+ delete options.digest;
1264
+ }
1265
+ else {
1266
+ digest = undefined;
1267
+ }
1278
1268
  }
1279
- if ('digest' in options) {
1280
- digest = options.digest;
1281
- delete options.digest;
1269
+ else if ((0, types_1.isObject)(digest)) {
1270
+ options = digest;
1271
+ digest = undefined;
1282
1272
  }
1283
- else {
1273
+ else if (typeof digest !== 'string') {
1284
1274
  digest = undefined;
1285
1275
  }
1286
- }
1287
- else if ((0, types_1.isObject)(digest)) {
1288
- options = digest;
1289
- digest = undefined;
1290
- }
1291
- else if (typeof digest !== 'string') {
1292
- digest = undefined;
1293
- }
1294
- try {
1295
1276
  return crypto.createHash(algorithm || "sha256", options).update(data).digest(digest || "hex");
1296
1277
  }
1297
1278
  catch {
@@ -1368,8 +1349,7 @@ class Module extends EventEmitter {
1368
1349
  }
1369
1350
  static isFile(value, type) {
1370
1351
  if (!type) {
1371
- value = asFile(value);
1372
- return !!value && this.isPath(value, true);
1352
+ return this.isPath(value = asFile(value), true);
1373
1353
  }
1374
1354
  if (typeof value === 'string') {
1375
1355
  switch (type) {
@@ -1483,11 +1463,13 @@ class Module extends EventEmitter {
1483
1463
  if (typeof values[values.length - 1] === 'boolean') {
1484
1464
  normalize = values.pop();
1485
1465
  }
1486
- const paths = values.map(item => typeof item === 'string' ? item.trim() : '').filter(item => item).map(value => this.toPosix(value, normalize));
1466
+ const paths = values.filter(item => (0, types_1.isString)(item)).map(value => this.toPosix(value, normalize));
1487
1467
  let result = paths[0] || '';
1488
1468
  for (let i = 1; i < paths.length; ++i) {
1489
1469
  const trailing = paths[i];
1490
- result += (!trailing.startsWith('/') && !result.endsWith('/') ? '/' : '') + trailing;
1470
+ if (trailing) {
1471
+ result += (!trailing.startsWith('/') && !result.endsWith('/') ? '/' : '') + trailing;
1472
+ }
1491
1473
  }
1492
1474
  return result;
1493
1475
  }
@@ -1584,11 +1566,11 @@ class Module extends EventEmitter {
1584
1566
  }
1585
1567
  static async copyDir(src, dest, move, recursive = true) {
1586
1568
  const srcOut = sanitizePath(asFile(src));
1587
- if (!srcOut || !this.isDir(srcOut)) {
1569
+ if (!this.isDir(srcOut)) {
1588
1570
  return Promise.reject(errorDirectory(asFile(src) || "Unknown"));
1589
1571
  }
1590
1572
  const destOut = sanitizePath(asFile(dest));
1591
- if (!destOut || !this.createDir(destOut)) {
1573
+ if (!this.createDir(destOut)) {
1592
1574
  return Promise.reject(errorDirectory(asFile(dest) || "Unknown"));
1593
1575
  }
1594
1576
  let symFile, symDir, ignoreFile, ignoreDir, silent, overwrite;
@@ -1713,7 +1695,7 @@ class Module extends EventEmitter {
1713
1695
  }
1714
1696
  static async globDir(src, pattern, options) {
1715
1697
  const outDir = sanitizePath(asFile(src));
1716
- if (!(outDir && this.isDir(outDir))) {
1698
+ if (!this.isDir(outDir)) {
1717
1699
  return Promise.reject(errorDirectory(asFile(src) || "Unknown"));
1718
1700
  }
1719
1701
  const pmOpts = { posixSlashes: true, windows: true, nocase: PLATFORM_WIN32 };
@@ -1786,7 +1768,7 @@ class Module extends EventEmitter {
1786
1768
  ({ minStreamSize = 0, encoding, signal, cache } = options);
1787
1769
  }
1788
1770
  const src = sanitizePath(asFile(value));
1789
- if (src && this.isPath(src, true)) {
1771
+ if (this.isPath(src, true)) {
1790
1772
  return new Promise(async (resolve) => {
1791
1773
  const fileSize = fs.statSync(src).size;
1792
1774
  let data;
@@ -1823,7 +1805,7 @@ class Module extends EventEmitter {
1823
1805
  ({ minStreamSize, encoding, cache } = options);
1824
1806
  }
1825
1807
  const src = sanitizePath(asFile(value));
1826
- if (src && this.isPath(src, true)) {
1808
+ if (this.isPath(src, true)) {
1827
1809
  let result;
1828
1810
  if (cache && (result = getCacheItem(CACHE_READTEXT, src))) {
1829
1811
  return (minStreamSize !== undefined ? Promise.resolve(result) : result);
@@ -1852,7 +1834,7 @@ class Module extends EventEmitter {
1852
1834
  ({ minStreamSize, cache } = options);
1853
1835
  }
1854
1836
  const src = sanitizePath(asFile(value));
1855
- if (src && this.isPath(src, true)) {
1837
+ if (this.isPath(src, true)) {
1856
1838
  let result;
1857
1839
  if (cache && (result = getCacheItem(CACHE_READBUFFER, src))) {
1858
1840
  return (minStreamSize !== undefined ? Promise.resolve(result) : result);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/module",
3
- "version": "0.9.21",
3
+ "version": "0.9.23",
4
4
  "description": "Module base class for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -20,7 +20,7 @@
20
20
  "license": "MIT",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/types": "0.9.21",
23
+ "@e-mc/types": "0.9.23",
24
24
  "chalk": "4.1.2",
25
25
  "file-type": "16.5.4",
26
26
  "js-yaml": "^4.1.0",