node-modules-tools 0.0.0 → 0.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.
@@ -34,8 +34,6 @@ async function listPackageDependencies(options) {
34
34
  return node;
35
35
  }
36
36
  function traverse(_node, level, mode, directImporter, nestedImporter) {
37
- if (_node.from.startsWith("@types"))
38
- return;
39
37
  const node = normalize(_node);
40
38
  if (directImporter)
41
39
  node.dependents.add(directImporter);
@@ -74,6 +72,8 @@ async function listPackageDependencies(options) {
74
72
  }
75
73
  }
76
74
  return {
75
+ cwd: options.cwd,
76
+ packageManager: "pnpm",
77
77
  packages
78
78
  };
79
79
  }
package/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  type PackageModuleTypeSimple = 'cjs' | 'esm';
2
- type PackageModuleType = 'cjs' | 'esm' | 'dual' | 'faux';
2
+ type PackageModuleType = 'cjs' | 'esm' | 'dual' | 'faux' | 'none';
3
3
  interface ListPackageDependenciesOptions {
4
4
  /**
5
5
  * Current working directory
@@ -19,6 +19,8 @@ interface ListPackageDependenciesOptions {
19
19
  traverseFilter?: (node: PackageNode) => boolean;
20
20
  }
21
21
  interface ListPackageDependenciesResult {
22
+ cwd: string;
23
+ packageManager: string;
22
24
  packages: PackageNode[];
23
25
  }
24
26
  interface PackageNode {
@@ -48,22 +50,28 @@ interface PackageNode {
48
50
  optional: boolean;
49
51
  }
50
52
  interface ResolvedPackageNode extends PackageNode {
51
- module: PackageModuleType;
53
+ resolved: {
54
+ module: PackageModuleType;
55
+ license?: string;
56
+ author?: string;
57
+ repository?: string;
58
+ engines?: Record<string, string>;
59
+ };
52
60
  }
53
61
 
54
62
  /**
55
- * Analyze a package node, and return a resolved package node.
56
- * This function mutates the input package node.
63
+ * List dependencies of packages in the current project.
57
64
  *
58
- * - Set `module` to the resolved module type (cjs, esm, dual, faux).
65
+ * This function will automatically detect the package manager in the current project, and list the dependencies of the packages.
59
66
  */
60
- declare function analyzePackage(pkg: PackageNode): Promise<ResolvedPackageNode>;
67
+ declare function listPackageDependencies(options: ListPackageDependenciesOptions): Promise<ListPackageDependenciesResult>;
61
68
 
62
69
  /**
63
- * List dependencies of packages in the current project.
70
+ * Analyze a package node, and return a resolved package node.
71
+ * This function mutates the input package node.
64
72
  *
65
- * This function will automatically detect the package manager in the current project, and list the dependencies of the packages.
73
+ * - Set `module` to the resolved module type (cjs, esm, dual, faux, none).
66
74
  */
67
- declare function listPackageDependencies(options: ListPackageDependenciesOptions): Promise<ListPackageDependenciesResult>;
75
+ declare function resolvePackage(pkg: PackageNode): Promise<ResolvedPackageNode>;
68
76
 
69
- export { type ListPackageDependenciesOptions, type ListPackageDependenciesResult, type PackageModuleType, type PackageModuleTypeSimple, type PackageNode, type ResolvedPackageNode, analyzePackage, listPackageDependencies };
77
+ export { type ListPackageDependenciesOptions, type ListPackageDependenciesResult, type PackageModuleType, type PackageModuleTypeSimple, type PackageNode, type ResolvedPackageNode, listPackageDependencies, resolvePackage };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  type PackageModuleTypeSimple = 'cjs' | 'esm';
2
- type PackageModuleType = 'cjs' | 'esm' | 'dual' | 'faux';
2
+ type PackageModuleType = 'cjs' | 'esm' | 'dual' | 'faux' | 'none';
3
3
  interface ListPackageDependenciesOptions {
4
4
  /**
5
5
  * Current working directory
@@ -19,6 +19,8 @@ interface ListPackageDependenciesOptions {
19
19
  traverseFilter?: (node: PackageNode) => boolean;
20
20
  }
21
21
  interface ListPackageDependenciesResult {
22
+ cwd: string;
23
+ packageManager: string;
22
24
  packages: PackageNode[];
23
25
  }
24
26
  interface PackageNode {
@@ -48,22 +50,28 @@ interface PackageNode {
48
50
  optional: boolean;
49
51
  }
50
52
  interface ResolvedPackageNode extends PackageNode {
51
- module: PackageModuleType;
53
+ resolved: {
54
+ module: PackageModuleType;
55
+ license?: string;
56
+ author?: string;
57
+ repository?: string;
58
+ engines?: Record<string, string>;
59
+ };
52
60
  }
53
61
 
54
62
  /**
55
- * Analyze a package node, and return a resolved package node.
56
- * This function mutates the input package node.
63
+ * List dependencies of packages in the current project.
57
64
  *
58
- * - Set `module` to the resolved module type (cjs, esm, dual, faux).
65
+ * This function will automatically detect the package manager in the current project, and list the dependencies of the packages.
59
66
  */
60
- declare function analyzePackage(pkg: PackageNode): Promise<ResolvedPackageNode>;
67
+ declare function listPackageDependencies(options: ListPackageDependenciesOptions): Promise<ListPackageDependenciesResult>;
61
68
 
62
69
  /**
63
- * List dependencies of packages in the current project.
70
+ * Analyze a package node, and return a resolved package node.
71
+ * This function mutates the input package node.
64
72
  *
65
- * This function will automatically detect the package manager in the current project, and list the dependencies of the packages.
73
+ * - Set `module` to the resolved module type (cjs, esm, dual, faux, none).
66
74
  */
67
- declare function listPackageDependencies(options: ListPackageDependenciesOptions): Promise<ListPackageDependenciesResult>;
75
+ declare function resolvePackage(pkg: PackageNode): Promise<ResolvedPackageNode>;
68
76
 
69
- export { type ListPackageDependenciesOptions, type ListPackageDependenciesResult, type PackageModuleType, type PackageModuleTypeSimple, type PackageNode, type ResolvedPackageNode, analyzePackage, listPackageDependencies };
77
+ export { type ListPackageDependenciesOptions, type ListPackageDependenciesResult, type PackageModuleType, type PackageModuleTypeSimple, type PackageNode, type ResolvedPackageNode, listPackageDependencies, resolvePackage };
package/dist/index.mjs CHANGED
@@ -1,8 +1,20 @@
1
+ import { detect } from 'package-manager-detector';
1
2
  import fs from 'node:fs/promises';
2
3
  import { join } from 'node:path';
3
- import { detect } from 'package-manager-detector';
4
4
 
5
- function analyzePackageJson(pkgJson) {
5
+ async function listPackageDependencies(options) {
6
+ const manager = await detect({
7
+ cwd: options.cwd
8
+ });
9
+ if (!manager)
10
+ throw new Error("Cannot detect package manager in the current patch");
11
+ if (manager.name === "pnpm")
12
+ return await import('./chunks/pnpm.mjs').then((r) => r.listPackageDependencies(options));
13
+ else
14
+ throw new Error(`Package manager ${manager.name} is not yet supported`);
15
+ }
16
+
17
+ function analyzePackageModuleType(pkgJson) {
6
18
  const { exports, main, type } = pkgJson;
7
19
  let cjs;
8
20
  let esm;
@@ -30,12 +42,19 @@ function analyzePackageJson(pkgJson) {
30
42
  if (cjs === undefined && esm === undefined) {
31
43
  if (type === "module" || main && /\.mjs$/.test(main)) {
32
44
  esm = true;
33
- } else {
45
+ } else if (main) {
34
46
  cjs = true;
35
47
  }
36
48
  }
37
- const style = esm && cjs ? "dual" : esm ? "esm" : fauxEsm ? "faux" : "cjs";
38
- return style;
49
+ if (esm && cjs)
50
+ return "dual";
51
+ if (esm)
52
+ return "esm";
53
+ if (fauxEsm)
54
+ return "faux";
55
+ if (!esm && !cjs)
56
+ return "none";
57
+ return "cjs";
39
58
  function analyzeThing(value, path) {
40
59
  if (value && typeof value === "object") {
41
60
  if (Array.isArray(value)) {
@@ -100,26 +119,20 @@ function stripBomTag(content) {
100
119
  return content;
101
120
  }
102
121
 
103
- async function analyzePackage(pkg) {
122
+ async function resolvePackage(pkg) {
104
123
  const _pkg = pkg;
105
- if (_pkg.module)
124
+ if (_pkg.resolved)
106
125
  return _pkg;
107
126
  const content = await fs.readFile(join(pkg.path, "package.json"), "utf-8");
108
127
  const json = JSON.parse(stripBomTag(content));
109
- _pkg.module = analyzePackageJson(json);
128
+ _pkg.resolved = {
129
+ module: analyzePackageModuleType(json),
130
+ engines: json.engines,
131
+ license: json.license,
132
+ author: typeof json.author === "string" ? json.author : json.author?.url,
133
+ repository: typeof json.repository === "string" ? json.repository : json.repository?.url
134
+ };
110
135
  return _pkg;
111
136
  }
112
137
 
113
- async function listPackageDependencies(options) {
114
- const manager = await detect({
115
- cwd: options.cwd
116
- });
117
- if (!manager)
118
- throw new Error("Cannot detect package manager in the current patch");
119
- if (manager.name === "pnpm")
120
- return await import('./chunks/pnpm.mjs').then((r) => r.listPackageDependencies(options));
121
- else
122
- throw new Error(`Package manager ${manager.name} is not yet supported`);
123
- }
124
-
125
- export { analyzePackage, listPackageDependencies };
138
+ export { listPackageDependencies, resolvePackage };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "node-modules-tools",
3
3
  "type": "module",
4
- "version": "0.0.0",
4
+ "version": "0.0.1",
5
5
  "description": "Tools for inspecting node_modules",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -44,7 +44,7 @@
44
44
  },
45
45
  "scripts": {
46
46
  "build": "unbuild",
47
- "dev": "unbuild --stub",
47
+ "stub": "unbuild --stub",
48
48
  "start": "tsx src/index.ts",
49
49
  "test": "vitest"
50
50
  }