@pnpm/fs.packlist 1100.0.0 → 1100.0.1

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.
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  > Get a list of the files to add from a directory into an npm package
4
4
 
5
5
  <!--@shields('npm')-->
6
- [![npm version](https://img.shields.io/npm/v/packlist.svg)](https://www.npmjs.com/package/@pnpm/fs.packlist)
6
+ [![npm version](https://img.shields.io/npm/v/packlist.svg)](https://npmx.dev/package/@pnpm/fs.packlist)
7
7
  <!--/@-->
8
8
 
9
9
  ## Installation
package/lib/index.js CHANGED
@@ -1,21 +1,92 @@
1
- import fs from 'node:fs/promises';
1
+ import fs from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import util from 'node:util';
4
4
  import npmPacklist from 'npm-packlist';
5
5
  export async function packlist(pkgDir, opts) {
6
- const pkg = opts?.manifest ?? await readPackageJson(pkgDir);
7
- const tree = {
6
+ const pkg = opts?.manifest ?? readPackageJson(pkgDir);
7
+ const tree = buildRootTree(pkgDir, pkg);
8
+ const files = await npmPacklist(tree);
9
+ return files.map((file) => file.replace(/^\.[/\\]/, ''));
10
+ }
11
+ function buildRootTree(pkgDir, pkg) {
12
+ const bundledDeps = getRootBundledDeps(pkg);
13
+ // npm-packlist's gatherBundles() iterates package.bundleDependencies directly,
14
+ // so the field must be an array. Normalize true/undefined to an explicit list.
15
+ const normalizedPkg = normalizePackage(pkg);
16
+ normalizedPkg.bundleDependencies = bundledDeps;
17
+ delete normalizedPkg.bundledDependencies;
18
+ const root = makeNode(pkgDir, normalizedPkg, true);
19
+ const seen = new Map([[pkgDir, root]]);
20
+ populateEdges(root, bundledDeps, seen);
21
+ return root;
22
+ }
23
+ function buildBundledTree(pkgDir, seen) {
24
+ const cached = seen.get(pkgDir);
25
+ if (cached)
26
+ return cached;
27
+ const pkg = readPackageJson(pkgDir);
28
+ const node = makeNode(pkgDir, normalizePackage(pkg), false);
29
+ seen.set(pkgDir, node);
30
+ populateEdges(node, getNestedBundledDeps(pkg), seen);
31
+ return node;
32
+ }
33
+ function populateEdges(node, deps, seen) {
34
+ for (const dep of deps) {
35
+ const depDir = resolveDependency(dep, node.path);
36
+ if (!depDir)
37
+ continue;
38
+ const depNode = buildBundledTree(depDir, seen);
39
+ node.edgesOut.set(dep, { to: depNode, peer: false, dev: false });
40
+ }
41
+ }
42
+ function makeNode(pkgDir, pkg, isProjectRoot) {
43
+ const node = {
8
44
  path: pkgDir,
9
- package: normalizePackage(pkg),
10
- isProjectRoot: true,
45
+ package: pkg,
46
+ isProjectRoot,
47
+ isLink: false,
11
48
  edgesOut: new Map(),
12
49
  };
13
- const files = await npmPacklist(tree);
14
- return files.map((file) => file.replace(/^\.[/\\]/, ''));
50
+ node.target = node;
51
+ return node;
52
+ }
53
+ function getRootBundledDeps(pkg) {
54
+ const bundle = pkg.bundleDependencies ?? pkg.bundledDependencies;
55
+ if (Array.isArray(bundle))
56
+ return bundle;
57
+ if (bundle === true) {
58
+ return Object.keys((pkg.dependencies ?? {}));
59
+ }
60
+ return [];
61
+ }
62
+ function getNestedBundledDeps(pkg) {
63
+ const dependencies = (pkg.dependencies ?? {});
64
+ const optionalDependencies = (pkg.optionalDependencies ?? {});
65
+ return [...Object.keys(dependencies), ...Object.keys(optionalDependencies)];
66
+ }
67
+ function resolveDependency(depName, fromDir) {
68
+ let currentDir = fromDir;
69
+ while (true) {
70
+ const candidate = path.join(currentDir, 'node_modules', depName);
71
+ try {
72
+ const stat = fs.statSync(path.join(candidate, 'package.json'));
73
+ if (stat.isFile())
74
+ return candidate;
75
+ }
76
+ catch (err) {
77
+ if (!util.types.isNativeError(err) || !('code' in err) || err.code !== 'ENOENT') {
78
+ throw err;
79
+ }
80
+ }
81
+ const parent = path.dirname(currentDir);
82
+ if (parent === currentDir)
83
+ return undefined;
84
+ currentDir = parent;
85
+ }
15
86
  }
16
- async function readPackageJson(dir) {
87
+ function readPackageJson(dir) {
17
88
  try {
18
- return JSON.parse(await fs.readFile(path.join(dir, 'package.json'), 'utf8'));
89
+ return JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'));
19
90
  }
20
91
  catch (err) {
21
92
  if (util.types.isNativeError(err) && 'code' in err && err.code === 'ENOENT') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pnpm/fs.packlist",
3
- "version": "1100.0.0",
3
+ "version": "1100.0.1",
4
4
  "description": "Get a list of the files to add from a directory into an npm package",
5
5
  "keywords": [
6
6
  "pnpm",
@@ -27,7 +27,7 @@
27
27
  "npm-packlist": "10.0.4"
28
28
  },
29
29
  "devDependencies": {
30
- "@pnpm/fs.packlist": "1100.0.0"
30
+ "@pnpm/fs.packlist": "1100.0.1"
31
31
  },
32
32
  "engines": {
33
33
  "node": ">=22.13"