reslop 0.1.9 → 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 (94) hide show
  1. package/CHANGELOG.md +12 -1
  2. package/README.md +2 -1
  3. package/bin/reslop.js +2 -2
  4. package/lib/ansi.js +4 -2
  5. package/lib/api.js +2 -2
  6. package/lib/capabilities.js +7 -14
  7. package/lib/cli.js +28 -47
  8. package/lib/clipboard.js +1 -1
  9. package/lib/{deps/changes.js → deps-diff.js} +309 -19
  10. package/lib/deps.js +498 -29
  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 +5 -8
  20. package/lib/{git/branches.js → git-branches.js} +6 -5
  21. package/lib/{git/dependencies.js → git-deps.js} +111 -38
  22. package/lib/git.js +589 -68
  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 +14 -8
  30. package/lib/manifest.js +330 -0
  31. package/lib/remote.js +374 -0
  32. package/lib/render/branches.js +14 -68
  33. package/lib/render/chrome.js +24 -9
  34. package/lib/render/commits.js +6 -59
  35. package/lib/render/diff.js +16 -16
  36. package/lib/render/files.js +6 -34
  37. package/lib/render/frame.js +23 -12
  38. package/lib/render/notes.js +4 -3
  39. package/lib/render/primitives.js +45 -0
  40. package/lib/{render.js → render/render.js} +5 -5
  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 +32 -16
  46. package/lib/session/branches.js +116 -128
  47. package/lib/session/change-actions.js +202 -123
  48. package/lib/session/commit.js +105 -112
  49. package/lib/session/compose.js +158 -13
  50. package/lib/session/dispatch.js +104 -60
  51. package/lib/session/files-pane.js +142 -148
  52. package/lib/session/items.js +22 -30
  53. package/lib/session/lifecycle.js +44 -44
  54. package/lib/session/load.js +19 -33
  55. package/lib/session/navigation.js +12 -1
  56. package/lib/session/ops.js +28 -41
  57. package/lib/session/pointer.js +54 -57
  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 +78 -9
  64. package/lib/session/watch.js +11 -19
  65. package/lib/session.js +21 -246
  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 +1 -1
  71. package/lib/deps/items.js +0 -134
  72. package/lib/deps/manifest.js +0 -152
  73. package/lib/deps/merge.js +0 -120
  74. package/lib/deps/proposals.js +0 -164
  75. package/lib/deps/reports.js +0 -180
  76. package/lib/deps/rows.js +0 -176
  77. package/lib/deps/scan.js +0 -138
  78. package/lib/deps/usage.js +0 -68
  79. package/lib/deps/versions.js +0 -119
  80. package/lib/diff.js +0 -33
  81. package/lib/git/changes.js +0 -164
  82. package/lib/git/read.js +0 -253
  83. package/lib/git/run.js +0 -100
  84. package/lib/npm/commands.js +0 -100
  85. package/lib/remote/changes.js +0 -37
  86. package/lib/remote/headers.js +0 -36
  87. package/lib/remote/http.js +0 -122
  88. package/lib/remote/notes.js +0 -94
  89. package/lib/review/format.js +0 -434
  90. package/lib/review/storage.js +0 -150
  91. package/lib/review/store.js +0 -178
  92. package/lib/review/templates.js +0 -49
  93. package/lib/session/origins.js +0 -7
  94. package/lib/sys.js +0 -84
@@ -1,180 +0,0 @@
1
- 'use strict';
2
-
3
- const { jsonParse, isHashObject } = require('metautil');
4
-
5
- const SEVERITY_RANK = { critical: 4, high: 3, moderate: 2, low: 1, info: 0 };
6
-
7
- const namedTitle = (name, title) => {
8
- if (!title) return '';
9
- if (!name) return title;
10
- const prefix = `${name}: `;
11
- if (title.startsWith(prefix)) return title;
12
- return `${prefix}${title}`;
13
- };
14
-
15
- const viaNotes = (via, pkgName) => {
16
- if (!Array.isArray(via)) return [];
17
- const notes = [];
18
- for (const item of via) {
19
- if (!item || typeof item !== 'object') continue;
20
- const title = typeof item.title === 'string' ? item.title : '';
21
- if (!title) continue;
22
- const hasName = typeof item.name === 'string' && item.name;
23
- const named = hasName ? item.name : pkgName;
24
- const note = namedTitle(named, title);
25
- if (!notes.includes(note)) notes.push(note);
26
- }
27
- return notes;
28
- };
29
-
30
- const severityRank = (severity) => SEVERITY_RANK[severity] ?? 0;
31
-
32
- const auditFix = (entry) => {
33
- const available = entry.fixAvailable;
34
- if (!available || typeof available !== 'object') return '';
35
- if (typeof available.version !== 'string') return '';
36
- return available.version;
37
- };
38
-
39
- const auditEntryNames = (key, entry) => {
40
- const names = [];
41
- const primary = entry.name || key;
42
- if (primary) names.push(primary);
43
- const effects = entry.effects;
44
- if (!Array.isArray(effects)) return names;
45
- for (const effect of effects) {
46
- if (typeof effect === 'string' && effect) names.push(effect);
47
- }
48
- return names;
49
- };
50
-
51
- const keepHighestAudit = (audit, name, severity, fix, range, notes) => {
52
- if (!name || !severity || severity === 'info') return;
53
- const titles = [];
54
- for (const note of notes) {
55
- if (note && !titles.includes(note)) titles.push(note);
56
- }
57
- const prev = audit.get(name);
58
- if (prev && severityRank(prev.severity) >= severityRank(severity)) {
59
- const merged = [...prev.titles];
60
- for (const note of titles) {
61
- if (!merged.includes(note)) merged.push(note);
62
- }
63
- const next = {
64
- ...prev,
65
- titles: merged,
66
- fix: prev.fix || fix,
67
- range: prev.range || range,
68
- };
69
- audit.set(name, next);
70
- return;
71
- }
72
- const title = titles[0] || '';
73
- const next = { name, severity, title, titles, fix, range };
74
- next.fix ||= '';
75
- next.range ||= '';
76
- audit.set(name, next);
77
- };
78
-
79
- const parseAuditReport = (text) => {
80
- const audit = new Map();
81
- if (!text || !`${text}`.trim()) return audit;
82
- const data = jsonParse(text);
83
- if (!isHashObject(data)) return audit;
84
- const vulns = data.vulnerabilities;
85
- if (vulns && typeof vulns === 'object') {
86
- for (const key of Object.keys(vulns)) {
87
- const entry = vulns[key];
88
- if (!entry || typeof entry !== 'object') continue;
89
- const severity = entry.severity;
90
- const names = auditEntryNames(key, entry);
91
- const pkgName = entry.name || key;
92
- const titles = viaNotes(entry.via, pkgName);
93
- const fix = auditFix(entry);
94
- const range = typeof entry.range === 'string' ? entry.range : '';
95
- for (const name of names) {
96
- keepHighestAudit(audit, name, severity, fix, range, titles);
97
- }
98
- }
99
- }
100
- const advisories = data.advisories;
101
- if (advisories && typeof advisories === 'object') {
102
- for (const entry of Object.values(advisories)) {
103
- if (!entry || typeof entry !== 'object') continue;
104
- const name = entry.module_name || entry.name;
105
- const severity = entry.severity;
106
- const raw = typeof entry.title === 'string' ? entry.title : '';
107
- const title = namedTitle(name, raw);
108
- const titles = title ? [title] : [];
109
- const fix = auditFix(entry);
110
- const range = typeof entry.range === 'string' ? entry.range : '';
111
- keepHighestAudit(audit, name, severity, fix, range, titles);
112
- }
113
- }
114
- return audit;
115
- };
116
-
117
- const parseOutdatedReport = (text) => {
118
- const outdated = new Map();
119
- if (!text || !`${text}`.trim()) return outdated;
120
- const data = jsonParse(text);
121
- if (!isHashObject(data)) return outdated;
122
- for (const name of Object.keys(data)) {
123
- const entry = data[name];
124
- if (!entry || typeof entry !== 'object') continue;
125
- const current = typeof entry.current === 'string' ? entry.current : '';
126
- const wanted = typeof entry.wanted === 'string' ? entry.wanted : '';
127
- const latest = typeof entry.latest === 'string' ? entry.latest : '';
128
- const type = typeof entry.type === 'string' ? entry.type : '';
129
- if (!wanted) continue;
130
- outdated.set(name, { current, wanted, latest, type });
131
- }
132
- return outdated;
133
- };
134
-
135
- const markAuditChanges = (changes, audit) => {
136
- if (!audit) return changes;
137
- const marked = [];
138
- for (const change of changes) {
139
- if (change.action === 'removed' || change.section === 'field') {
140
- marked.push(change);
141
- continue;
142
- }
143
- const found = audit.get(change.name);
144
- if (!found) {
145
- marked.push(change);
146
- continue;
147
- }
148
- marked.push({ ...change, audit: found });
149
- }
150
- return marked;
151
- };
152
-
153
- const markOutdatedChanges = (changes, outdated) => {
154
- if (!outdated) return changes;
155
- const marked = [];
156
- for (const change of changes) {
157
- if (change.action === 'removed' || change.section === 'field') {
158
- marked.push(change);
159
- continue;
160
- }
161
- const found = outdated.get(change.name);
162
- if (!found) {
163
- marked.push(change);
164
- continue;
165
- }
166
- if (found.wanted && found.wanted === found.current) {
167
- marked.push(change);
168
- continue;
169
- }
170
- marked.push({ ...change, outdated: found });
171
- }
172
- return marked;
173
- };
174
-
175
- module.exports = {
176
- parseAuditReport,
177
- parseOutdatedReport,
178
- markAuditChanges,
179
- markOutdatedChanges,
180
- };
package/lib/deps/rows.js DELETED
@@ -1,176 +0,0 @@
1
- 'use strict';
2
-
3
- const { countSides, isCtxType } = require('../diff.js');
4
-
5
- const DEP_CAPTION = 'Dependencies in package.json & package-lock.json';
6
-
7
- const changeMarks = (change) => {
8
- const marks = [];
9
- if (change.unused) marks.push('unused');
10
- if (change.outdated && !change.propose) marks.push('outdated');
11
- if (change.audit) marks.push(change.audit.severity);
12
- return marks;
13
- };
14
-
15
- const changeSources = (change) => {
16
- const sources = [];
17
- if (change.unused) {
18
- sources.push('npm uninstall');
19
- return sources;
20
- }
21
- if (change.propose && change.outdated) sources.push('npm outdated');
22
- if (change.audit) sources.push('npm audit');
23
- return sources;
24
- };
25
-
26
- const sectionTitle = (section, action, marks, sources) => {
27
- const title = action === 'changed' ? 'version changed' : action;
28
- const kind = section.replace(/ies$/, 'y');
29
- let label = `${kind} ${title}`;
30
- if (section === 'field') label = `package.json ${title}`;
31
- if (section === 'resolved' && action === 'vulnerable') {
32
- label = 'lockfile vulnerable';
33
- } else if (section === 'resolved') {
34
- label = `lockfile resolved ${title}`;
35
- }
36
- if (marks.includes('unused')) {
37
- label = `${kind} unused`;
38
- const extra = [];
39
- for (const mark of marks) {
40
- if (mark !== 'unused') extra.push(mark);
41
- }
42
- if (extra.length) label = `${label}, ${extra.join(', ')}`;
43
- } else if (marks.length) {
44
- label = `${label}, ${marks.join(', ')}`;
45
- }
46
- if (!sources.length) return label;
47
- return `${sources.join(', ')}: ${label}`;
48
- };
49
-
50
- const entryText = (change, side) => {
51
- const version = side === 'old' ? change.from : change.to;
52
- return `"${change.name}": "${version}"`;
53
- };
54
-
55
- const ctxLine = (text) => {
56
- const type = 'ctx';
57
- return { type, text };
58
- };
59
-
60
- const warnLine = (text) => {
61
- const type = 'warn';
62
- return { type, text };
63
- };
64
-
65
- const noteLine = (text) => {
66
- const type = 'note';
67
- return { type, text };
68
- };
69
-
70
- const noteSepLine = () => {
71
- const type = 'noteSep';
72
- const text = '';
73
- return { type, text };
74
- };
75
-
76
- const changeLine = (type, change, side) => {
77
- const text = entryText(change, side);
78
- return { type, text };
79
- };
80
-
81
- const pushChangeLines = (rows, change) => {
82
- if (change.unused) {
83
- const side = change.action === 'added' ? 'new' : 'old';
84
- rows.push(changeLine('del', change, side));
85
- return;
86
- }
87
- if (change.action === 'added') {
88
- rows.push(changeLine('add', change, 'new'));
89
- return;
90
- }
91
- if (change.action === 'removed') {
92
- rows.push(changeLine('del', change, 'old'));
93
- return;
94
- }
95
- if (change.from === change.to) {
96
- rows.push(changeLine('ctx', change, 'old'));
97
- return;
98
- }
99
- rows.push(changeLine('del', change, 'old'));
100
- rows.push(changeLine('add', change, 'new'));
101
- };
102
-
103
- const lockCountText = (oldCount, newCount) => {
104
- if (oldCount === newCount) return '';
105
- return `lockfile packages ${oldCount} → ${newCount}`;
106
- };
107
-
108
- const auditNoteTexts = (audit) => {
109
- if (!audit) return [];
110
- const notes = [];
111
- if (Array.isArray(audit.titles)) {
112
- for (const note of audit.titles) {
113
- if (note && !notes.includes(note)) notes.push(note);
114
- }
115
- } else if (audit.title) {
116
- notes.push(audit.title);
117
- }
118
- const severity = audit.severity;
119
- if (!notes.length) return [`npm audit ${severity}`];
120
- const texts = [];
121
- for (const note of notes) {
122
- texts.push(`npm audit ${severity} ${note}`);
123
- }
124
- return texts;
125
- };
126
-
127
- const pushAuditNotes = (rows, audit) => {
128
- const texts = auditNoteTexts(audit);
129
- if (!texts.length) return;
130
- rows.push(ctxLine(''));
131
- let first = true;
132
- for (const text of texts) {
133
- if (!first) rows.push(noteSepLine());
134
- first = false;
135
- rows.push(noteLine(text));
136
- }
137
- };
138
-
139
- const buildRows = (change, oldCount, newCount) => {
140
- const rows = [ctxLine(DEP_CAPTION), ctxLine('')];
141
- if (change) {
142
- const marks = changeMarks(change);
143
- const sources = changeSources(change);
144
- const title = sectionTitle(change.section, change.action, marks, sources);
145
- const audited = sources.includes('npm audit');
146
- rows.push(audited ? warnLine(title) : ctxLine(title));
147
- rows.push(ctxLine(''));
148
- pushChangeLines(rows, change);
149
- pushAuditNotes(rows, change.audit);
150
- }
151
- const countLine = lockCountText(oldCount, newCount);
152
- if (countLine) {
153
- if (rows.at(-1).text !== '') rows.push(ctxLine(''));
154
- rows.push(ctxLine(countLine));
155
- }
156
- return rows;
157
- };
158
-
159
- const toHunkLine = (row, blockId) => {
160
- const type = row.type;
161
- const text = row.text;
162
- const noNl = false;
163
- const id = isCtxType(type) ? null : blockId;
164
- return { type, text, noNl, blockId: id };
165
- };
166
-
167
- const makeHunk = (rows, blockId) => {
168
- const lines = rows.map((row) => toHunkLine(row, blockId));
169
- const counts = countSides(lines);
170
- const oldCount = counts.oldCount;
171
- const newCount = counts.newCount;
172
- const header = `@@ -1,${oldCount} +1,${newCount} @@`;
173
- return { oldStart: 1, oldCount, newStart: 1, newCount, header, lines };
174
- };
175
-
176
- module.exports = { DEP_CAPTION, buildRows, makeHunk };
package/lib/deps/scan.js DELETED
@@ -1,138 +0,0 @@
1
- 'use strict';
2
-
3
- const fs = require('node:fs');
4
- const path = require('node:path');
5
-
6
- const manifest = require('./manifest.js');
7
- const { MANIFEST, SECTION_KEYS, parseJson, readSections } = manifest;
8
-
9
- const usage = require('./usage.js');
10
- const { SOURCE_EXT, collectUsedFromText, collectUsedFromScripts } = usage;
11
- const { pkgHasExportEntry } = usage;
12
-
13
- const SKIP_WALK = ['node_modules', '.git', '.review', 'coverage', 'dist'];
14
-
15
- const collectUsedFromManifest = (used, rel) => {
16
- let text;
17
- try {
18
- text = fs.readFileSync(rel, 'utf8');
19
- } catch {
20
- return;
21
- }
22
- const pkg = parseJson(text);
23
- if (!pkg) return;
24
- collectUsedFromScripts(used, pkg.scripts);
25
- };
26
-
27
- const walkUsedNames = (used, dir) => {
28
- let entries;
29
- try {
30
- entries = fs.readdirSync(dir, { withFileTypes: true });
31
- } catch {
32
- return;
33
- }
34
- for (const entry of entries) {
35
- if (entry.isSymbolicLink()) continue;
36
- const full = path.join(dir, entry.name);
37
- if (entry.isDirectory()) {
38
- if (SKIP_WALK.includes(entry.name)) continue;
39
- if (entry.name.startsWith('.')) continue;
40
- walkUsedNames(used, full);
41
- continue;
42
- }
43
- if (entry.name === MANIFEST) {
44
- collectUsedFromManifest(used, full);
45
- continue;
46
- }
47
- const ext = path.extname(entry.name);
48
- if (!SOURCE_EXT.includes(ext)) continue;
49
- let text;
50
- try {
51
- text = fs.readFileSync(full, 'utf8');
52
- } catch {
53
- continue;
54
- }
55
- collectUsedFromText(used, text);
56
- }
57
- };
58
-
59
- const collectUsedNames = (root) => {
60
- const used = new Set();
61
- if (!root) return used;
62
- walkUsedNames(used, root);
63
- return used;
64
- };
65
-
66
- const depModuleParts = (name) => {
67
- if (!name.startsWith('@')) return [name];
68
- const parts = name.split('/');
69
- if (parts.length < 2) return [];
70
- return parts.slice(0, 2);
71
- };
72
-
73
- const readDepPkg = (root, pkgDir, name) => {
74
- if (!root || !name) return null;
75
- const parts = depModuleParts(name);
76
- if (!parts.length) return null;
77
- const nested = pkgDir && pkgDir !== '.' ? pkgDir : '';
78
- let dir = nested ? path.join(root, nested) : root;
79
- while (true) {
80
- const file = path.join(dir, 'node_modules', ...parts, MANIFEST);
81
- let text = '';
82
- try {
83
- text = fs.readFileSync(file, 'utf8');
84
- } catch {
85
- // Missing node_modules copy; try a parent directory.
86
- }
87
- if (text) return parseJson(text);
88
- const parent = path.dirname(dir);
89
- if (parent === dir) return null;
90
- dir = parent;
91
- }
92
- };
93
-
94
- const depHasExportEntry = (root, pkgDir, name) => {
95
- const pkg = readDepPkg(root, pkgDir, name);
96
- if (!pkg) return true;
97
- return pkgHasExportEntry(pkg);
98
- };
99
-
100
- const collectExportEntries = (root, pkgDir, names) => {
101
- const entries = new Map();
102
- if (!root) return entries;
103
- for (const name of names) {
104
- const hasExport = depHasExportEntry(root, pkgDir, name);
105
- entries.set(name, hasExport);
106
- }
107
- return entries;
108
- };
109
-
110
- const declaredDepNames = (root, pkgDir) => {
111
- const nested = pkgDir && pkgDir !== '.' ? pkgDir : '';
112
- const file = path.join(root, nested, MANIFEST);
113
- let text;
114
- try {
115
- text = fs.readFileSync(file, 'utf8');
116
- } catch {
117
- return [];
118
- }
119
- const maps = readSections(parseJson(text));
120
- const names = [];
121
- for (const key of SECTION_KEYS) {
122
- for (const name of maps[key].keys()) names.push(name);
123
- }
124
- return names;
125
- };
126
-
127
- const collectExportMeta = (root, extra = {}) => {
128
- if (extra.exportEntries !== undefined) return extra.exportEntries;
129
- const pkgDir = extra.dir ?? '.';
130
- const names = extra.names ?? declaredDepNames(root, pkgDir);
131
- return collectExportEntries(root, pkgDir, names);
132
- };
133
-
134
- module.exports = {
135
- collectUsedNames,
136
- collectExportEntries,
137
- collectExportMeta,
138
- };
package/lib/deps/usage.js DELETED
@@ -1,68 +0,0 @@
1
- 'use strict';
2
-
3
- const SOURCE_EXT = ['.js', '.mjs', '.cjs', '.ts', '.mts', '.cts'];
4
- const REQUIRE_SPEC = /require\(\s*['"]([^'"]+)['"]\s*\)/g;
5
- const FROM_SPEC = /\bfrom\s+['"]([^'"]+)['"]/g;
6
- const IMPORT_FILE = /import\s+['"]([^'"]+)['"]/g;
7
- const IMPORT_CALL = /import\(\s*['"]([^'"]+)['"]\s*\)/g;
8
-
9
- const toPackageName = (spec) => {
10
- if (!spec || spec.startsWith('.') || spec.startsWith('/')) return '';
11
- if (spec.startsWith('node:')) return '';
12
- if (spec.startsWith('@')) {
13
- const parts = spec.split('/');
14
- if (parts.length < 2) return '';
15
- return `${parts[0]}/${parts[1]}`;
16
- }
17
- return spec.split('/')[0];
18
- };
19
-
20
- const addSpec = (used, spec) => {
21
- const name = toPackageName(spec);
22
- if (name) used.add(name);
23
- };
24
-
25
- const addMatches = (used, text, pattern) => {
26
- const matches = text.matchAll(pattern);
27
- for (const match of matches) addSpec(used, match[1]);
28
- };
29
-
30
- const collectUsedFromText = (used, text) => {
31
- addMatches(used, text, REQUIRE_SPEC);
32
- addMatches(used, text, FROM_SPEC);
33
- addMatches(used, text, IMPORT_FILE);
34
- addMatches(used, text, IMPORT_CALL);
35
- };
36
-
37
- const collectUsedFromScripts = (used, scripts) => {
38
- if (!scripts || typeof scripts !== 'object') return;
39
- for (const script of Object.values(scripts)) {
40
- const tokens = `${script}`.split(/[\s;&|]+/);
41
- for (const token of tokens) {
42
- if (!token || token.startsWith('-')) continue;
43
- if (token === 'npx' || token === 'npm' || token === 'node') continue;
44
- addSpec(used, token);
45
- }
46
- }
47
- };
48
-
49
- const pkgHasExportEntry = (pkg) => {
50
- if (!pkg || typeof pkg !== 'object') return false;
51
- const main = pkg.main;
52
- if (typeof main === 'string' && main) return true;
53
- const esm = pkg.module;
54
- if (typeof esm === 'string' && esm) return true;
55
- const exported = pkg.exports;
56
- if (typeof exported === 'string' && exported) return true;
57
- if (!exported || typeof exported !== 'object') return false;
58
- if (Array.isArray(exported)) return exported.length > 0;
59
- return Object.keys(exported).length > 0;
60
- };
61
-
62
- module.exports = {
63
- SOURCE_EXT,
64
- toPackageName,
65
- collectUsedFromText,
66
- collectUsedFromScripts,
67
- pkgHasExportEntry,
68
- };
@@ -1,119 +0,0 @@
1
- 'use strict';
2
-
3
- const { SECTION_KEYS } = require('./manifest.js');
4
-
5
- const applyWantedRange = (declared, wanted) => {
6
- if (!wanted) return declared ?? '';
7
- if (!declared) return wanted;
8
- if (/^(file:|git[+@:]|workspace:|https?:|npm:)/.test(declared)) {
9
- return declared;
10
- }
11
- if (declared.includes(' ') || declared.includes('|')) return declared;
12
- if (declared.includes('<') || declared.includes('>')) return declared;
13
- const prefix = declared.startsWith('^') || declared.startsWith('~');
14
- const mark = prefix ? declared[0] : '';
15
- return `${mark}${wanted}`;
16
- };
17
-
18
- const findDeclared = (pkg, name, type) => {
19
- if (type && SECTION_KEYS.includes(type)) {
20
- const map = pkg[type];
21
- if (map && typeof map[name] === 'string') {
22
- return { section: type, version: map[name] };
23
- }
24
- }
25
- for (const section of SECTION_KEYS) {
26
- const map = pkg[section];
27
- if (map && typeof map[name] === 'string') {
28
- return { section, version: map[name] };
29
- }
30
- }
31
- return null;
32
- };
33
-
34
- const VER_CORE = /^(\d+)\.(\d+)\.(\d+)/;
35
-
36
- const parseVer = (text) => {
37
- const match = `${text}`.match(VER_CORE);
38
- if (!match) return null;
39
- const major = Number(match[1]);
40
- const minor = Number(match[2]);
41
- const patch = Number(match[3]);
42
- return [major, minor, patch];
43
- };
44
-
45
- const fmtVer = (parts) => `${parts[0]}.${parts[1]}.${parts[2]}`;
46
-
47
- const cmpVer = (left, right) => {
48
- if (left[0] !== right[0]) return left[0] - right[0];
49
- if (left[1] !== right[1]) return left[1] - right[1];
50
- return left[2] - right[2];
51
- };
52
-
53
- const bumpPatch = (parts) => {
54
- const major = parts[0];
55
- const minor = parts[1];
56
- const patch = parts[2] + 1;
57
- return [major, minor, patch];
58
- };
59
-
60
- const clauseUpper = (clause) => {
61
- const pattern = /(<=?)\s*(\d+\.\d+\.\d+)/g;
62
- const matches = clause.matchAll(pattern);
63
- let best = null;
64
- for (const match of matches) {
65
- const op = match[1];
66
- const parsed = parseVer(match[2]);
67
- if (!parsed) continue;
68
- if (best && cmpVer(parsed, best.ver) >= 0) continue;
69
- best = { op, ver: parsed };
70
- }
71
- return best;
72
- };
73
-
74
- const patchedFromRange = (current, range) => {
75
- if (!current || !range) return '';
76
- const now = parseVer(current);
77
- if (!now) return '';
78
- const clauses = `${range}`.split('||');
79
- for (const clause of clauses) {
80
- const upper = clauseUpper(clause);
81
- if (!upper) continue;
82
- if (now[0] !== upper.ver[0]) continue;
83
- if (cmpVer(now, upper.ver) > 0) continue;
84
- if (upper.op === '<') return fmtVer(upper.ver);
85
- return fmtVer(bumpPatch(upper.ver));
86
- }
87
- return '';
88
- };
89
-
90
- const sameMajor = (from, to) => {
91
- const left = parseVer(from);
92
- const right = parseVer(to);
93
- if (!left || !right) return false;
94
- return left[0] === right[0];
95
- };
96
-
97
- const proposedLockVersion = (current, vul) => {
98
- if (!current) return '';
99
- if (!vul) return current;
100
- const fix = vul.fix;
101
- if (fix && sameMajor(current, fix) && fix !== current) return fix;
102
- const patched = patchedFromRange(current, vul.range);
103
- if (patched && patched !== current) return patched;
104
- return current;
105
- };
106
-
107
- const proposedWanted = (out, vul) => {
108
- if (out && out.wanted && out.wanted !== out.current) return out.wanted;
109
- if (vul && vul.fix) return vul.fix;
110
- return '';
111
- };
112
-
113
- module.exports = {
114
- applyWantedRange,
115
- findDeclared,
116
- patchedFromRange,
117
- proposedLockVersion,
118
- proposedWanted,
119
- };