pkgprn 0.5.4 → 0.6.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/package.json +1 -1
  2. package/prune.js +30 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pkgprn",
3
- "version": "0.5.4",
3
+ "version": "0.6.0",
4
4
  "license": "MIT",
5
5
  "author": "Konstantin Shutkin",
6
6
  "bin": "./index.js",
package/prune.js CHANGED
@@ -1,4 +1,4 @@
1
- import { access, mkdir, readdir, readFile, rename, rm, stat, writeFile } from 'node:fs/promises';
1
+ import { access, glob as fsGlob, mkdir, readdir, readFile, rename, rm, stat, writeFile } from 'node:fs/promises';
2
2
  import path from 'node:path';
3
3
 
4
4
  import { extractReferences } from './extract-references.js';
@@ -46,12 +46,20 @@ export async function prunePkg(pkg, options, logger) {
46
46
  }
47
47
  }
48
48
 
49
+ if (pkg.files && Array.isArray(pkg.files)) {
50
+ pkg.files = await expandFileGlobs(pkg.files);
51
+ }
52
+
49
53
  if (options.cleanupFiles) {
50
54
  await removeJunkFiles('.');
51
55
  } else if (options.flatten) {
52
56
  logger('cleanup is disabled, junk files may cause flatten to fail', 2);
53
57
  }
54
58
 
59
+ if (pkg.files && Array.isArray(pkg.files) && pkg.files.length > 0 && options.cleanupFiles) {
60
+ await cleanupDir(pkg, logger);
61
+ }
62
+
55
63
  if (options.flatten) {
56
64
  await flatten(pkg, options.flatten, logger, options.removeSourcemaps);
57
65
  }
@@ -209,8 +217,8 @@ export async function prunePkg(pkg, options, logger) {
209
217
  }
210
218
  }
211
219
 
212
- if (pkg.files && Array.isArray(pkg.files) && options.cleanupFiles) {
213
- await cleanupDir(pkg, logger);
220
+ if (options.cleanupFiles) {
221
+ pkg.files = undefined;
214
222
  }
215
223
  }
216
224
 
@@ -590,6 +598,25 @@ function isAlwaysIncludedByBasename(file) {
590
598
  return alwaysIncludedBasenames.includes(basenameWithoutExtension);
591
599
  }
592
600
 
601
+ function isGlobPattern(str) {
602
+ return /[*?[\]{}]/.test(str);
603
+ }
604
+
605
+ async function expandFileGlobs(files) {
606
+
607
+ const result = [];
608
+ for (const entry of files) {
609
+ if (isGlobPattern(entry)) {
610
+ for await (const match of fsGlob(entry)) {
611
+ result.push(normalizePath(match));
612
+ }
613
+ } else {
614
+ result.push(entry);
615
+ }
616
+ }
617
+ return [...new Set(result)];
618
+ }
619
+
593
620
  async function cleanupDir(pkg, logger) {
594
621
  logger.update('cleaning up files...');
595
622
 
@@ -625,8 +652,6 @@ async function cleanupDir(pkg, logger) {
625
652
 
626
653
  await rm(entry, { recursive: true, force: true });
627
654
  }
628
-
629
- pkg.files = undefined;
630
655
  }
631
656
 
632
657
  async function cleanupSubDir(dir, filesEntries, alwaysIncludedFiles) {