@graftsync/adapter-patch 0.0.0-stage → 0.1.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.
@@ -0,0 +1,28 @@
1
+ import { type StructuredPatch, type StructuredPatchHunk } from 'diff';
2
+ export interface FilePatch {
3
+ /** Path relative to the package root, or null for a created file. */
4
+ oldFile: string | null;
5
+ /** Path relative to the package root, or null for a deleted file. */
6
+ newFile: string | null;
7
+ hunks: StructuredPatchHunk[];
8
+ raw: StructuredPatch;
9
+ }
10
+ /** Parse a git-style unified patch (as written by `pnpm patch-commit` or `git diff`). */
11
+ export declare function parsePatchFile(text: string): FilePatch[];
12
+ /** The file a patch entry reads from (or creates). */
13
+ export declare const patchTarget: (p: FilePatch) => string;
14
+ export interface HunkMatch {
15
+ /** 1-based line where the hunk's old side was found. */
16
+ line?: number;
17
+ /** Distance from the line stated in the hunk header. */
18
+ offset?: number;
19
+ /** Number of places the hunk's old side occurs. */
20
+ occurrences: number;
21
+ }
22
+ /** Locate a hunk's old side (context + removed lines) exactly in `content`. */
23
+ export declare function locateHunk(content: string, hunk: StructuredPatchHunk): HunkMatch;
24
+ /** Apply one file's patch with no fuzz. Returns false if any hunk does not fit exactly. */
25
+ export declare function applyFilePatch(content: string, patch: FilePatch): string | false;
26
+ /** Short human description of a hunk: its first changed line. */
27
+ export declare function describeHunk(hunk: StructuredPatchHunk): string;
28
+ //# sourceMappingURL=patchfile.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"patchfile.d.ts","sourceRoot":"","sources":["../src/patchfile.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,KAAK,eAAe,EAAE,KAAK,mBAAmB,EAAE,MAAM,MAAM,CAAC;AAE9F,MAAM,WAAW,SAAS;IACxB,qEAAqE;IACrE,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,qEAAqE;IACrE,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,KAAK,EAAE,mBAAmB,EAAE,CAAC;IAC7B,GAAG,EAAE,eAAe,CAAC;CACtB;AAOD,yFAAyF;AACzF,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,EAAE,CAIxD;AAED,sDAAsD;AACtD,eAAO,MAAM,WAAW,MAAO,SAAS,KAAG,MAAmC,CAAC;AAE/E,MAAM,WAAW,SAAS;IACxB,wDAAwD;IACxD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,wDAAwD;IACxD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,mDAAmD;IACnD,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,+EAA+E;AAC/E,wBAAgB,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,mBAAmB,GAAG,SAAS,CAYhF;AAED,2FAA2F;AAC3F,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,GAAG,MAAM,GAAG,KAAK,CAEhF;AAED,iEAAiE;AACjE,wBAAgB,YAAY,CAAC,IAAI,EAAE,mBAAmB,GAAG,MAAM,CAI9D"}
@@ -0,0 +1,43 @@
1
+ import { applyPatch, parsePatch } from 'diff';
2
+ const strip = (name) => {
3
+ if (!name || name === '/dev/null')
4
+ return null;
5
+ return name.replace(/^[ab]\//, '').replace(/\t.*$/, '');
6
+ };
7
+ /** Parse a git-style unified patch (as written by `pnpm patch-commit` or `git diff`). */
8
+ export function parsePatchFile(text) {
9
+ return parsePatch(text)
10
+ .filter((p) => p.hunks.length > 0 || p.oldFileName || p.newFileName)
11
+ .map((raw) => ({ oldFile: strip(raw.oldFileName), newFile: strip(raw.newFileName), hunks: raw.hunks, raw }));
12
+ }
13
+ /** The file a patch entry reads from (or creates). */
14
+ export const patchTarget = (p) => (p.oldFile ?? p.newFile);
15
+ /** Locate a hunk's old side (context + removed lines) exactly in `content`. */
16
+ export function locateHunk(content, hunk) {
17
+ const oldLines = hunk.lines.filter((l) => l[0] === ' ' || l[0] === '-').map((l) => l.slice(1));
18
+ const lines = content.split('\n');
19
+ if (oldLines.length === 0)
20
+ return { line: hunk.oldStart, offset: 0, occurrences: 1 };
21
+ const hits = [];
22
+ outer: for (let i = 0; i + oldLines.length <= lines.length; i++) {
23
+ for (let j = 0; j < oldLines.length; j++)
24
+ if (lines[i + j] !== oldLines[j])
25
+ continue outer;
26
+ hits.push(i + 1);
27
+ }
28
+ if (!hits.length)
29
+ return { occurrences: 0 };
30
+ const best = hits.reduce((a, b) => (Math.abs(b - hunk.oldStart) < Math.abs(a - hunk.oldStart) ? b : a));
31
+ return { line: best, offset: best - hunk.oldStart, occurrences: hits.length };
32
+ }
33
+ /** Apply one file's patch with no fuzz. Returns false if any hunk does not fit exactly. */
34
+ export function applyFilePatch(content, patch) {
35
+ return applyPatch(content, patch.raw, { fuzzFactor: 0 });
36
+ }
37
+ /** Short human description of a hunk: its first changed line. */
38
+ export function describeHunk(hunk) {
39
+ const first = hunk.lines.find((l) => l[0] === '+' || l[0] === '-') ?? '';
40
+ const text = first.slice(1).trim();
41
+ return `${first[0] === '-' ? 'replaces' : 'adds'} \`${text.length > 70 ? `${text.slice(0, 67)}…` : text}\``;
42
+ }
43
+ //# sourceMappingURL=patchfile.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"patchfile.js","sourceRoot":"","sources":["../src/patchfile.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAkD,MAAM,MAAM,CAAC;AAW9F,MAAM,KAAK,GAAG,CAAC,IAAwB,EAAiB,EAAE;IACxD,IAAI,CAAC,IAAI,IAAI,IAAI,KAAK,WAAW;QAAE,OAAO,IAAI,CAAC;IAC/C,OAAO,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;AAC1D,CAAC,CAAC;AAEF,yFAAyF;AACzF,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,OAAO,UAAU,CA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,GAAG,CAAC,IAAI,EAAE,CAAC;IACzE,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IACnC,OAAO,GAAG,KAAK,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,MAAM,MAAM,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;AAC9G,CAAC"}
package/dist/pnpm.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ export declare const WORKSPACE_FILE = "pnpm-workspace.yaml";
2
+ /** Nearest directory at or above `from` containing pnpm-workspace.yaml. */
3
+ export declare function findPnpmWorkspaceRoot(from: string): string | undefined;
4
+ export interface PnpmPatchState {
5
+ workspaceRoot: string;
6
+ /** Value of patchedDependencies keys that apply to `pkg` (name-only or name@version). */
7
+ entries: {
8
+ key: string;
9
+ path: string;
10
+ }[];
11
+ ignorePatchFailures: unknown;
12
+ /**
13
+ * Entries for `pkg` in the root package.json's `pnpm.patchedDependencies` (where
14
+ * `pnpm patch-commit` writes when that field exists). pnpm honors these too.
15
+ */
16
+ manifestEntries: {
17
+ key: string;
18
+ path: string;
19
+ }[];
20
+ }
21
+ export declare function readPnpmPatchState(root: string, pkg: string): Promise<PnpmPatchState | undefined>;
22
+ /** Problems that would make pnpm apply something other than `variantPath` to `pkg`, or fail silently. */
23
+ export declare function pnpmConfigProblems(root: string, pkg: string, variantPath: string): Promise<string[]>;
24
+ /** Point patchedDependencies[pkg] at `variantPath` and enforce ignorePatchFailures: false. Preserves comments. */
25
+ export declare function configurePnpmPatch(root: string, pkg: string, variantPath: string): Promise<{
26
+ file: string;
27
+ changed: boolean;
28
+ }>;
29
+ //# sourceMappingURL=pnpm.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pnpm.d.ts","sourceRoot":"","sources":["../src/pnpm.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,cAAc,wBAAwB,CAAC;AAEpD,2EAA2E;AAC3E,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAQtE;AAED,MAAM,WAAW,cAAc;IAC7B,aAAa,EAAE,MAAM,CAAC;IACtB,yFAAyF;IACzF,OAAO,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IACzC,mBAAmB,EAAE,OAAO,CAAC;IAC7B;;;OAGG;IACH,eAAe,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;CAClD;AAWD,wBAAsB,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,GAAG,SAAS,CAAC,CAmBvG;AAED,yGAAyG;AACzG,wBAAsB,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAqB1G;AAED,kHAAkH;AAClH,wBAAsB,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC,CAoCpI"}
package/dist/pnpm.js ADDED
@@ -0,0 +1,113 @@
1
+ import { existsSync } from 'node:fs';
2
+ import { readFile, writeFile } from 'node:fs/promises';
3
+ import path from 'node:path';
4
+ import { parseDocument } from 'yaml';
5
+ export const WORKSPACE_FILE = 'pnpm-workspace.yaml';
6
+ /** Nearest directory at or above `from` containing pnpm-workspace.yaml. */
7
+ export function findPnpmWorkspaceRoot(from) {
8
+ let dir = path.resolve(from);
9
+ for (;;) {
10
+ if (existsSync(path.join(dir, WORKSPACE_FILE)))
11
+ return dir;
12
+ const parent = path.dirname(dir);
13
+ if (parent === dir)
14
+ return undefined;
15
+ dir = parent;
16
+ }
17
+ }
18
+ async function readManifest(workspaceRoot) {
19
+ const file = path.join(workspaceRoot, 'package.json');
20
+ if (!existsSync(file))
21
+ return undefined;
22
+ const text = await readFile(file, 'utf8');
23
+ return { file, text, json: JSON.parse(text) };
24
+ }
25
+ const keyMatches = (key, pkg) => key === pkg || (key.startsWith(`${pkg}@`) && key.length > pkg.length + 1);
26
+ export async function readPnpmPatchState(root, pkg) {
27
+ const workspaceRoot = findPnpmWorkspaceRoot(root);
28
+ if (!workspaceRoot)
29
+ return undefined;
30
+ const doc = parseDocument(await readFile(path.join(workspaceRoot, WORKSPACE_FILE), 'utf8'));
31
+ const patched = doc.get('patchedDependencies', false)?.toJSON?.();
32
+ const manifest = await readManifest(workspaceRoot);
33
+ const fromManifest = (manifest?.json.pnpm?.patchedDependencies ?? {});
34
+ return {
35
+ workspaceRoot,
36
+ entries: Object.entries(patched ?? {})
37
+ .filter(([key]) => keyMatches(key, pkg))
38
+ .map(([key, p]) => ({ key, path: p })),
39
+ ignorePatchFailures: doc.get('ignorePatchFailures'),
40
+ manifestEntries: Object.entries(fromManifest)
41
+ .filter(([key]) => keyMatches(key, pkg))
42
+ .map(([key, p]) => ({ key, path: p })),
43
+ };
44
+ }
45
+ /** Problems that would make pnpm apply something other than `variantPath` to `pkg`, or fail silently. */
46
+ export async function pnpmConfigProblems(root, pkg, variantPath) {
47
+ const state = await readPnpmPatchState(root, pkg);
48
+ if (!state)
49
+ return [`no ${WORKSPACE_FILE} found; pnpm applies grafts through its patchedDependencies — run \`graft apply\` to create it`];
50
+ const problems = [];
51
+ const expected = relativePosix(state.workspaceRoot, variantPath);
52
+ if (state.ignorePatchFailures !== false) {
53
+ problems.push(`${WORKSPACE_FILE} must set \`ignorePatchFailures: false\`, otherwise pnpm only warns when a patch stops applying`);
54
+ }
55
+ if (!state.entries.length)
56
+ problems.push(`patchedDependencies has no entry for ${pkg} (expected ${pkg}: ${expected})`);
57
+ for (const e of state.manifestEntries) {
58
+ problems.push(`package.json pnpm.patchedDependencies["${e.key}"] (${e.path}) also patches ${pkg}; graft manages it in ${WORKSPACE_FILE} — run \`graft apply\` to move it`);
59
+ }
60
+ for (const e of state.entries) {
61
+ if (e.key !== pkg)
62
+ problems.push(`patchedDependencies["${e.key}"] is version-pinned; graft manages ${pkg} with a name-only key`);
63
+ else if (path.resolve(state.workspaceRoot, e.path) !== path.resolve(variantPath)) {
64
+ problems.push(`patchedDependencies["${pkg}"] points to ${e.path}, but the selected variant is ${expected}`);
65
+ }
66
+ }
67
+ return problems;
68
+ }
69
+ /** Point patchedDependencies[pkg] at `variantPath` and enforce ignorePatchFailures: false. Preserves comments. */
70
+ export async function configurePnpmPatch(root, pkg, variantPath) {
71
+ // A project can be pnpm (e.g. via packageManager) without a workspace file yet.
72
+ // pnpm 10 accepts a settings-only pnpm-workspace.yaml for a single-package project.
73
+ const workspaceRoot = findPnpmWorkspaceRoot(root) ?? path.resolve(root);
74
+ const file = path.join(workspaceRoot, WORKSPACE_FILE);
75
+ const before = existsSync(file) ? await readFile(file, 'utf8') : '';
76
+ const doc = parseDocument(before);
77
+ const patched = doc.get('patchedDependencies', true);
78
+ for (const item of patched?.items ?? []) {
79
+ const key = String(item.key?.value ?? item.key);
80
+ if (key !== pkg && keyMatches(key, pkg))
81
+ doc.deleteIn(['patchedDependencies', key]);
82
+ }
83
+ doc.setIn(['patchedDependencies', pkg], relativePosix(workspaceRoot, variantPath));
84
+ doc.set('ignorePatchFailures', false);
85
+ const after = doc.toString();
86
+ if (after !== before)
87
+ await writeFile(file, after);
88
+ // Remove competing entries `pnpm patch-commit` may have written to package.json.
89
+ let manifestChanged = false;
90
+ const manifest = await readManifest(workspaceRoot);
91
+ const patchedInManifest = manifest?.json.pnpm?.patchedDependencies;
92
+ if (manifest && patchedInManifest) {
93
+ for (const key of Object.keys(patchedInManifest)) {
94
+ if (keyMatches(key, pkg)) {
95
+ delete patchedInManifest[key];
96
+ manifestChanged = true;
97
+ }
98
+ }
99
+ if (manifestChanged) {
100
+ if (!Object.keys(patchedInManifest).length)
101
+ delete manifest.json.pnpm.patchedDependencies;
102
+ if (!Object.keys(manifest.json.pnpm).length)
103
+ delete manifest.json.pnpm;
104
+ const indent = /^[ \t]+/m.exec(manifest.text)?.[0] ?? ' ';
105
+ await writeFile(manifest.file, `${JSON.stringify(manifest.json, null, indent)}\n`);
106
+ }
107
+ }
108
+ return { file, changed: after !== before || manifestChanged };
109
+ }
110
+ function relativePosix(from, to) {
111
+ return path.relative(from, to).split(path.sep).join('/');
112
+ }
113
+ //# sourceMappingURL=pnpm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pnpm.js","sourceRoot":"","sources":["../src/pnpm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,MAAM,MAAM,CAAC;AAErC,MAAM,CAAC,MAAM,cAAc,GAAG,qBAAqB,CAAC;AAEpD,2EAA2E;AAC3E,MAAM,UAAU,qBAAqB,CAAC,IAAY;IAChD,IAAI,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B,SAAS,CAAC;QACR,IAAI,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,cAAc,CAAC,CAAC;YAAE,OAAO,GAAG,CAAC;QAC3D,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QACjC,IAAI,MAAM,KAAK,GAAG;YAAE,OAAO,SAAS,CAAC;QACrC,GAAG,GAAG,MAAM,CAAC;IACf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,CAAC;YACpB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC,MAAM;gBAAE,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,mBAAmB,CAAC;YAC1F,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM;gBAAE,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC;YACvE,MAAM,MAAM,GAAG,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;YAC3D,MAAM,SAAS,CAAC,QAAQ,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;QACrF,CAAC;IACH,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,KAAK,MAAM,IAAI,eAAe,EAAE,CAAC;AAChE,CAAC;AAED,SAAS,aAAa,CAAC,IAAY,EAAE,EAAU;IAC7C,OAAO,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC3D,CAAC"}
@@ -0,0 +1,43 @@
1
+ import { type UpstreamSource } from '@graftsync/core';
2
+ export declare const VENDOR_DIR = ".grafts/.vendor";
3
+ export declare const vendorFileName: (pkg: string, version: string) => string;
4
+ /** Contents of a .tgz as path → bytes (for content, not byte, comparison across platforms). */
5
+ export declare function tarballContents(file: string): Promise<Map<string, Buffer>>;
6
+ /** Build pristine + variant into a temp .tgz; returns its path (caller removes the temp dir). */
7
+ export declare function buildVendorTarball(pristineDir: string, variantPath: string): Promise<{
8
+ file: string;
9
+ tmp: string;
10
+ }>;
11
+ export interface VendorContext {
12
+ upstream?: UpstreamSource;
13
+ /** Directory containing .grafts/ (the vendored tarballs live under it). */
14
+ graftRoot: string;
15
+ /** Directory with the package.json that declares overrides (install root). */
16
+ installRoot: string;
17
+ }
18
+ /** Builds the expected tarball into a temp dir (the caller removes `tmp`). */
19
+ export type TarballBuilder = () => Promise<{
20
+ file: string;
21
+ tmp: string;
22
+ }>;
23
+ export declare function vendorProblems(ctx: VendorContext, pkg: string, version: string, variantPath: string): Promise<string[]>;
24
+ /**
25
+ * Problems with installing `pkg@version` from a vendored tarball through overrides: the
26
+ * tarball is missing or stale (when `build` is given, its contents must equal a fresh build,
27
+ * described by `expected`), overrides / root dependency / npm lock entries point elsewhere, or
28
+ * a competing patch is configured. Shared by the vendor applier and the fork mechanism.
29
+ */
30
+ export declare function vendoredTarballProblems(ctx: VendorContext, pkg: string, version: string, build: TarballBuilder | undefined, expected: string): Promise<string[]>;
31
+ export declare function vendorConfigure(ctx: VendorContext, pkg: string, version: string, variantPath: string): Promise<{
32
+ files: string[];
33
+ }>;
34
+ /**
35
+ * Commit a freshly built tarball as `.grafts/.vendor/<pkg>-<version>.tgz` (keeping the old bytes
36
+ * when contents are unchanged, so lockfile integrity is stable), remove other versions' tarballs,
37
+ * and point overrides, the root dependency and npm's lockfile at it. Shared by the vendor applier
38
+ * and the fork mechanism.
39
+ */
40
+ export declare function installVendoredTarball(ctx: VendorContext, pkg: string, version: string, build: TarballBuilder): Promise<{
41
+ files: string[];
42
+ }>;
43
+ //# sourceMappingURL=vendor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vendor.d.ts","sourceRoot":"","sources":["../src/vendor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAwB,KAAK,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAK5E,eAAO,MAAM,UAAU,oBAAoB,CAAC;AAK5C,eAAO,MAAM,cAAc,QAAS,MAAM,WAAW,MAAM,WAA8C,CAAC;AAc1G,+FAA+F;AAC/F,wBAAsB,eAAe,CAAC,IAAI,EAAE,M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E,KAAK,EAAE,cAAc,GAAG,OAAO,CAAC;IAAE,KAAK,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC,CAwDlJ"}
package/dist/vendor.js ADDED
@@ -0,0 +1,300 @@
1
+ // The vendored override applier: build pristine upstream + the selected variant into a
2
+ // tarball committed under .grafts/.vendor/, and point the package manager's overrides (and
3
+ // the root's own dependency) at it. The package manager then installs the patched package as
4
+ // an ordinary dependency: recorded with its integrity in the lockfile, surviving fresh
5
+ // installs, `--ignore-scripts`, and "repair"/verify passes, with no install hooks at all.
6
+ // It is the only applier that works with upm, which never runs the project's postinstall and
7
+ // repairs edited package files.
8
+ import { existsSync } from 'node:fs';
9
+ import { cp, mkdir, mkdtemp, readdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
10
+ import os from 'node:os';
11
+ import path from 'node:path';
12
+ import { parseDocument } from 'yaml';
13
+ import { create as tarCreate, list as tarList } from 'tar';
14
+ import { detectPackageManager } from '@graftsync/core';
15
+ import { applyToInstalledCopy } from './installed.js';
16
+ import { parsePatchFile } from './patchfile.js';
17
+ import { WORKSPACE_FILE } from './pnpm.js';
18
+ export const VENDOR_DIR = '.grafts/.vendor';
19
+ /** Fixed mtime for every tarball entry, so rebuilding identical contents gives identical bytes. */
20
+ const FIXED_MTIME = new Date('2000-01-01T00:00:00Z');
21
+ const DEP_FIELDS = ['dependencies', 'devDependencies', 'optionalDependencies'];
22
+ export const vendorFileName = (pkg, version) => `${pkg.replace('/', '+')}-${version}.tgz`;
23
+ const vendorPrefix = (pkg) => `${pkg.replace('/', '+')}-`;
24
+ const posix = (p) => p.split(path.sep).join('/');
25
+ async function listFiles(dir, rel = '') {
26
+ const out = [];
27
+ for (const e of await readdir(path.join(dir, rel), { withFileTypes: true })) {
28
+ const child = rel ? `${rel}/${e.name}` : e.name;
29
+ if (e.isDirectory())
30
+ out.push(...(await listFiles(dir, child)));
31
+ else if (e.isFile())
32
+ out.push(child);
33
+ }
34
+ return out.sort();
35
+ }
36
+ /** Contents of a .tgz as path → bytes (for content, not byte, comparison across platforms). */
37
+ export async function tarballContents(file) {
38
+ const entries = new Map();
39
+ await tarList({
40
+ file,
41
+ onReadEntry: (entry) => {
42
+ const chunks = [];
43
+ entry.on('data', (c) => chunks.push(c));
44
+ entry.on('end', () => {
45
+ if (entry.type === 'File')
46
+ entries.set(entry.path, Buffer.concat(chunks));
47
+ });
48
+ },
49
+ });
50
+ return entries;
51
+ }
52
+ const sameContents = (a, b) => a.size === b.size && [...a].every(([k, v]) => b.get(k)?.equals(v));
53
+ /** Build pristine + variant into a temp .tgz; returns its path (caller removes the temp dir). */
54
+ export async function buildVendorTarball(pristineDir, variantPath) {
55
+ const tmp = await mkdtemp(path.join(os.tmpdir(), 'graft-vendor-'));
56
+ const stage = path.join(tmp, 'package');
57
+ await cp(pristineDir, stage, { recursive: true });
58
+ const result = await applyToInstalledCopy({ dir: stage, version: '' }, parsePatchFile(await readFile(variantPath, 'utf8')));
59
+ if (result.action !== 'patched') {
60
+ await rm(tmp, { recursive: true, force: true });
61
+ throw new Error(`cannot build the vendored package: ${result.reason ?? `variant ${result.action}`}`);
62
+ }
63
+ const files = (await listFiles(stage)).map((f) => `package/${f}`);
64
+ const file = path.join(tmp, 'vendor.tgz');
65
+ await tarCreate({ gzip: true, file, cwd: tmp, portable: true, mtime: FIXED_MTIME }, files);
66
+ return { file, tmp };
67
+ }
68
+ async function readManifest(installRoot) {
69
+ const file = path.join(installRoot, 'package.json');
70
+ const text = await readFile(file, 'utf8');
71
+ return { file, text, json: JSON.parse(text) };
72
+ }
73
+ async function writeManifest(m) {
74
+ const indent = /^[ \t]+/m.exec(m.text)?.[0] ?? ' ';
75
+ const next = `${JSON.stringify(m.json, null, indent)}\n`;
76
+ if (next !== m.text)
77
+ await writeFile(m.file, next);
78
+ }
79
+ /** Where this package manager reads overrides from. */
80
+ function overrideLocation(installRoot) {
81
+ const pm = detectPackageManager(installRoot);
82
+ if (pm === 'yarn' || pm === 'yarn-berry')
83
+ return { kind: 'resolutions' };
84
+ if (pm === 'pnpm')
85
+ return { kind: existsSync(path.join(installRoot, WORKSPACE_FILE)) ? 'pnpm-yaml' : 'pnpm-json' };
86
+ return { kind: 'overrides' }; // npm, bun, upm
87
+ }
88
+ /**
89
+ * The override value. npm resolves a `file:` override relative to each *consuming* package
90
+ * (node_modules/<dependent>/…), so for npm the override references the root's own dependency
91
+ * (`$<pkg>`), which npm resolves against the root.
92
+ */
93
+ const overrideValue = (installRoot, pkg, spec) => (detectPackageManager(installRoot) === 'npm' ? `$${pkg}` : spec);
94
+ async function readOverride(installRoot, json, pkg) {
95
+ const loc = overrideLocation(installRoot);
96
+ if (loc.kind === 'overrides')
97
+ return json.overrides?.[pkg];
98
+ if (loc.kind === 'resolutions')
99
+ return json.resolutions?.[pkg];
100
+ if (loc.kind === 'pnpm-json')
101
+ return json.pnpm?.overrides?.[pkg];
102
+ const doc = parseDocument(await readFile(path.join(installRoot, WORKSPACE_FILE), 'utf8'));
103
+ return doc.getIn(['overrides', pkg]);
104
+ }
105
+ const lockKeyIsFor = (key, pkg) => key === `node_modules/${pkg}` || key.endsWith(`/node_modules/${pkg}`);
106
+ /**
107
+ * npm keeps nested copies already in package-lock.json and, when it later applies the
108
+ * override to them, resolves the root-relative `file:` path relative to the dependent
109
+ * (ENOENT). Dropping every locked entry for `pkg` makes npm re-resolve them all to the
110
+ * vendored package. Returns the lockfile path if it changed.
111
+ */
112
+ async function pruneNpmLock(installRoot, pkg) {
113
+ const file = path.join(installRoot, 'package-lock.json');
114
+ if (!existsSync(file))
115
+ return undefined;
116
+ const text = await readFile(file, 'utf8');
117
+ let lock;
118
+ try {
119
+ lock = JSON.parse(text);
120
+ }
121
+ catch {
122
+ return undefined;
123
+ }
124
+ let changed = false;
125
+ for (const key of Object.keys(lock.packages ?? {})) {
126
+ if (lockKeyIsFor(key, pkg)) {
127
+ delete lock.packages[key];
128
+ changed = true;
129
+ }
130
+ }
131
+ if (lock.dependencies?.[pkg]) {
132
+ delete lock.dependencies[pkg];
133
+ changed = true;
134
+ }
135
+ if (!changed)
136
+ return undefined;
137
+ const indent = /^[ \t]+/m.exec(text)?.[0] ?? ' ';
138
+ await writeFile(file, `${JSON.stringify(lock, null, indent)}\n`);
139
+ return file;
140
+ }
141
+ /** package-lock.json entries for `pkg` that are not the vendored tarball. */
142
+ async function strayNpmLockEntries(installRoot, pkg, spec) {
143
+ const file = path.join(installRoot, 'package-lock.json');
144
+ if (!existsSync(file))
145
+ return [];
146
+ try {
147
+ const lock = JSON.parse(await readFile(file, 'utf8'));
148
+ return Object.entries(lock.packages ?? {})
149
+ .filter(([key, entry]) => lockKeyIsFor(key, pkg) && entry.resolved !== spec)
150
+ .map(([key, entry]) => `${key} (${entry.resolved ?? 'unresolved'})`);
151
+ }
152
+ catch {
153
+ return [];
154
+ }
155
+ }
156
+ export async function vendorProblems(ctx, pkg, version, variantPath) {
157
+ const build = ctx.upstream ? async () => buildVendorTarball(await ctx.upstream.resolve(pkg, version), variantPath) : undefined;
158
+ return vendoredTarballProblems(ctx, pkg, version, build, `pristine ${pkg}@${version} + ${posix(path.relative(ctx.installRoot, variantPath))}`);
159
+ }
160
+ /**
161
+ * Problems with installing `pkg@version` from a vendored tarball through overrides: the
162
+ * tarball is missing or stale (when `build` is given, its contents must equal a fresh build,
163
+ * described by `expected`), overrides / root dependency / npm lock entries point elsewhere, or
164
+ * a competing patch is configured. Shared by the vendor applier and the fork mechanism.
165
+ */
166
+ export async function vendoredTarballProblems(ctx, pkg, version, build, expected) {
167
+ const { installRoot } = ctx;
168
+ const problems = [];
169
+ const vendorDir = path.join(ctx.graftRoot, VENDOR_DIR);
170
+ const tarball = path.join(vendorDir, vendorFileName(pkg, version));
171
+ const spec = `file:${posix(path.relative(installRoot, tarball))}`;
172
+ const { json } = await readManifest(installRoot);
173
+ if (!existsSync(tarball)) {
174
+ problems.push(`${posix(path.relative(installRoot, tarball))} is missing; run \`graft apply\``);
175
+ }
176
+ else if (build) {
177
+ const built = await build().catch((err) => err);
178
+ if (built instanceof Error)
179
+ problems.push(built.message);
180
+ else {
181
+ try {
182
+ if (!sameContents(await tarballContents(built.file), await tarballContents(tarball))) {
183
+ problems.push(`${posix(path.relative(installRoot, tarball))} is stale (it is not ${expected}); run \`graft apply\``);
184
+ }
185
+ }
186
+ finally {
187
+ await rm(built.tmp, { recursive: true, force: true });
188
+ }
189
+ }
190
+ }
191
+ const override = await readOverride(installRoot, json, pkg);
192
+ const expectedOverride = overrideValue(installRoot, pkg, spec);
193
+ const where = { overrides: 'package.json overrides', resolutions: 'package.json resolutions', 'pnpm-json': 'package.json pnpm.overrides', 'pnpm-yaml': `${WORKSPACE_FILE} overrides` }[overrideLocation(installRoot).kind];
194
+ if (override !== expectedOverride)
195
+ problems.push(`${where}["${pkg}"] must be "${expectedOverride}"${override ? ` (is "${override}")` : ''}`);
196
+ if (expectedOverride.startsWith('$') && !DEP_FIELDS.some((f) => json[f]?.[pkg] !== undefined)) {
197
+ problems.push(`package.json must declare ${pkg} as a dependency ("${spec}") for npm's "$${pkg}" override reference`);
198
+ }
199
+ if (detectPackageManager(installRoot) === 'npm') {
200
+ for (const entry of await strayNpmLockEntries(installRoot, pkg, spec)) {
201
+ problems.push(`package-lock.json still locks ${entry} instead of the vendored package; run \`graft apply\``);
202
+ }
203
+ }
204
+ for (const field of DEP_FIELDS) {
205
+ const declared = json[field]?.[pkg];
206
+ if (declared !== undefined && declared !== spec) {
207
+ const why = detectPackageManager(installRoot) === 'npm' ? '; npm refuses an override that differs from a direct dependency' : '';
208
+ problems.push(`package.json ${field}["${pkg}"] must be "${spec}" (is "${declared}")${why}`);
209
+ }
210
+ }
211
+ if (existsSync(vendorDir)) {
212
+ for (const f of (await readdir(vendorDir)).filter((f) => f.startsWith(vendorPrefix(pkg)) && /^\d/.test(f.slice(vendorPrefix(pkg).length)) && f !== path.basename(tarball))) {
213
+ problems.push(`${VENDOR_DIR}/${f} is for another version of ${pkg}; remove it`);
214
+ }
215
+ }
216
+ if (Object.keys(json.patchedDependencies ?? {}).some((k) => k === pkg || k.startsWith(`${pkg}@`))) {
217
+ problems.push(`package.json patchedDependencies also patches ${pkg}; the vendored package already contains the patch — remove the entry`);
218
+ }
219
+ return problems;
220
+ }
221
+ export async function vendorConfigure(ctx, pkg, version, variantPath) {
222
+ if (!ctx.upstream)
223
+ throw new Error('the vendor applier needs the upstream source to build the patched package');
224
+ const upstream = ctx.upstream;
225
+ return installVendoredTarball(ctx, pkg, version, async () => buildVendorTarball(await upstream.resolve(pkg, version), variantPath));
226
+ }
227
+ /**
228
+ * Commit a freshly built tarball as `.grafts/.vendor/<pkg>-<version>.tgz` (keeping the old bytes
229
+ * when contents are unchanged, so lockfile integrity is stable), remove other versions' tarballs,
230
+ * and point overrides, the root dependency and npm's lockfile at it. Shared by the vendor applier
231
+ * and the fork mechanism.
232
+ */
233
+ export async function installVendoredTarball(ctx, pkg, version, build) {
234
+ const { installRoot } = ctx;
235
+ const vendorDir = path.join(ctx.graftRoot, VENDOR_DIR);
236
+ await mkdir(vendorDir, { recursive: true });
237
+ const tarball = path.join(vendorDir, vendorFileName(pkg, version));
238
+ const built = await build();
239
+ try {
240
+ // Keep the existing bytes when contents are unchanged, so the lockfile's integrity is stable.
241
+ if (!existsSync(tarball) || !sameContents(await tarballContents(built.file), await tarballContents(tarball))) {
242
+ await rename(built.file, tarball).catch(async () => writeFile(tarball, await readFile(built.file)));
243
+ }
244
+ }
245
+ finally {
246
+ await rm(built.tmp, { recursive: true, force: true });
247
+ }
248
+ for (const f of await readdir(vendorDir)) {
249
+ if (f.startsWith(vendorPrefix(pkg)) && /^\d/.test(f.slice(vendorPrefix(pkg).length)) && f !== path.basename(tarball))
250
+ await rm(path.join(vendorDir, f));
251
+ }
252
+ const spec = `file:${posix(path.relative(installRoot, tarball))}`;
253
+ const m = await readManifest(installRoot);
254
+ let declared = false;
255
+ for (const field of DEP_FIELDS) {
256
+ if (m.json[field]?.[pkg] !== undefined) {
257
+ m.json[field][pkg] = spec;
258
+ declared = true;
259
+ }
260
+ }
261
+ const value = overrideValue(installRoot, pkg, spec);
262
+ // npm's `$pkg` reference needs a root dependency; add one when the package is only transitive.
263
+ if (value.startsWith('$') && !declared)
264
+ (m.json.dependencies ??= {})[pkg] = spec;
265
+ const loc = overrideLocation(installRoot);
266
+ if (loc.kind === 'overrides')
267
+ (m.json.overrides ??= {})[pkg] = value;
268
+ if (loc.kind === 'resolutions')
269
+ (m.json.resolutions ??= {})[pkg] = spec;
270
+ if (loc.kind === 'pnpm-json')
271
+ ((m.json.pnpm ??= {}).overrides ??= {})[pkg] = spec;
272
+ const pd = m.json.patchedDependencies;
273
+ if (pd) {
274
+ for (const k of Object.keys(pd))
275
+ if (k === pkg || k.startsWith(`${pkg}@`))
276
+ delete pd[k];
277
+ if (!Object.keys(pd).length)
278
+ delete m.json.patchedDependencies;
279
+ }
280
+ await writeManifest(m);
281
+ const files = [tarball, m.file];
282
+ if (detectPackageManager(installRoot) === 'npm') {
283
+ const lock = await pruneNpmLock(installRoot, pkg);
284
+ if (lock)
285
+ files.push(lock);
286
+ }
287
+ if (loc.kind === 'pnpm-yaml') {
288
+ const yamlFile = path.join(installRoot, WORKSPACE_FILE);
289
+ const doc = parseDocument(await readFile(yamlFile, 'utf8'));
290
+ doc.setIn(['overrides', pkg], spec);
291
+ for (const key of Object.keys(doc.get('patchedDependencies')?.toJSON?.() ?? {})) {
292
+ if (key === pkg || key.startsWith(`${pkg}@`))
293
+ doc.deleteIn(['patchedDependencies', key]);
294
+ }
295
+ await writeFile(yamlFile, doc.toString());
296
+ files.push(yamlFile);
297
+ }
298
+ return { files };
299
+ }
300
+ //# sourceMappingURL=vendor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vendor.js","sourceRoot":"","sources":["../src/vendor.ts"],"names":[],"mappings":"AAAA,uFAAuF;AACvF,2FAA2F;AAC3F,6FAA6F;AAC7F,uFAAuF;AACvF,0FAA0F;AAC1F,6FAA6F;AAC7F,gCAAgC;AAChC,OAAO,EAAE,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