toolcrib 0.8.0 → 0.10.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.
- package/README.md +31 -0
- package/package.json +1 -1
- package/src/commands/exportPatches.js +283 -0
- package/src/commands/merge.js +66 -9
- package/src/index.js +11 -0
- package/src/lib/discussions.js +183 -0
- package/src/lib/exportFormat.js +224 -0
- package/src/lib/exportPatches.js +582 -0
- package/src/lib/git.js +48 -6
- package/src/lib/importScan.js +139 -0
- package/src/lib/patches.js +42 -26
- package/src/lib/zip.js +61 -0
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dependency-free import scanning, shared by the release build
|
|
3
|
+
* (scripts/build-release.js) and the CLI's own tooling so both see imports
|
|
4
|
+
* identically. Deliberately imports nothing: this file ships inside the
|
|
5
|
+
* published `toolcrib` npm package, and must work with zero dependencies.
|
|
6
|
+
*
|
|
7
|
+
* Pure string handling only. Not a parser: it will not see imports produced
|
|
8
|
+
* by macros, and an import-looking phrase inside a string literal is still
|
|
9
|
+
* matched (comments are the only thing stripped).
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Remove `//` line comments and `/* *\/` block comments from JS/TS source,
|
|
14
|
+
* leaving string literals ('...', "...", `...`, with backslash escapes)
|
|
15
|
+
* untouched. Newlines inside removed block comments are kept so line
|
|
16
|
+
* structure is roughly preserved. Backslash escapes in code are skipped
|
|
17
|
+
* as a pair so a regex literal like `/\/\//` isn't mistaken for a comment.
|
|
18
|
+
*
|
|
19
|
+
* @param {string} source
|
|
20
|
+
* @returns {string}
|
|
21
|
+
*/
|
|
22
|
+
export function stripComments(source) {
|
|
23
|
+
const n = source.length;
|
|
24
|
+
let out = '';
|
|
25
|
+
let i = 0;
|
|
26
|
+
while (i < n) {
|
|
27
|
+
const ch = source[i];
|
|
28
|
+
const next = source[i + 1];
|
|
29
|
+
|
|
30
|
+
if (ch === '/' && next === '/') {
|
|
31
|
+
i += 2;
|
|
32
|
+
while (i < n && source[i] !== '\n') i++;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (ch === '/' && next === '*') {
|
|
37
|
+
i += 2;
|
|
38
|
+
while (i < n && !(source[i] === '*' && source[i + 1] === '/')) {
|
|
39
|
+
if (source[i] === '\n') out += '\n';
|
|
40
|
+
i++;
|
|
41
|
+
}
|
|
42
|
+
i += 2;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (ch === '\\') {
|
|
47
|
+
out += source.slice(i, i + 2);
|
|
48
|
+
i += 2;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (ch === '"' || ch === "'" || ch === '`') {
|
|
53
|
+
let j = i + 1;
|
|
54
|
+
while (j < n && source[j] !== ch) {
|
|
55
|
+
if (source[j] === '\\') {
|
|
56
|
+
j += 2;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
// Single/double-quoted strings cannot span lines; stop at the newline
|
|
60
|
+
// so an apostrophe in JSX text can't swallow the rest of the file.
|
|
61
|
+
if (ch !== '`' && source[j] === '\n') break;
|
|
62
|
+
j++;
|
|
63
|
+
}
|
|
64
|
+
j = Math.min(j, n);
|
|
65
|
+
const closed = j < n && source[j] === ch;
|
|
66
|
+
const end = closed ? j + 1 : j;
|
|
67
|
+
out += source.slice(i, end);
|
|
68
|
+
i = end;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
out += ch;
|
|
73
|
+
i++;
|
|
74
|
+
}
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Module specifiers referenced by a source file, in source order, after
|
|
80
|
+
* comments are stripped. Covers static `import`/`export ... from`, bare
|
|
81
|
+
* side-effect `import 'x'`, `import type`, `export * as ns from`, and
|
|
82
|
+
* dynamic `import('x')` with a string-literal argument.
|
|
83
|
+
*
|
|
84
|
+
* @param {string} source
|
|
85
|
+
* @returns {string[]}
|
|
86
|
+
*/
|
|
87
|
+
export function extractImports(source) {
|
|
88
|
+
const code = stripComments(source);
|
|
89
|
+
const re = /\b(?:(?:import|export)\s+(?:[^'";]*?\sfrom\s+)?|import\s*\(\s*)['"]([^'"]+)['"]/g;
|
|
90
|
+
const specifiers = [];
|
|
91
|
+
let match;
|
|
92
|
+
while ((match = re.exec(code))) {
|
|
93
|
+
specifiers.push(match[1]);
|
|
94
|
+
}
|
|
95
|
+
return specifiers;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Resolve a relative import (`./` or `../` only) against the directory of
|
|
100
|
+
* the importing file, trying exact path, then the TS/JS extensions, then
|
|
101
|
+
* index files. Path handling is done by hand with forward slashes, never
|
|
102
|
+
* path.join/resolve, so the result is identical on Windows.
|
|
103
|
+
*
|
|
104
|
+
* @param {string} fromRelPath importing file, forward-slash, relative to the root (e.g. `components/Card/Card.tsx`)
|
|
105
|
+
* @param {string} specifier the import specifier as written
|
|
106
|
+
* @param {(relPath: string) => boolean} fileExists caller-supplied existence check, same root-relative form
|
|
107
|
+
* @returns {string | null} the first existing root-relative path, or null (non-relative, missing, or escaping the root)
|
|
108
|
+
*/
|
|
109
|
+
export function resolveRelativeImport(fromRelPath, specifier, fileExists) {
|
|
110
|
+
if (!specifier.startsWith('./') && !specifier.startsWith('../')) return null;
|
|
111
|
+
|
|
112
|
+
const segments = fromRelPath.split('/').slice(0, -1).filter((s) => s !== '');
|
|
113
|
+
for (const part of specifier.split('/')) {
|
|
114
|
+
if (part === '' || part === '.') continue;
|
|
115
|
+
if (part === '..') {
|
|
116
|
+
if (segments.length === 0) return null;
|
|
117
|
+
segments.pop();
|
|
118
|
+
} else {
|
|
119
|
+
segments.push(part);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const base = segments.join('/');
|
|
124
|
+
if (base === '') return null; // `./` alone names the root directory, not a file
|
|
125
|
+
const candidates = [
|
|
126
|
+
base,
|
|
127
|
+
`${base}.ts`,
|
|
128
|
+
`${base}.tsx`,
|
|
129
|
+
`${base}.js`,
|
|
130
|
+
`${base}.jsx`,
|
|
131
|
+
`${base}/index.ts`,
|
|
132
|
+
`${base}/index.tsx`,
|
|
133
|
+
`${base}/index.js`,
|
|
134
|
+
];
|
|
135
|
+
for (const candidate of candidates) {
|
|
136
|
+
if (fileExists(candidate)) return candidate;
|
|
137
|
+
}
|
|
138
|
+
return null;
|
|
139
|
+
}
|
package/src/lib/patches.js
CHANGED
|
@@ -37,6 +37,45 @@ export function normalize(content) {
|
|
|
37
37
|
return content.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
function buildPatchInfo(relPath, currentContent, proposedContent, label) {
|
|
41
|
+
relPath = relPath.replace(/\\/g, '/');
|
|
42
|
+
// label defaults to the *normalized* relPath — a default parameter
|
|
43
|
+
// expression (`label = relPath`) would capture the pre-normalization
|
|
44
|
+
// value instead, since defaults evaluate before the function body runs.
|
|
45
|
+
if (label === undefined) label = relPath;
|
|
46
|
+
if (normalize(currentContent) === normalize(proposedContent)) return null; // no-op
|
|
47
|
+
|
|
48
|
+
const isNewFile = currentContent === '';
|
|
49
|
+
// Symmetric with isNewFile above: a deletion is just a patch whose
|
|
50
|
+
// *target* is /dev/null instead of its *source* — git apply (confirmed
|
|
51
|
+
// directly against a real repo, not assumed) recognizes this from a
|
|
52
|
+
// plain unified diff exactly the same way it recognizes a new file from
|
|
53
|
+
// an empty source, no git-extended `diff --git`/`deleted file mode`
|
|
54
|
+
// headers required. The `!isNewFile` guard only rules out the
|
|
55
|
+
// '' -> '' case, already caught by the no-op check above; kept for
|
|
56
|
+
// clarity that a file can't be simultaneously new and deleted.
|
|
57
|
+
const isDeletedFile = proposedContent === '' && !isNewFile;
|
|
58
|
+
const patch = createTwoFilesPatch(
|
|
59
|
+
isNewFile ? '/dev/null' : `a/${relPath}`,
|
|
60
|
+
isDeletedFile ? '/dev/null' : `b/${relPath}`,
|
|
61
|
+
currentContent,
|
|
62
|
+
proposedContent,
|
|
63
|
+
isNewFile ? undefined : `${label} (current)`,
|
|
64
|
+
isDeletedFile ? undefined : isNewFile ? `${label} (new file)` : `${label} (proposed)`
|
|
65
|
+
);
|
|
66
|
+
return { patch, isNewFile, isDeletedFile };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Pure patch-text construction shared by PendingChanges.propose() and the
|
|
71
|
+
* export-patches command. Returns the git-apply-compatible unified diff
|
|
72
|
+
* string, or null when the two contents are equal after line-ending
|
|
73
|
+
* normalization. '' as current means a new file; '' as proposed, a deletion.
|
|
74
|
+
*/
|
|
75
|
+
export function buildPatch(relPath, currentContent, proposedContent, label) {
|
|
76
|
+
return buildPatchInfo(relPath, currentContent, proposedContent, label)?.patch ?? null;
|
|
77
|
+
}
|
|
78
|
+
|
|
40
79
|
/**
|
|
41
80
|
* Collects proposed file changes as unified diff patches. Nothing is ever
|
|
42
81
|
* written directly to a pre-existing or ambiguous target — every mutation
|
|
@@ -69,32 +108,9 @@ export class PendingChanges {
|
|
|
69
108
|
*/
|
|
70
109
|
propose(relPath, currentContent, proposedContent, label) {
|
|
71
110
|
relPath = relPath.replace(/\\/g, '/');
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
if (label === undefined) label = relPath;
|
|
76
|
-
if (normalize(currentContent) === normalize(proposedContent)) return; // no-op, nothing to propose
|
|
77
|
-
|
|
78
|
-
const isNewFile = currentContent === '';
|
|
79
|
-
// Symmetric with isNewFile above: a deletion is just a patch whose
|
|
80
|
-
// *target* is /dev/null instead of its *source* — git apply (confirmed
|
|
81
|
-
// directly against a real repo, not assumed) recognizes this from a
|
|
82
|
-
// plain unified diff exactly the same way it recognizes a new file from
|
|
83
|
-
// an empty source, no git-extended `diff --git`/`deleted file mode`
|
|
84
|
-
// headers required. The `!isNewFile` guard only rules out the
|
|
85
|
-
// '' -> '' case, already caught by the no-op check above; kept for
|
|
86
|
-
// clarity that a file can't be simultaneously new and deleted.
|
|
87
|
-
const isDeletedFile = proposedContent === '' && !isNewFile;
|
|
88
|
-
const patch = createTwoFilesPatch(
|
|
89
|
-
isNewFile ? '/dev/null' : `a/${relPath}`,
|
|
90
|
-
isDeletedFile ? '/dev/null' : `b/${relPath}`,
|
|
91
|
-
currentContent,
|
|
92
|
-
proposedContent,
|
|
93
|
-
isNewFile ? undefined : `${label} (current)`,
|
|
94
|
-
isDeletedFile ? undefined : isNewFile ? `${label} (new file)` : `${label} (proposed)`
|
|
95
|
-
);
|
|
96
|
-
|
|
97
|
-
this.entries.push({ relPath, patch, isNewFile, isDeletedFile });
|
|
111
|
+
const built = buildPatchInfo(relPath, currentContent, proposedContent, label);
|
|
112
|
+
if (!built) return; // no-op, nothing to propose
|
|
113
|
+
this.entries.push({ relPath, patch: built.patch, isNewFile: built.isNewFile, isDeletedFile: built.isDeletedFile });
|
|
98
114
|
}
|
|
99
115
|
|
|
100
116
|
isEmpty() {
|
package/src/lib/zip.js
CHANGED
|
@@ -29,7 +29,68 @@ export function powerShellQuote(value) {
|
|
|
29
29
|
return `'${value.replace(/'/g, "''")}'`;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Read every entry name out of a zip's central directory, in plain Node.
|
|
34
|
+
* Throws if the buffer isn't a readable zip. Zip64 archives (the 0xFFFF /
|
|
35
|
+
* 0xFFFFFFFF sentinels) are rejected rather than guessed at: a toolcrib
|
|
36
|
+
* release is a few hundred small files.
|
|
37
|
+
*/
|
|
38
|
+
export function listZipEntryNames(zipBuffer) {
|
|
39
|
+
const EOCD_SIG = 0x06054b50;
|
|
40
|
+
const CEN_SIG = 0x02014b50;
|
|
41
|
+
// The end-of-central-directory record is 22 bytes plus an optional comment
|
|
42
|
+
// of up to 65535 bytes, so it starts somewhere in the last ~64 KiB.
|
|
43
|
+
const searchFrom = Math.max(0, zipBuffer.length - 22 - 0xffff);
|
|
44
|
+
let eocd = -1;
|
|
45
|
+
for (let i = zipBuffer.length - 22; i >= searchFrom; i--) {
|
|
46
|
+
if (zipBuffer.readUInt32LE(i) === EOCD_SIG) {
|
|
47
|
+
eocd = i;
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (eocd < 0) throw new Error('Not a readable zip archive (no end-of-central-directory record).');
|
|
52
|
+
|
|
53
|
+
const entryCount = zipBuffer.readUInt16LE(eocd + 10);
|
|
54
|
+
const centralOffset = zipBuffer.readUInt32LE(eocd + 16);
|
|
55
|
+
if (entryCount === 0xffff || centralOffset === 0xffffffff) {
|
|
56
|
+
throw new Error('Zip64 archives are not supported.');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const names = [];
|
|
60
|
+
let pos = centralOffset;
|
|
61
|
+
for (let n = 0; n < entryCount; n++) {
|
|
62
|
+
if (pos + 46 > zipBuffer.length || zipBuffer.readUInt32LE(pos) !== CEN_SIG) {
|
|
63
|
+
throw new Error('Corrupt zip central directory.');
|
|
64
|
+
}
|
|
65
|
+
const nameLen = zipBuffer.readUInt16LE(pos + 28);
|
|
66
|
+
const extraLen = zipBuffer.readUInt16LE(pos + 30);
|
|
67
|
+
const commentLen = zipBuffer.readUInt16LE(pos + 32);
|
|
68
|
+
names.push(zipBuffer.toString('utf8', pos + 46, pos + 46 + nameLen));
|
|
69
|
+
pos += 46 + nameLen + extraLen + commentLen;
|
|
70
|
+
}
|
|
71
|
+
return names;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Refuse an archive with an entry that would extract outside the target
|
|
76
|
+
* directory (zip-slip): an absolute path, a drive letter, or a `..` segment,
|
|
77
|
+
* with either separator (a backslash is a path separator to Windows'
|
|
78
|
+
* Expand-Archive but a filename character to unzip, so both are checked).
|
|
79
|
+
* Done before extraction rather than trusting the platform tool, because
|
|
80
|
+
* Windows PowerShell 5.1's Expand-Archive has historically been weaker here
|
|
81
|
+
* than modern unzip (#831).
|
|
82
|
+
*/
|
|
83
|
+
export function assertSafeZipEntries(zipBuffer) {
|
|
84
|
+
for (const name of listZipEntryNames(zipBuffer)) {
|
|
85
|
+
const segments = name.split(/[\\/]/);
|
|
86
|
+
if (name.startsWith('/') || name.startsWith('\\') || /^[A-Za-z]:/.test(name) || name.includes('\0') || segments.includes('..')) {
|
|
87
|
+
throw new Error(`Refusing to extract an archive with an unsafe entry path: ${name}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
32
92
|
export async function extractZip(zipBuffer, targetDir) {
|
|
93
|
+
assertSafeZipEntries(zipBuffer);
|
|
33
94
|
await fsp.mkdir(targetDir, { recursive: true });
|
|
34
95
|
|
|
35
96
|
// A bare `Date.now()`-keyed filename collides under real concurrent use:
|