@efraindrummer/ghostdeps 0.3.0 → 0.5.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.
@@ -9,7 +9,7 @@
9
9
  import { readFile } from 'node:fs/promises';
10
10
  import path from 'node:path';
11
11
  import { ManifestError } from '../errors.js';
12
- export const LOCKFILE_NAMES = ['npm-shrinkwrap.json', 'package-lock.json', 'yarn.lock'];
12
+ export const LOCKFILE_NAMES = ['npm-shrinkwrap.json', 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml'];
13
13
  function unquote(s) {
14
14
  const t = s.trim();
15
15
  if ((t.startsWith('"') && t.endsWith('"')) || (t.startsWith("'") && t.endsWith("'"))) {
@@ -49,13 +49,22 @@ export function parseYarnLock(content) {
49
49
  const trimmed = rawLine.trim();
50
50
  if (!trimmed || trimmed.startsWith('#'))
51
51
  continue;
52
- // Header de nivel superior (ej. "foo@^1.0.0", "foo@^1.2.0":)
52
+ // Header de nivel superior (ej. "foo@^1.0.0", "foo@^1.2.0": o Yarn Berry "foo@npm:^1.0.0":)
53
53
  if (!rawLine.startsWith(' ') && trimmed.endsWith(':')) {
54
54
  flush();
55
55
  const headerStr = trimmed.slice(0, -1);
56
- const parts = headerStr.split(',').map((p) => unquote(p));
57
- currentHeaders = parts;
58
- const first = parts[0] ?? '';
56
+ if (headerStr.startsWith('__metadata'))
57
+ continue;
58
+ const rawParts = headerStr.split(',').map((p) => unquote(p.trim()));
59
+ const allHeaders = [];
60
+ for (const p of rawParts) {
61
+ allHeaders.push(p);
62
+ if (p.includes('@npm:')) {
63
+ allHeaders.push(p.replace(/@npm:/g, '@'));
64
+ }
65
+ }
66
+ currentHeaders = allHeaders;
67
+ const first = (rawParts[0] ?? '').replace(/@npm:/g, '@');
59
68
  const at = first.lastIndexOf('@');
60
69
  const name = at > 0 ? first.slice(0, at) : first;
61
70
  currentPkg = { name, dependencies: {}, optionalDependencies: {} };
@@ -66,11 +75,11 @@ export function parseYarnLock(content) {
66
75
  // Propiedades directas (2 espacios de indentación)
67
76
  if (rawLine.startsWith(' ') && !rawLine.startsWith(' ')) {
68
77
  section = 'none';
69
- if (trimmed.startsWith('version ')) {
70
- currentPkg.version = unquote(trimmed.slice(8));
78
+ if (trimmed.startsWith('version ') || trimmed.startsWith('version:')) {
79
+ currentPkg.version = unquote(trimmed.replace(/^version:?\s*/, ''));
71
80
  }
72
- else if (trimmed.startsWith('resolved ')) {
73
- currentPkg.resolved = unquote(trimmed.slice(9));
81
+ else if (trimmed.startsWith('resolved ') || trimmed.startsWith('resolved:') || trimmed.startsWith('resolution:')) {
82
+ currentPkg.resolved = unquote(trimmed.replace(/^(resolved|resolution):?\s*/, ''));
74
83
  }
75
84
  else if (trimmed === 'dependencies:') {
76
85
  section = 'dependencies';
@@ -82,10 +91,25 @@ export function parseYarnLock(content) {
82
91
  }
83
92
  // Sub-claves de dependencias (4 espacios de indentación)
84
93
  if (rawLine.startsWith(' ') && (section === 'dependencies' || section === 'optionalDependencies')) {
85
- const parts = trimmed.split(/\s+/);
86
- if (parts.length >= 2) {
87
- const depName = unquote(parts[0] ?? '');
88
- const depSpec = unquote(parts.slice(1).join(' '));
94
+ let depName = '';
95
+ let depSpec = '';
96
+ if (trimmed.includes(':')) {
97
+ // Yarn Berry format (name: spec)
98
+ const colon = trimmed.indexOf(':');
99
+ depName = unquote(trimmed.slice(0, colon).trim());
100
+ depSpec = unquote(trimmed.slice(colon + 1).trim());
101
+ if (depSpec.startsWith('npm:'))
102
+ depSpec = depSpec.slice(4);
103
+ }
104
+ else {
105
+ // Yarn v1 format (name spec)
106
+ const parts = trimmed.split(/\s+/);
107
+ if (parts.length >= 2) {
108
+ depName = unquote(parts[0] ?? '');
109
+ depSpec = unquote(parts.slice(1).join(' '));
110
+ }
111
+ }
112
+ if (depName && depSpec) {
89
113
  if (section === 'dependencies') {
90
114
  currentPkg.dependencies[depName] = depSpec;
91
115
  }
@@ -98,6 +122,89 @@ export function parseYarnLock(content) {
98
122
  flush();
99
123
  return { type: 'yarn', entries };
100
124
  }
125
+ /**
126
+ * Parser ligero y seguro para pnpm-lock.yaml (v5/v6/v9).
127
+ */
128
+ export function parsePnpmLock(content) {
129
+ const packages = new Map();
130
+ const lines = content.split(/\r?\n/);
131
+ let inPackages = false;
132
+ let currentKey = null;
133
+ let currentPkg = null;
134
+ let subSection = 'none';
135
+ const flush = () => {
136
+ if (currentKey && currentPkg && currentPkg.name && currentPkg.version) {
137
+ packages.set(currentKey, {
138
+ name: currentPkg.name,
139
+ version: currentPkg.version,
140
+ dependencies: currentPkg.dependencies ?? {},
141
+ optionalDependencies: currentPkg.optionalDependencies ?? {},
142
+ });
143
+ }
144
+ currentKey = null;
145
+ currentPkg = null;
146
+ subSection = 'none';
147
+ };
148
+ for (let i = 0; i < lines.length; i++) {
149
+ const rawLine = lines[i] ?? '';
150
+ const trimmed = rawLine.trim();
151
+ if (!trimmed || trimmed.startsWith('#'))
152
+ continue;
153
+ if (rawLine.startsWith('packages:')) {
154
+ inPackages = true;
155
+ continue;
156
+ }
157
+ if (!inPackages)
158
+ continue;
159
+ if (!rawLine.startsWith(' ') && trimmed.endsWith(':')) {
160
+ flush();
161
+ inPackages = false;
162
+ continue;
163
+ }
164
+ if (rawLine.startsWith(' ') && !rawLine.startsWith(' ') && trimmed.endsWith(':')) {
165
+ flush();
166
+ let key = trimmed.slice(0, -1).trim();
167
+ key = unquote(key).replace(/^\//, '').replace(/\(.*$/, '');
168
+ const lastAt = key.lastIndexOf('@');
169
+ if (lastAt > 0) {
170
+ const name = key.slice(0, lastAt);
171
+ const version = key.slice(lastAt + 1);
172
+ currentKey = `${name}@${version}`;
173
+ currentPkg = { name, version, dependencies: {}, optionalDependencies: {} };
174
+ }
175
+ continue;
176
+ }
177
+ if (!currentPkg)
178
+ continue;
179
+ if (rawLine.startsWith(' ') && !rawLine.startsWith(' ')) {
180
+ if (trimmed === 'dependencies:') {
181
+ subSection = 'dependencies';
182
+ }
183
+ else if (trimmed === 'optionalDependencies:') {
184
+ subSection = 'optionalDependencies';
185
+ }
186
+ else {
187
+ subSection = 'none';
188
+ }
189
+ continue;
190
+ }
191
+ if (rawLine.startsWith(' ') && (subSection === 'dependencies' || subSection === 'optionalDependencies')) {
192
+ const colonIdx = trimmed.indexOf(':');
193
+ if (colonIdx > 0) {
194
+ const depName = unquote(trimmed.slice(0, colonIdx));
195
+ const depVersion = unquote(trimmed.slice(colonIdx + 1));
196
+ if (subSection === 'dependencies') {
197
+ currentPkg.dependencies[depName] = depVersion;
198
+ }
199
+ else {
200
+ currentPkg.optionalDependencies[depName] = depVersion;
201
+ }
202
+ }
203
+ }
204
+ }
205
+ flush();
206
+ return { type: 'pnpm', packages };
207
+ }
101
208
  export async function readLockfile(dir) {
102
209
  for (const name of LOCKFILE_NAMES) {
103
210
  const file = path.resolve(dir, name);
@@ -119,6 +226,15 @@ export async function readLockfile(dir) {
119
226
  throw new ManifestError(`Error al procesar ${file}: ${cause.message}`, { cause });
120
227
  }
121
228
  }
229
+ if (name === 'pnpm-lock.yaml') {
230
+ try {
231
+ const lock = parsePnpmLock(content);
232
+ return { file, lock };
233
+ }
234
+ catch (cause) {
235
+ throw new ManifestError(`Error al procesar ${file}: ${cause.message}`, { cause });
236
+ }
237
+ }
122
238
  try {
123
239
  const parsed = JSON.parse(content);
124
240
  const lock = {
@@ -132,8 +248,8 @@ export async function readLockfile(dir) {
132
248
  throw new ManifestError(`${file} no es un JSON válido.`, { cause });
133
249
  }
134
250
  }
135
- throw new ManifestError(`--transitive requiere package-lock.json o yarn.lock en ${path.resolve(dir)}. ` +
136
- 'Ejecuta "npm install" o "yarn install".');
251
+ throw new ManifestError(`--transitive requiere package-lock.json, yarn.lock o pnpm-lock.yaml en ${path.resolve(dir)}. ` +
252
+ 'Ejecuta "npm install", "yarn install" o "pnpm install".');
137
253
  }
138
254
  /**
139
255
  * Resolución estilo Node para npm: busca `<from>/node_modules/<name>` y sube por los ancestros.
@@ -221,9 +337,16 @@ function resolveYarnTransitive(lock, direct, options) {
221
337
  const result = [];
222
338
  const findEntry = (name, spec) => {
223
339
  if (spec) {
340
+ const cleanSpec = spec.startsWith('npm:') ? spec.slice(4) : spec;
224
341
  const directKey = `${name}@${spec}`;
225
342
  if (entries.has(directKey))
226
343
  return entries.get(directKey);
344
+ const cleanKey = `${name}@${cleanSpec}`;
345
+ if (entries.has(cleanKey))
346
+ return entries.get(cleanKey);
347
+ const berryKey = `${name}@npm:${cleanSpec}`;
348
+ if (entries.has(berryKey))
349
+ return entries.get(berryKey);
227
350
  }
228
351
  const prefix = `${name}@`;
229
352
  for (const [k, v] of entries) {
@@ -279,13 +402,82 @@ function resolveYarnTransitive(lock, direct, options) {
279
402
  }
280
403
  return result;
281
404
  }
405
+ function resolvePnpmTransitive(lock, direct, options) {
406
+ const packages = lock.packages;
407
+ const directNames = new Set(direct.map((d) => d.name));
408
+ const seenNames = new Set(directNames);
409
+ const visited = new Set();
410
+ const result = [];
411
+ const findEntry = (name, spec) => {
412
+ if (spec) {
413
+ const cleanSpec = spec.replace(/\(.*$/, '');
414
+ const exactKey = `${name}@${cleanSpec}`;
415
+ if (packages.has(exactKey))
416
+ return packages.get(exactKey);
417
+ if (packages.has(spec))
418
+ return packages.get(spec);
419
+ }
420
+ const prefix = `${name}@`;
421
+ for (const [k, v] of packages) {
422
+ if (k.startsWith(prefix))
423
+ return v;
424
+ }
425
+ return undefined;
426
+ };
427
+ const queue = [];
428
+ for (const d of direct) {
429
+ if (d.unsupportedReason)
430
+ continue;
431
+ const pkg = findEntry(d.name, d.spec);
432
+ if (!pkg)
433
+ continue;
434
+ const visitKey = `${d.name}@${pkg.version}`;
435
+ if (visited.has(visitKey))
436
+ continue;
437
+ visited.add(visitKey);
438
+ queue.push({ name: d.name, pkg, depth: 1, via: [d.name] });
439
+ }
440
+ while (queue.length > 0) {
441
+ const node = queue.shift();
442
+ if (node.depth >= options.maxDepth)
443
+ continue;
444
+ const children = { ...node.pkg.dependencies, ...node.pkg.optionalDependencies };
445
+ for (const [childName, childSpec] of Object.entries(children).sort(([a], [b]) => a.localeCompare(b))) {
446
+ const childPkg = findEntry(childName, childSpec);
447
+ if (!childPkg)
448
+ continue;
449
+ const visitKey = `${childName}@${childPkg.version}`;
450
+ if (visited.has(visitKey))
451
+ continue;
452
+ visited.add(visitKey);
453
+ const depth = node.depth + 1;
454
+ if (!seenNames.has(childName)) {
455
+ seenNames.add(childName);
456
+ const spec = {
457
+ name: childName,
458
+ registryName: childPkg.name,
459
+ spec: childPkg.version,
460
+ kind: 'transitive',
461
+ depth,
462
+ via: node.via,
463
+ };
464
+ result.push(spec);
465
+ }
466
+ queue.push({ name: childName, pkg: childPkg, depth, via: [...node.via, childName] });
467
+ }
468
+ }
469
+ return result;
470
+ }
282
471
  /**
283
472
  * Devuelve SOLO las dependencias transitivas (profundidad >= 2), deduplicadas por nombre,
284
- * alcanzables desde las directas indicadas en un lockfile de npm o Yarn.
473
+ * alcanzables desde las directas indicadas en un lockfile de npm, Yarn o pnpm.
285
474
  */
286
475
  export function resolveTransitive(lock, direct, options) {
287
476
  if (lock.type === 'yarn') {
288
477
  return resolveYarnTransitive(lock, direct, options);
289
478
  }
479
+ if (lock.type === 'pnpm') {
480
+ return resolvePnpmTransitive(lock, direct, options);
481
+ }
290
482
  return resolveNpmTransitive(lock, direct, options);
291
483
  }