reslop 0.1.8 → 0.2.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 (95) hide show
  1. package/CHANGELOG.md +25 -1
  2. package/README.md +27 -38
  3. package/bin/reslop.js +2 -2
  4. package/lib/ansi.js +6 -2
  5. package/lib/api.js +2 -2
  6. package/lib/capabilities.js +10 -14
  7. package/lib/cli.js +28 -47
  8. package/lib/clipboard.js +1 -1
  9. package/lib/deps-diff.js +712 -0
  10. package/lib/deps.js +556 -32
  11. package/lib/detect.js +1 -7
  12. package/lib/diff/align.js +7 -10
  13. package/lib/diff/diff.js +53 -0
  14. package/lib/diff/display.js +5 -4
  15. package/lib/diff/inline.js +26 -21
  16. package/lib/diff/items.js +6 -4
  17. package/lib/diff/patch.js +1 -3
  18. package/lib/diff/unit.js +392 -0
  19. package/lib/files.js +34 -8
  20. package/lib/{git/branches.js → git-branches.js} +103 -10
  21. package/lib/{git/dependencies.js → git-deps.js} +111 -38
  22. package/lib/git.js +592 -65
  23. package/lib/github.js +28 -197
  24. package/lib/gitlab.js +24 -195
  25. package/lib/highlight/bash.js +1 -3
  26. package/lib/highlight/css.js +1 -3
  27. package/lib/highlight/js.js +11 -14
  28. package/lib/highlight.js +6 -9
  29. package/lib/keys.js +22 -9
  30. package/lib/manifest.js +330 -0
  31. package/lib/remote.js +374 -0
  32. package/lib/render/branches.js +34 -66
  33. package/lib/render/chrome.js +57 -22
  34. package/lib/render/commits.js +85 -0
  35. package/lib/render/diff.js +16 -16
  36. package/lib/render/files.js +29 -37
  37. package/lib/render/frame.js +29 -13
  38. package/lib/render/notes.js +4 -3
  39. package/lib/render/primitives.js +46 -1
  40. package/lib/{render.js → render/render.js} +6 -7
  41. package/lib/render/unit.js +41 -0
  42. package/lib/review.js +750 -14
  43. package/lib/select.js +5 -10
  44. package/lib/session/accessors.js +9 -1
  45. package/lib/session/actions.js +93 -36
  46. package/lib/session/branches.js +130 -144
  47. package/lib/session/change-actions.js +206 -124
  48. package/lib/session/commit.js +217 -62
  49. package/lib/session/compose.js +158 -13
  50. package/lib/session/dispatch.js +109 -55
  51. package/lib/session/files-pane.js +145 -142
  52. package/lib/session/items.js +131 -34
  53. package/lib/session/lifecycle.js +82 -42
  54. package/lib/session/load.js +19 -33
  55. package/lib/session/navigation.js +15 -3
  56. package/lib/session/ops.js +28 -41
  57. package/lib/session/pointer.js +58 -54
  58. package/lib/session/review.js +23 -37
  59. package/lib/session/terminal.js +6 -12
  60. package/lib/session/todos.js +8 -12
  61. package/lib/session/unit.js +208 -0
  62. package/lib/session/update.js +33 -46
  63. package/lib/session/view.js +82 -11
  64. package/lib/session/watch.js +11 -19
  65. package/lib/session.js +29 -226
  66. package/lib/source.js +3 -3
  67. package/lib/update.js +40 -75
  68. package/lib/{git/proc.js → utilities.js} +75 -11
  69. package/lib/wrap.js +1 -3
  70. package/package.json +2 -2
  71. package/lib/deps/changes.js +0 -331
  72. package/lib/deps/items.js +0 -131
  73. package/lib/deps/manifest.js +0 -145
  74. package/lib/deps/merge.js +0 -120
  75. package/lib/deps/proposals.js +0 -164
  76. package/lib/deps/reports.js +0 -180
  77. package/lib/deps/rows.js +0 -177
  78. package/lib/deps/scan.js +0 -138
  79. package/lib/deps/usage.js +0 -68
  80. package/lib/deps/versions.js +0 -119
  81. package/lib/diff.js +0 -33
  82. package/lib/git/changes.js +0 -164
  83. package/lib/git/read.js +0 -253
  84. package/lib/git/run.js +0 -100
  85. package/lib/npm/commands.js +0 -100
  86. package/lib/remote/changes.js +0 -37
  87. package/lib/remote/headers.js +0 -36
  88. package/lib/remote/http.js +0 -122
  89. package/lib/remote/notes.js +0 -94
  90. package/lib/review/format.js +0 -434
  91. package/lib/review/storage.js +0 -150
  92. package/lib/review/store.js +0 -178
  93. package/lib/review/templates.js +0 -49
  94. package/lib/session/origins.js +0 -7
  95. package/lib/sys.js +0 -84
package/lib/deps.js CHANGED
@@ -1,34 +1,504 @@
1
1
  'use strict';
2
2
 
3
+ const fs = require('node:fs');
4
+ const path = require('node:path');
5
+ const { jsonParse, isHashObject } = require('metautil');
3
6
  const { itemPath } = require('./files.js');
7
+ const manifest = require('./manifest.js');
8
+ const { MANIFEST, LOCKFILE, SECTION_KEYS, depFileMeta, relOf } = manifest;
9
+ const { listFilePath, readSections, lockEntries, lockPackageCount } = manifest;
10
+ const { mergeDepFile, findDeclared, applyWantedRange } = manifest;
11
+ const { patchedFromRange, proposedLockVersion, proposedWanted } = manifest;
12
+ const depDiff = require('./deps-diff.js');
13
+ const { diffSections, mergeResolved, summarizeGroup, collectGroups } = depDiff;
14
+ const { takeManifestItems, foldedDepNames, stripFoldedDepItem } = depDiff;
15
+ const { makeDepItems, stubFileItem } = depDiff;
4
16
 
5
- const manifest = require('./deps/manifest.js');
6
- const { MANIFEST, LOCKFILE, depFileMeta, parseJson } = manifest;
7
- const { readSections, lockEntries, lockPackageCount } = manifest;
17
+ const SOURCE_EXT = ['.js', '.mjs', '.cjs', '.ts', '.mts', '.cts'];
18
+ const REQUIRE_SPEC = /require\(\s*['"]([^'"]+)['"]\s*\)/g;
19
+ const FROM_SPEC = /\bfrom\s+['"]([^'"]+)['"]/g;
20
+ const IMPORT_FILE = /import\s+['"]([^'"]+)['"]/g;
21
+ const IMPORT_CALL = /import\(\s*['"]([^'"]+)['"]\s*\)/g;
8
22
 
9
- const { mergeDepFile } = require('./deps/merge.js');
23
+ const toPackageName = (spec) => {
24
+ if (!spec || spec.startsWith('.') || spec.startsWith('/')) return '';
25
+ if (spec.startsWith('node:')) return '';
26
+ if (spec.startsWith('@')) {
27
+ const parts = spec.split('/');
28
+ if (parts.length < 2) return '';
29
+ return `${parts[0]}/${parts[1]}`;
30
+ }
31
+ return spec.split('/')[0];
32
+ };
33
+
34
+ const addSpec = (used, spec) => {
35
+ const name = toPackageName(spec);
36
+ if (name) used.add(name);
37
+ };
38
+
39
+ const addMatches = (used, text, pattern) => {
40
+ const matches = text.matchAll(pattern);
41
+ for (const match of matches) addSpec(used, match[1]);
42
+ };
43
+
44
+ const collectUsedFromText = (used, text) => {
45
+ addMatches(used, text, REQUIRE_SPEC);
46
+ addMatches(used, text, FROM_SPEC);
47
+ addMatches(used, text, IMPORT_FILE);
48
+ addMatches(used, text, IMPORT_CALL);
49
+ };
50
+
51
+ const collectUsedFromScripts = (used, scripts) => {
52
+ if (!scripts || typeof scripts !== 'object') return;
53
+ for (const script of Object.values(scripts)) {
54
+ const tokens = `${script}`.split(/[\s;&|]+/);
55
+ for (const token of tokens) {
56
+ if (!token || token.startsWith('-')) continue;
57
+ if (token === 'npx' || token === 'npm' || token === 'node') continue;
58
+ addSpec(used, token);
59
+ }
60
+ }
61
+ };
62
+
63
+ const pkgHasExportEntry = (pkg) => {
64
+ if (!pkg || typeof pkg !== 'object') return false;
65
+ const main = pkg.main;
66
+ if (typeof main === 'string' && main) return true;
67
+ const esm = pkg.module;
68
+ if (typeof esm === 'string' && esm) return true;
69
+ const exported = pkg.exports;
70
+ if (typeof exported === 'string' && exported) return true;
71
+ if (!exported || typeof exported !== 'object') return false;
72
+ if (Array.isArray(exported)) return exported.length > 0;
73
+ return Object.keys(exported).length > 0;
74
+ };
75
+
76
+ const SKIP_WALK = ['node_modules', '.git', '.review', 'coverage', 'dist'];
77
+
78
+ const collectUsedFromManifest = (used, rel) => {
79
+ let text;
80
+ try {
81
+ text = fs.readFileSync(rel, 'utf8');
82
+ } catch {
83
+ return;
84
+ }
85
+ const pkg = jsonParse(text);
86
+ if (!pkg) return;
87
+ collectUsedFromScripts(used, pkg.scripts);
88
+ };
89
+
90
+ const walkUsedNames = (used, dir) => {
91
+ let entries;
92
+ try {
93
+ entries = fs.readdirSync(dir, { withFileTypes: true });
94
+ } catch {
95
+ return;
96
+ }
97
+ for (const entry of entries) {
98
+ if (entry.isSymbolicLink()) continue;
99
+ const full = path.join(dir, entry.name);
100
+ if (entry.isDirectory()) {
101
+ if (SKIP_WALK.includes(entry.name)) continue;
102
+ if (entry.name.startsWith('.')) continue;
103
+ walkUsedNames(used, full);
104
+ continue;
105
+ }
106
+ if (entry.name === MANIFEST) {
107
+ collectUsedFromManifest(used, full);
108
+ continue;
109
+ }
110
+ const ext = path.extname(entry.name);
111
+ if (!SOURCE_EXT.includes(ext)) continue;
112
+ let text;
113
+ try {
114
+ text = fs.readFileSync(full, 'utf8');
115
+ } catch {
116
+ continue;
117
+ }
118
+ collectUsedFromText(used, text);
119
+ }
120
+ };
121
+
122
+ const collectUsedNames = (root) => {
123
+ const used = new Set();
124
+ if (!root) return used;
125
+ walkUsedNames(used, root);
126
+ return used;
127
+ };
128
+
129
+ const depModuleParts = (name) => {
130
+ if (!name.startsWith('@')) return [name];
131
+ const parts = name.split('/');
132
+ if (parts.length < 2) return [];
133
+ return parts.slice(0, 2);
134
+ };
10
135
 
11
- const changes = require('./deps/changes.js');
12
- const { diffSections, mergeResolved, summarizeGroup } = changes;
13
- const { collectGroups, takeManifestItems } = changes;
136
+ const readDepPkg = (root, pkgDir, name) => {
137
+ if (!root || !name) return null;
138
+ const parts = depModuleParts(name);
139
+ if (!parts.length) return null;
140
+ const nested = pkgDir && pkgDir !== '.' ? pkgDir : '';
141
+ let dir = nested ? path.join(root, nested) : root;
142
+ while (true) {
143
+ const file = path.join(dir, 'node_modules', ...parts, MANIFEST);
144
+ let text = '';
145
+ try {
146
+ text = fs.readFileSync(file, 'utf8');
147
+ } catch {
148
+ // Missing node_modules copy; try a parent directory.
149
+ }
150
+ if (text) return jsonParse(text);
151
+ const parent = path.dirname(dir);
152
+ if (parent === dir) return null;
153
+ dir = parent;
154
+ }
155
+ };
14
156
 
15
- const { makeDepItems, stubFileItem } = require('./deps/items.js');
157
+ const depHasExportEntry = (root, pkgDir, name) => {
158
+ const pkg = readDepPkg(root, pkgDir, name);
159
+ if (!pkg) return true;
160
+ return pkgHasExportEntry(pkg);
161
+ };
16
162
 
17
- const scan = require('./deps/scan.js');
18
- const { collectUsedNames, collectExportEntries } = scan;
163
+ const collectExportEntries = (root, pkgDir, names) => {
164
+ const entries = new Map();
165
+ if (!root) return entries;
166
+ for (const name of names) {
167
+ const hasExport = depHasExportEntry(root, pkgDir, name);
168
+ entries.set(name, hasExport);
169
+ }
170
+ return entries;
171
+ };
19
172
 
20
- const reports = require('./deps/reports.js');
21
- const { parseAuditReport, parseOutdatedReport } = reports;
22
- const { markAuditChanges, markOutdatedChanges } = reports;
173
+ const declaredDepNames = (root, pkgDir) => {
174
+ const nested = pkgDir && pkgDir !== '.' ? pkgDir : '';
175
+ const file = path.join(root, nested, MANIFEST);
176
+ let text;
177
+ try {
178
+ text = fs.readFileSync(file, 'utf8');
179
+ } catch {
180
+ return [];
181
+ }
182
+ const maps = readSections(jsonParse(text));
183
+ let size = 0;
184
+ for (const key of SECTION_KEYS) size += maps[key].size;
185
+ const names = new Array(size);
186
+ let i = 0;
187
+ for (const key of SECTION_KEYS) {
188
+ for (const name of maps[key].keys()) names[i++] = name;
189
+ }
190
+ return names;
191
+ };
23
192
 
24
- const versions = require('./deps/versions.js');
25
- const { applyWantedRange, patchedFromRange } = versions;
193
+ const collectExportMeta = (root, extra = {}) => {
194
+ if (extra.exportEntries !== undefined) return extra.exportEntries;
195
+ const pkgDir = extra.dir ?? '.';
196
+ const names = extra.names ?? declaredDepNames(root, pkgDir);
197
+ return collectExportEntries(root, pkgDir, names);
198
+ };
26
199
 
27
- const proposals = require('./deps/proposals.js');
28
- const { unusedChangeNames, unusedDeclaredNames } = proposals;
29
- const { markUnusedChanges, buildProposedChanges } = proposals;
200
+ const SEVERITY_RANK = { critical: 4, high: 3, moderate: 2, low: 1, info: 0 };
30
201
 
31
- const noSides = () => ({ oldText: '', newText: '' });
202
+ const namedTitle = (name, title) => {
203
+ if (!title) return '';
204
+ if (!name) return title;
205
+ const prefix = `${name}: `;
206
+ if (title.startsWith(prefix)) return title;
207
+ return `${prefix}${title}`;
208
+ };
209
+
210
+ const viaNotes = (via, pkgName) => {
211
+ if (!Array.isArray(via)) return [];
212
+ const notes = [];
213
+ for (const item of via) {
214
+ if (!item || typeof item !== 'object') continue;
215
+ const title = typeof item.title === 'string' ? item.title : '';
216
+ if (!title) continue;
217
+ const hasName = typeof item.name === 'string' && item.name;
218
+ const named = hasName ? item.name : pkgName;
219
+ const note = namedTitle(named, title);
220
+ if (!notes.includes(note)) notes.push(note);
221
+ }
222
+ return notes;
223
+ };
224
+
225
+ const severityRank = (severity) => SEVERITY_RANK[severity] ?? 0;
226
+
227
+ const auditFix = (entry) => {
228
+ const available = entry.fixAvailable;
229
+ if (!available || typeof available !== 'object') return '';
230
+ if (typeof available.version !== 'string') return '';
231
+ return available.version;
232
+ };
233
+
234
+ const auditEntryNames = (key, entry) => {
235
+ const names = [];
236
+ const primary = entry.name || key;
237
+ if (primary) names.push(primary);
238
+ const effects = entry.effects;
239
+ if (!Array.isArray(effects)) return names;
240
+ for (const effect of effects) {
241
+ if (typeof effect === 'string' && effect) names.push(effect);
242
+ }
243
+ return names;
244
+ };
245
+
246
+ const keepHighestAudit = (audit, name, severity, fix, range, notes) => {
247
+ if (!name || !severity || severity === 'info') return;
248
+ const titles = [];
249
+ for (const note of notes) {
250
+ if (note && !titles.includes(note)) titles.push(note);
251
+ }
252
+ const prev = audit.get(name);
253
+ if (prev && severityRank(prev.severity) >= severityRank(severity)) {
254
+ const merged = [...prev.titles];
255
+ for (const note of titles) {
256
+ if (!merged.includes(note)) merged.push(note);
257
+ }
258
+ const next = {
259
+ ...prev,
260
+ titles: merged,
261
+ fix: prev.fix || fix,
262
+ range: prev.range || range,
263
+ };
264
+ audit.set(name, next);
265
+ return;
266
+ }
267
+ const title = titles[0] || '';
268
+ const next = { name, severity, title, titles, fix, range };
269
+ next.fix ||= '';
270
+ next.range ||= '';
271
+ audit.set(name, next);
272
+ };
273
+
274
+ const parseAuditReport = (text) => {
275
+ const audit = new Map();
276
+ if (!text || !`${text}`.trim()) return audit;
277
+ const data = jsonParse(text);
278
+ if (!isHashObject(data)) return audit;
279
+ const vulns = data.vulnerabilities;
280
+ if (vulns && typeof vulns === 'object') {
281
+ for (const key of Object.keys(vulns)) {
282
+ const entry = vulns[key];
283
+ if (!entry || typeof entry !== 'object') continue;
284
+ const severity = entry.severity;
285
+ const names = auditEntryNames(key, entry);
286
+ const pkgName = entry.name || key;
287
+ const titles = viaNotes(entry.via, pkgName);
288
+ const fix = auditFix(entry);
289
+ const range = typeof entry.range === 'string' ? entry.range : '';
290
+ for (const name of names) {
291
+ keepHighestAudit(audit, name, severity, fix, range, titles);
292
+ }
293
+ }
294
+ }
295
+ const advisories = data.advisories;
296
+ if (advisories && typeof advisories === 'object') {
297
+ for (const entry of Object.values(advisories)) {
298
+ if (!entry || typeof entry !== 'object') continue;
299
+ const name = entry.module_name || entry.name;
300
+ const severity = entry.severity;
301
+ const raw = typeof entry.title === 'string' ? entry.title : '';
302
+ const title = namedTitle(name, raw);
303
+ const titles = title ? [title] : [];
304
+ const fix = auditFix(entry);
305
+ const range = typeof entry.range === 'string' ? entry.range : '';
306
+ keepHighestAudit(audit, name, severity, fix, range, titles);
307
+ }
308
+ }
309
+ return audit;
310
+ };
311
+
312
+ const parseOutdatedReport = (text) => {
313
+ const outdated = new Map();
314
+ if (!text || !`${text}`.trim()) return outdated;
315
+ const data = jsonParse(text);
316
+ if (!isHashObject(data)) return outdated;
317
+ for (const name of Object.keys(data)) {
318
+ const entry = data[name];
319
+ if (!entry || typeof entry !== 'object') continue;
320
+ const current = typeof entry.current === 'string' ? entry.current : '';
321
+ const wanted = typeof entry.wanted === 'string' ? entry.wanted : '';
322
+ const latest = typeof entry.latest === 'string' ? entry.latest : '';
323
+ const type = typeof entry.type === 'string' ? entry.type : '';
324
+ if (!wanted) continue;
325
+ outdated.set(name, { current, wanted, latest, type });
326
+ }
327
+ return outdated;
328
+ };
329
+
330
+ const markChanges = (changes, report, kind) => {
331
+ if (!report) return changes;
332
+ return changes.map((change) => {
333
+ if (change.action === 'removed' || change.section === 'field') {
334
+ return change;
335
+ }
336
+ const found = report.get(change.name);
337
+ if (!found) return change;
338
+ if (kind === 'outdated' && found.wanted && found.wanted === found.current) {
339
+ return change;
340
+ }
341
+ return { ...change, [kind]: found };
342
+ });
343
+ };
344
+
345
+ const markAuditChanges = (changes, audit) =>
346
+ markChanges(changes, audit, 'audit');
347
+
348
+ const markOutdatedChanges = (changes, outdated) =>
349
+ markChanges(changes, outdated, 'outdated');
350
+
351
+ const depExports = (exportEntries, name) => {
352
+ if (!exportEntries || !exportEntries.has(name)) return true;
353
+ return exportEntries.get(name) === true;
354
+ };
355
+
356
+ const isAddedDep = (change) => {
357
+ if (change.action !== 'added') return false;
358
+ if (change.section === 'resolved') return false;
359
+ if (change.section === 'field') return false;
360
+ return true;
361
+ };
362
+
363
+ const unusedChangeNames = (changes, used) => {
364
+ const names = [];
365
+ if (!used || !used.size) return names;
366
+ for (const change of changes) {
367
+ if (!isAddedDep(change)) continue;
368
+ if (used.has(change.name)) continue;
369
+ names.push(change.name);
370
+ }
371
+ return names;
372
+ };
373
+
374
+ const unusedDeclaredNames = (pkg, used) => {
375
+ const names = [];
376
+ if (!pkgHasExportEntry(pkg)) return names;
377
+ if (!used || !used.size) return names;
378
+ const maps = readSections(pkg);
379
+ for (const section of SECTION_KEYS) {
380
+ if (section === 'peerDependencies') continue;
381
+ const keys = [...maps[section].keys()];
382
+ keys.sort();
383
+ for (const name of keys) {
384
+ if (used.has(name)) continue;
385
+ names.push(name);
386
+ }
387
+ }
388
+ return names;
389
+ };
390
+
391
+ const markUnusedChanges = (changes, used, pkg, exportEntries) => {
392
+ if (!pkgHasExportEntry(pkg)) return changes;
393
+ if (!used || !used.size) return changes;
394
+ const { length } = changes;
395
+ const marked = new Array(length);
396
+ for (let i = 0; i < length; i++) {
397
+ const change = changes[i];
398
+ if (!isAddedDep(change) || used.has(change.name)) {
399
+ marked[i] = change;
400
+ continue;
401
+ }
402
+ if (!depExports(exportEntries, change.name)) {
403
+ marked[i] = change;
404
+ continue;
405
+ }
406
+ marked[i] = { ...change, unused: true, propose: true };
407
+ }
408
+ return marked;
409
+ };
410
+
411
+ const unusedDeclaredChanges = (pkg, used, exportEntries) => {
412
+ const changes = [];
413
+ if (!pkgHasExportEntry(pkg)) return changes;
414
+ if (!used || !used.size) return changes;
415
+ const maps = readSections(pkg);
416
+ const taken = new Set();
417
+ for (const section of SECTION_KEYS) {
418
+ if (section === 'peerDependencies') continue;
419
+ const map = maps[section];
420
+ const names = [...map.keys()];
421
+ names.sort();
422
+ for (const name of names) {
423
+ if (used.has(name)) continue;
424
+ if (taken.has(name)) continue;
425
+ if (!depExports(exportEntries, name)) continue;
426
+ taken.add(name);
427
+ const version = map.get(name);
428
+ changes.push({
429
+ name,
430
+ section,
431
+ action: 'removed',
432
+ from: version,
433
+ to: '',
434
+ propose: true,
435
+ unused: true,
436
+ });
437
+ }
438
+ }
439
+ return changes;
440
+ };
441
+
442
+ const buildProposedChanges = (pkg, outdated, audit, lock, options = {}) => {
443
+ const used = options.used;
444
+ const exportEntries = options.exportEntries;
445
+ const names = new Set();
446
+ if (outdated) {
447
+ for (const name of outdated.keys()) names.add(name);
448
+ }
449
+ if (audit) {
450
+ for (const name of audit.keys()) names.add(name);
451
+ }
452
+ const listed = [...names];
453
+ listed.sort();
454
+ const resolved = lockEntries(lock);
455
+ const unused = unusedDeclaredChanges(pkg, used, exportEntries);
456
+ const taken = new Set();
457
+ for (const change of unused) taken.add(change.name);
458
+ const changes = [...unused];
459
+ for (const name of listed) {
460
+ if (taken.has(name)) continue;
461
+ const out = outdated ? outdated.get(name) : null;
462
+ const vul = audit ? audit.get(name) : null;
463
+ const type = out ? out.type : '';
464
+ const declared = findDeclared(pkg, name, type);
465
+ if (!declared) {
466
+ if (!vul) continue;
467
+ const version = resolved.get(name) || '';
468
+ if (!version) continue;
469
+ const to = proposedLockVersion(version, vul);
470
+ changes.push({
471
+ name,
472
+ section: 'resolved',
473
+ action: 'vulnerable',
474
+ from: version,
475
+ to,
476
+ propose: true,
477
+ audit: vul,
478
+ });
479
+ continue;
480
+ }
481
+ const wanted = proposedWanted(out, vul);
482
+ if (!wanted && !vul) continue;
483
+ let to = declared.version;
484
+ if (wanted) to = applyWantedRange(declared.version, wanted);
485
+ if (to === declared.version && !vul) continue;
486
+ let action = 'changed';
487
+ if (to === declared.version) action = 'vulnerable';
488
+ const change = {
489
+ name,
490
+ section: declared.section,
491
+ action,
492
+ from: declared.version,
493
+ to,
494
+ propose: true,
495
+ };
496
+ if (out) change.outdated = out;
497
+ if (vul) change.audit = vul;
498
+ changes.push(change);
499
+ }
500
+ return changes;
501
+ };
32
502
 
33
503
  const loadExportEntries = (names, extra = {}, pkgDir) => {
34
504
  const supplied = extra.exportEntries;
@@ -46,9 +516,50 @@ const loadExportEntries = (names, extra = {}, pkgDir) => {
46
516
  return merged;
47
517
  };
48
518
 
519
+ const groupedSynthetics = (synthetic) => {
520
+ const byPath = new Map();
521
+ for (const key of synthetic.keys()) {
522
+ const dir = key.slice(key.indexOf('\0') + 1);
523
+ const rel = relOf(dir, MANIFEST);
524
+ const list = byPath.get(rel) ?? [];
525
+ for (const item of synthetic.get(key)) list.push(item);
526
+ byPath.set(rel, list);
527
+ }
528
+ return byPath;
529
+ };
530
+
531
+ const spliceAtSlots = (out, synthetic, slot) => {
532
+ const byPath = groupedSynthetics(synthetic);
533
+ const inserts = new Array(byPath.size);
534
+ let i = 0;
535
+ for (const rel of byPath.keys()) {
536
+ const at = slot.has(rel) ? slot.get(rel) : out.length;
537
+ inserts[i++] = { at, items: byPath.get(rel) };
538
+ }
539
+ inserts.sort((left, right) => right.at - left.at);
540
+ for (const insert of inserts) {
541
+ out.splice(insert.at, 0, ...insert.items);
542
+ }
543
+ return out;
544
+ };
545
+
546
+ const lastPathIndex = (items, rel) => {
547
+ let at = -1;
548
+ for (let i = 0; i < items.length; i++) {
549
+ if (listFilePath(itemPath(items[i])) === rel) at = i;
550
+ }
551
+ return at;
552
+ };
553
+
554
+ const insertAfterPath = (items, added, rel) => {
555
+ const at = lastPathIndex(items, rel);
556
+ if (at < 0) return [...items, ...added];
557
+ return [...items.slice(0, at + 1), ...added, ...items.slice(at + 1)];
558
+ };
559
+
49
560
  const foldDepItems = (
50
561
  items,
51
- readSides = noSides,
562
+ readSides = () => ({ oldText: '', newText: '' }),
52
563
  usedNames = null,
53
564
  audit = null,
54
565
  outdated = null,
@@ -58,6 +569,7 @@ const foldDepItems = (
58
569
  if (!groups.size) return items;
59
570
  const folded = new Set();
60
571
  const synthetic = new Map();
572
+ const leftoverNames = new Map();
61
573
  for (const key of groups.keys()) {
62
574
  const group = groups.get(key);
63
575
  const summary = summarizeGroup(group, readSides);
@@ -72,38 +584,48 @@ const foldDepItems = (
72
584
  const audited = markAuditChanges(unused, audit);
73
585
  const changes = markOutdatedChanges(audited, outdated);
74
586
  const next = { ...summary, changes };
587
+ leftoverNames.set(key, foldedDepNames(next.changes));
75
588
  const taken = takeManifestItems(next);
76
589
  if (next.foldLock) {
77
590
  for (const item of next.lockItems) taken.push(item);
78
591
  }
79
592
  if (!taken.length && !next.changes.length) continue;
80
593
  const made = makeDepItems(group.origin, group.items, taken, next);
81
- synthetic.set(key, made);
594
+ if (made.length) synthetic.set(key, made);
82
595
  for (const item of taken) folded.add(item);
83
596
  }
84
- if (!synthetic.size) return items;
597
+ if (!synthetic.size && !folded.size) return items;
85
598
  const out = [];
86
- const placed = new Set();
599
+ const slot = new Map();
87
600
  for (const item of items) {
88
601
  const rel = itemPath(item);
89
602
  const meta = depFileMeta(rel);
90
603
  if (meta) {
604
+ const pkgRel = relOf(meta.dir, MANIFEST);
91
605
  const key = `${item.origin}\0${meta.dir}`;
92
- if (synthetic.has(key) && !placed.has(key)) {
93
- placed.add(key);
94
- const made = synthetic.get(key);
95
- for (const depItem of made) out.push(depItem);
606
+ if (synthetic.has(key) && !slot.has(pkgRel)) {
607
+ slot.set(pkgRel, out.length);
96
608
  }
97
609
  }
98
- if (!folded.has(item)) out.push(item);
610
+ if (folded.has(item)) continue;
611
+ if (meta && meta.kind === 'manifest') {
612
+ const names = leftoverNames.get(`${item.origin}\0${meta.dir}`);
613
+ const kept = names ? stripFoldedDepItem(item, names) : item;
614
+ if (kept) {
615
+ out.push(kept);
616
+ slot.set(relOf(meta.dir, MANIFEST), out.length);
617
+ }
618
+ continue;
619
+ }
620
+ out.push(item);
99
621
  }
100
- return out;
622
+ return spliceAtSlots(out, synthetic, slot);
101
623
  };
102
624
 
103
625
  const proposeDepItems = (pkgText, outdated, audit, extra = {}) => {
104
- const pkg = parseJson(pkgText);
626
+ const pkg = jsonParse(pkgText ?? '');
105
627
  if (!pkg) return [];
106
- const lock = parseJson(extra.lockText ?? '');
628
+ const lock = jsonParse(extra.lockText ?? '');
107
629
  const used = extra.usedNames;
108
630
  const names = unusedDeclaredNames(pkg, used);
109
631
  const exportEntries = loadExportEntries(names, extra, extra.dir);
@@ -133,7 +655,8 @@ const mergeProposedItems = (items, pkgText, outdated, audit, extra = {}) => {
133
655
  added.push(item);
134
656
  }
135
657
  if (!added.length) return items;
136
- return [...items, ...added];
658
+ const rel = relOf(extra.dir ?? '.', MANIFEST);
659
+ return insertAfterPath(items, added, rel);
137
660
  };
138
661
 
139
662
  module.exports = {
@@ -147,6 +670,7 @@ module.exports = {
147
670
  mergeDepFile,
148
671
  mergeResolved,
149
672
  collectUsedNames,
673
+ collectExportMeta,
150
674
  parseAuditReport,
151
675
  parseOutdatedReport,
152
676
  applyWantedRange,
package/lib/detect.js CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const path = require('node:path');
4
+ const { fileExt } = require('metautil');
4
5
 
5
6
  const { mapLang } = require('./highlight.js');
6
7
 
@@ -13,13 +14,6 @@ const PROSE_BASENAMES = [
13
14
  'copyright',
14
15
  ];
15
16
 
16
- const fileExt = (filePath) => {
17
- const base = path.basename(filePath || '');
18
- const dot = base.lastIndexOf('.');
19
- if (dot <= 0) return '';
20
- return base.slice(dot + 1).toLowerCase();
21
- };
22
-
23
17
  const isProseBasename = (base) => {
24
18
  const name = (base || '').toLowerCase();
25
19
  if (PROSE_BASENAMES.includes(name)) return true;