@multiplatform.one/cli 8.0.0 → 8.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.
- package/README.md +14 -8
- package/lib/bin/multiplatformOne.mjs +17 -11
- package/lib/commands/e2e.mjs +6 -15
- package/lib/commands/pythonTools.mjs +81 -0
- package/lib/commands/scaffold.mjs +62 -3
- package/lib/commands/verbs.mjs +55 -33
- package/lib/source.json +2 -2
- package/package.json +2 -2
- package/scripts/scaffold.sh +12 -0
- package/src/bin/multiplatformOne.ts +19 -13
- package/src/commands/e2e.ts +16 -21
- package/src/commands/pythonTools.spec.ts +134 -0
- package/src/commands/pythonTools.ts +106 -0
- package/src/commands/scaffold.ts +60 -3
- package/src/commands/updateCleanups.spec.ts +282 -0
- package/src/commands/verbs.spec.ts +94 -5
- package/src/commands/verbs.ts +57 -33
- package/types/commands/e2e.d.ts +1 -1
- package/types/commands/e2e.d.ts.map +1 -1
- package/types/commands/pythonTools.d.ts +22 -0
- package/types/commands/pythonTools.d.ts.map +1 -0
- package/types/commands/scaffold.d.ts +1 -1
- package/types/commands/scaffold.d.ts.map +1 -1
- package/types/commands/verbs.d.ts +2 -2
- package/types/commands/verbs.d.ts.map +1 -1
- package/lib/commands/ruff.mjs +0 -52
- package/src/commands/ruff.spec.ts +0 -46
- package/src/commands/ruff.ts +0 -74
- package/types/commands/ruff.d.ts +0 -15
- package/types/commands/ruff.d.ts.map +0 -1
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import nanoSpawn from "nano-spawn";
|
|
5
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
6
|
+
import { pythonFiles, runPythonTools } from "./pythonTools";
|
|
7
|
+
|
|
8
|
+
type Spawn = typeof nanoSpawn;
|
|
9
|
+
|
|
10
|
+
// Runs a real subprocess in place of uv, so the errors are nano-spawn's own.
|
|
11
|
+
function standIn(script: (args: readonly string[]) => string, calls: string[][] = []): Spawn {
|
|
12
|
+
return ((_file: string, args: readonly string[] = [], options = {}) => {
|
|
13
|
+
calls.push([...args]);
|
|
14
|
+
return nanoSpawn("sh", ["-c", script(args)], options);
|
|
15
|
+
}) as Spawn;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const files = ["apps/frappe/serve_dev.py", "scripts/tool.py"];
|
|
19
|
+
|
|
20
|
+
const dirs: string[] = [];
|
|
21
|
+
afterEach(() => {
|
|
22
|
+
for (const dir of dirs.splice(0)) fs.rmSync(dir, { recursive: true, force: true });
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
function tempDir(): string {
|
|
26
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "fz-10574-"));
|
|
27
|
+
dirs.push(dir);
|
|
28
|
+
return dir;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function project(): string {
|
|
32
|
+
const dir = tempDir();
|
|
33
|
+
fs.writeFileSync(path.join(dir, "uv.lock"), "version = 1\n");
|
|
34
|
+
return dir;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
describe("runPythonTools (MPO-497, FZ-10574)", () => {
|
|
38
|
+
it("names a missing uv and the pin that provides it", async () => {
|
|
39
|
+
const missing = ((_file: string, args: readonly string[] = [], options = {}) =>
|
|
40
|
+
nanoSpawn("fz-10574-no-such-uv", [...args], options)) as Spawn;
|
|
41
|
+
const reason = await runPythonTools("check", files, project(), missing);
|
|
42
|
+
expect(reason).toMatch(/^uv is not on PATH\./);
|
|
43
|
+
expect(reason).toContain(".tool-versions");
|
|
44
|
+
expect(reason).toContain("make configure");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("reports a uv that will not start with what it printed", async () => {
|
|
48
|
+
const shim = standIn(() => "echo 'mise ERROR No version is set for shim: uv' >&2; exit 1");
|
|
49
|
+
const reason = await runPythonTools("check", files, project(), shim);
|
|
50
|
+
expect(reason).toBe(
|
|
51
|
+
"uv did not start: mise ERROR No version is set for shim: uv. uv is pinned in .tool-versions; `make configure` installs it through mise or asdf.",
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("checks with black --check, then basedpyright, over the files through the locked uv project", async () => {
|
|
56
|
+
const calls: string[][] = [];
|
|
57
|
+
expect(
|
|
58
|
+
await runPythonTools(
|
|
59
|
+
"check",
|
|
60
|
+
files,
|
|
61
|
+
project(),
|
|
62
|
+
standIn(() => "true", calls),
|
|
63
|
+
),
|
|
64
|
+
).toBe(undefined);
|
|
65
|
+
expect(calls).toEqual([
|
|
66
|
+
["--version"],
|
|
67
|
+
["run", "--locked", "black", "--check", ...files],
|
|
68
|
+
["run", "--locked", "basedpyright", ...files],
|
|
69
|
+
]);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("formats with black alone", async () => {
|
|
73
|
+
const calls: string[][] = [];
|
|
74
|
+
expect(
|
|
75
|
+
await runPythonTools(
|
|
76
|
+
"format",
|
|
77
|
+
files,
|
|
78
|
+
project(),
|
|
79
|
+
standIn(() => "true", calls),
|
|
80
|
+
),
|
|
81
|
+
).toBe(undefined);
|
|
82
|
+
expect(calls).toEqual([["--version"], ["run", "--locked", "black", ...files]]);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("names a project without uv.lock before running anything", async () => {
|
|
86
|
+
const calls: string[][] = [];
|
|
87
|
+
const reason = await runPythonTools(
|
|
88
|
+
"check",
|
|
89
|
+
files,
|
|
90
|
+
tempDir(),
|
|
91
|
+
standIn(() => "true", calls),
|
|
92
|
+
);
|
|
93
|
+
expect(reason).toBe(
|
|
94
|
+
"No uv.lock at the project root pins black and basedpyright; `mpo update` brings it and pyproject.toml.",
|
|
95
|
+
);
|
|
96
|
+
expect(calls).toEqual([["--version"]]);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("runs both checks and separates findings from a configuration error", async () => {
|
|
100
|
+
const findings = standIn((args) => (args[0] === "--version" ? "true" : "exit 1"));
|
|
101
|
+
expect(await runPythonTools("check", files, project(), findings)).toBe(
|
|
102
|
+
[
|
|
103
|
+
"black --check found files to reformat (listed above).",
|
|
104
|
+
"basedpyright found errors .basedpyright/baseline.json does not hold (listed above).",
|
|
105
|
+
].join("\n"),
|
|
106
|
+
);
|
|
107
|
+
const broken = standIn((args) =>
|
|
108
|
+
args[0] === "--version" ? "true" : args.includes("basedpyright") ? "exit 3" : "true",
|
|
109
|
+
);
|
|
110
|
+
expect(await runPythonTools("check", files, project(), broken)).toBe(
|
|
111
|
+
"basedpyright exited 3, a configuration or usage error (see above).",
|
|
112
|
+
);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe("pythonFiles (FZ-10574)", () => {
|
|
117
|
+
it("lists tracked and untracked Python files, never ignored or deleted ones", async () => {
|
|
118
|
+
const dir = tempDir();
|
|
119
|
+
const write = (file: string) => {
|
|
120
|
+
fs.mkdirSync(path.dirname(path.join(dir, file)), { recursive: true });
|
|
121
|
+
fs.writeFileSync(path.join(dir, file), "x = 1\n");
|
|
122
|
+
};
|
|
123
|
+
await nanoSpawn("git", ["init", "--quiet"], { cwd: dir });
|
|
124
|
+
for (const file of ["tracked.py", "gone.py", "notes.txt", "apps/frappe/env/lib/site.py"])
|
|
125
|
+
write(file);
|
|
126
|
+
fs.writeFileSync(path.join(dir, ".gitignore"), "/apps/frappe/env\n");
|
|
127
|
+
await nanoSpawn("git", ["add", "tracked.py", "gone.py", "notes.txt", ".gitignore"], {
|
|
128
|
+
cwd: dir,
|
|
129
|
+
});
|
|
130
|
+
fs.rmSync(path.join(dir, "gone.py"));
|
|
131
|
+
write("typings/frappe/__init__.pyi");
|
|
132
|
+
expect((await pythonFiles(dir)).sort()).toEqual(["tracked.py", "typings/frappe/__init__.pyi"]);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import fsSync from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import nanoSpawn from "nano-spawn";
|
|
4
|
+
|
|
5
|
+
export type PythonMode = "format" | "check";
|
|
6
|
+
|
|
7
|
+
type Spawn = typeof nanoSpawn;
|
|
8
|
+
|
|
9
|
+
type Tool = "black" | "black --check" | "basedpyright";
|
|
10
|
+
|
|
11
|
+
const pinHint = "uv is pinned in .tool-versions; `make configure` installs it through mise or asdf";
|
|
12
|
+
|
|
13
|
+
function errorDetail(error: unknown): {
|
|
14
|
+
code?: string;
|
|
15
|
+
exitCode?: number;
|
|
16
|
+
signalName?: string;
|
|
17
|
+
stderr?: string;
|
|
18
|
+
message: string;
|
|
19
|
+
} {
|
|
20
|
+
const e = (error ?? {}) as {
|
|
21
|
+
cause?: { code?: string; message?: string };
|
|
22
|
+
exitCode?: number;
|
|
23
|
+
signalName?: string;
|
|
24
|
+
stderr?: string;
|
|
25
|
+
message?: string;
|
|
26
|
+
};
|
|
27
|
+
return {
|
|
28
|
+
code: e.cause?.code,
|
|
29
|
+
exitCode: e.exitCode,
|
|
30
|
+
signalName: e.signalName,
|
|
31
|
+
stderr: e.stderr?.trim() || undefined,
|
|
32
|
+
message: e.cause?.message ?? e.message ?? String(error),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Why `uv --version` did not answer, as one line for the lint output. */
|
|
37
|
+
export function uvStartFailure(error: unknown): string {
|
|
38
|
+
const d = errorDetail(error);
|
|
39
|
+
if (d.code === "ENOENT") return `uv is not on PATH. ${pinHint}.`;
|
|
40
|
+
const why = d.stderr ?? d.message;
|
|
41
|
+
return `uv did not start: ${why}. ${pinHint}.`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Why a tool failed once uv had started it, as one line for the lint output. */
|
|
45
|
+
export function toolFailure(tool: Tool, error: unknown): string {
|
|
46
|
+
const d = errorDetail(error);
|
|
47
|
+
if (d.signalName) return `${tool} was killed by ${d.signalName}.`;
|
|
48
|
+
if (d.exitCode === 1 && tool === "black --check")
|
|
49
|
+
return "black --check found files to reformat (listed above).";
|
|
50
|
+
if (d.exitCode === 1 && tool === "basedpyright")
|
|
51
|
+
return "basedpyright found errors .basedpyright/baseline.json does not hold (listed above).";
|
|
52
|
+
if (d.exitCode !== undefined)
|
|
53
|
+
return `${tool} exited ${d.exitCode}, a configuration or usage error (see above).`;
|
|
54
|
+
return `${tool} failed: ${d.message}.`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The Python files git knows under `cwd`, tracked or untracked and not
|
|
59
|
+
* ignored, that are still on disk: the set CI's frappe/lint job checks.
|
|
60
|
+
*/
|
|
61
|
+
export async function pythonFiles(cwd: string, spawn: Spawn = nanoSpawn): Promise<string[]> {
|
|
62
|
+
const { stdout } = await spawn(
|
|
63
|
+
"git",
|
|
64
|
+
["ls-files", "-z", "--cached", "--others", "--exclude-standard", "--", "*.py", "*.pyi"],
|
|
65
|
+
{ cwd },
|
|
66
|
+
);
|
|
67
|
+
const listed = new Set(stdout.split("\0").filter(Boolean));
|
|
68
|
+
return [...listed].filter((file) => fsSync.existsSync(path.join(cwd, file)));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Run black, or black --check then basedpyright, on `files` through the uv
|
|
73
|
+
* project at `cwd`, first proving uv starts and the lock is there, so a missing
|
|
74
|
+
* install or project is reported as that instead of as a lint failure. Returns
|
|
75
|
+
* the reasons it failed, one per line, or undefined when it passed.
|
|
76
|
+
*/
|
|
77
|
+
export async function runPythonTools(
|
|
78
|
+
mode: PythonMode,
|
|
79
|
+
files: readonly string[],
|
|
80
|
+
cwd: string,
|
|
81
|
+
spawn: Spawn = nanoSpawn,
|
|
82
|
+
): Promise<string | undefined> {
|
|
83
|
+
try {
|
|
84
|
+
await spawn("uv", ["--version"], { cwd });
|
|
85
|
+
} catch (error) {
|
|
86
|
+
return uvStartFailure(error);
|
|
87
|
+
}
|
|
88
|
+
if (!fsSync.existsSync(path.join(cwd, "uv.lock")))
|
|
89
|
+
return "No uv.lock at the project root pins black and basedpyright; `mpo update` brings it and pyproject.toml.";
|
|
90
|
+
const steps: [Tool, string[]][] =
|
|
91
|
+
mode === "format"
|
|
92
|
+
? [["black", ["black"]]]
|
|
93
|
+
: [
|
|
94
|
+
["black --check", ["black", "--check"]],
|
|
95
|
+
["basedpyright", ["basedpyright"]],
|
|
96
|
+
];
|
|
97
|
+
const reasons: string[] = [];
|
|
98
|
+
for (const [tool, args] of steps) {
|
|
99
|
+
try {
|
|
100
|
+
await spawn("uv", ["run", "--locked", ...args, ...files], { stdio: "inherit", cwd });
|
|
101
|
+
} catch (error) {
|
|
102
|
+
reasons.push(toolFailure(tool, error));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return reasons.length > 0 ? reasons.join("\n") : undefined;
|
|
106
|
+
}
|
package/src/commands/scaffold.ts
CHANGED
|
@@ -53,9 +53,12 @@ function reportKept(stdout: string): void {
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
/** Run scaffold.sh and return the provenance it wrote. */
|
|
56
|
-
export async function runScaffold(
|
|
56
|
+
export async function runScaffold(
|
|
57
|
+
request: ScaffoldRequest,
|
|
58
|
+
script = scaffoldScript(),
|
|
59
|
+
): Promise<MpoProvenance> {
|
|
57
60
|
await checkSourceAccess(request.source);
|
|
58
|
-
const args = [
|
|
61
|
+
const args = [script];
|
|
59
62
|
for (const app of request.apps) args.push("--app", app);
|
|
60
63
|
for (const service of request.services) args.push("--service", service);
|
|
61
64
|
if (request.mpoVersion) args.push("--mpo-version", request.mpoVersion);
|
|
@@ -88,6 +91,55 @@ export interface TempScaffold {
|
|
|
88
91
|
cleanup: () => void;
|
|
89
92
|
}
|
|
90
93
|
|
|
94
|
+
/** Where the monorepo keeps its scaffold, at every commit since MPO-310. */
|
|
95
|
+
const scaffoldPath = "public/cli/scripts/scaffold.sh";
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Write the scaffold.sh that `source` carries at `ref` to `file`; false when
|
|
99
|
+
* that commit carries none. A merge baseline made with it is what that
|
|
100
|
+
* commit's own CLI scaffolded, so whatever the scaffold learned between two
|
|
101
|
+
* commits (a cleanup of root wiring, say) is part of the difference between
|
|
102
|
+
* their baselines and reaches the project, with no step of it named here. A
|
|
103
|
+
* url is fetched shallow and blobless, then the one blob, as scaffold.sh does.
|
|
104
|
+
*/
|
|
105
|
+
async function scaffoldScriptAt(source: string, ref: string, file: string): Promise<boolean> {
|
|
106
|
+
await checkSourceAccess(source);
|
|
107
|
+
const script = [
|
|
108
|
+
"set -eu",
|
|
109
|
+
'SOURCE="$1"; REF="$2"; SCRIPT="$3"; OUT="$4"',
|
|
110
|
+
'if [ -d "$SOURCE" ]; then',
|
|
111
|
+
' REPO="$SOURCE"',
|
|
112
|
+
' COMMIT="$(git -C "$REPO" rev-parse --verify --quiet "$REF^{commit}")" ||',
|
|
113
|
+
' { printf "%s is not a commit in %s\\n" "$REF" "$REPO" >&2; exit 1; }',
|
|
114
|
+
"else",
|
|
115
|
+
' REPO="$(mktemp -d "${TMPDIR:-/tmp}/mpo-scaffold-script.XXXXXX")"',
|
|
116
|
+
" trap 'rm -rf \"$REPO\"' EXIT",
|
|
117
|
+
' git init -q "$REPO"',
|
|
118
|
+
' git -C "$REPO" remote add origin "$SOURCE"',
|
|
119
|
+
' git -C "$REPO" fetch -q --depth 1 --filter=blob:none origin "$REF"',
|
|
120
|
+
' COMMIT="$(git -C "$REPO" rev-parse --verify --quiet "FETCH_HEAD^{commit}")"',
|
|
121
|
+
"fi",
|
|
122
|
+
'BLOB="$(git -C "$REPO" ls-tree "$COMMIT" -- "$SCRIPT" | awk \'$2 == "blob" { print $3 }\')"',
|
|
123
|
+
'[ -n "$BLOB" ] || exit 3',
|
|
124
|
+
'if [ "$(git -C "$REPO" config --get remote.origin.promisor || true)" = true ]; then',
|
|
125
|
+
' printf "%s\\n" "$BLOB" | git -C "$REPO" -c fetch.negotiationAlgorithm=noop fetch -q \\',
|
|
126
|
+
" --no-tags --no-write-fetch-head --filter=blob:none --stdin origin",
|
|
127
|
+
"fi",
|
|
128
|
+
'git -C "$REPO" cat-file blob "$BLOB" >"$OUT"',
|
|
129
|
+
].join("\n");
|
|
130
|
+
try {
|
|
131
|
+
await spawn("sh", ["-c", script, "sh", source, ref, scaffoldPath, file], {
|
|
132
|
+
stdio: ["ignore", "ignore", "pipe"],
|
|
133
|
+
});
|
|
134
|
+
return true;
|
|
135
|
+
} catch (err) {
|
|
136
|
+
if ((err as { exitCode?: number }).exitCode === 3) return false;
|
|
137
|
+
const stderr = ((err as { stderr?: string }).stderr ?? "").trim();
|
|
138
|
+
const detail = stderr.split("\n").filter(Boolean).pop() ?? (err as Error).message;
|
|
139
|
+
throw new Error(`could not read ${scaffoldPath} at ${ref} from ${source}: ${detail}`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
91
143
|
/** A scaffold in a temp dir (merge baselines). */
|
|
92
144
|
export async function scaffoldToTemp(
|
|
93
145
|
request: Omit<ScaffoldRequest, "target" | "into">,
|
|
@@ -96,7 +148,12 @@ export async function scaffoldToTemp(
|
|
|
96
148
|
const dir = join(parent, "project");
|
|
97
149
|
const cleanup = () => rmSync(parent, { recursive: true, force: true });
|
|
98
150
|
try {
|
|
99
|
-
const
|
|
151
|
+
const script = join(parent, "scaffold.sh");
|
|
152
|
+
const carried = await scaffoldScriptAt(request.source, request.ref, script);
|
|
153
|
+
if (!carried) {
|
|
154
|
+
console.warn(`⚠️ ${request.ref} carries no ${scaffoldPath}; using this CLI's for that side`);
|
|
155
|
+
}
|
|
156
|
+
const provenance = await runScaffold({ ...request, target: dir }, carried ? script : undefined);
|
|
100
157
|
return { dir, provenance, cleanup };
|
|
101
158
|
} catch (err) {
|
|
102
159
|
cleanup();
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import spawn from "nano-spawn";
|
|
5
|
+
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
|
|
6
|
+
import { addApp } from "./addApp";
|
|
7
|
+
import { init } from "./init";
|
|
8
|
+
import { readProvenance } from "./provenance";
|
|
9
|
+
import { scaffoldScript } from "./scaffold";
|
|
10
|
+
import { updateApp } from "./updateApp";
|
|
11
|
+
|
|
12
|
+
// A stand-in monorepo of five files, so a scaffold takes well under a second.
|
|
13
|
+
// Its scaffold.sh starts as this CLI's own, and a later commit teaches it a
|
|
14
|
+
// cleanup made up here. Nothing in update or add-app names that cleanup: it can
|
|
15
|
+
// only reach a project when each side of the merge runs the scaffold.sh its own
|
|
16
|
+
// commit carries.
|
|
17
|
+
const heavy = 300_000;
|
|
18
|
+
const json = (value: unknown) => `${JSON.stringify(value, null, 2)}\n`;
|
|
19
|
+
const ownScript = readFileSync(scaffoldScript(), "utf-8");
|
|
20
|
+
const anchor = "# ── 6. provenance";
|
|
21
|
+
|
|
22
|
+
const legacyRule = ".PHONY: legacy\nlegacy:\n\t@echo legacy\n\n";
|
|
23
|
+
const makefile = `.PHONY: build\nbuild:\n\t@echo build\n\n${legacyRule}.PHONY: test\ntest:\n\t@echo test\n`;
|
|
24
|
+
const cleaned = makefile.replace(legacyRule, "");
|
|
25
|
+
const dropLegacy = "sed_file \"$TREE/Makefile\" '/^\\.PHONY: legacy$/,/^$/d'";
|
|
26
|
+
|
|
27
|
+
/** The scaffold with `step` run just before it writes .mpo.json. */
|
|
28
|
+
function scaffoldWith(step: string): string {
|
|
29
|
+
if (!ownScript.includes(anchor)) throw new Error(`scaffold.sh has no "${anchor}" line`);
|
|
30
|
+
return ownScript.replace(anchor, `${step}\n\n${anchor}`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function git(cwd: string, args: string[]): Promise<string> {
|
|
34
|
+
const result = await spawn("git", args, { cwd });
|
|
35
|
+
return result.stdout.trim();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function newRepo(dir: string): Promise<void> {
|
|
39
|
+
mkdirSync(dir, { recursive: true });
|
|
40
|
+
await git(dir, ["init", "--quiet", "--initial-branch=main"]);
|
|
41
|
+
await git(dir, ["config", "user.email", "spec@example.com"]);
|
|
42
|
+
await git(dir, ["config", "user.name", "Spec"]);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function commitAll(dir: string, message: string): Promise<string> {
|
|
46
|
+
await git(dir, ["add", "-A"]);
|
|
47
|
+
await git(dir, ["commit", "--quiet", "-m", message]);
|
|
48
|
+
return git(dir, ["rev-parse", "HEAD"]);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function writeFiles(dir: string, files: Record<string, string>): void {
|
|
52
|
+
for (const [file, content] of Object.entries(files)) {
|
|
53
|
+
mkdirSync(dirname(join(dir, file)), { recursive: true });
|
|
54
|
+
writeFileSync(join(dir, file), content);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A stand-in monorepo whose first commit carries `script` as its scaffold.sh. */
|
|
59
|
+
async function standIn(dir: string, script: string): Promise<string> {
|
|
60
|
+
await newRepo(dir);
|
|
61
|
+
// The url cases fetch commits by sha, as a project's base does from GitLab.
|
|
62
|
+
await git(dir, ["config", "uploadpack.allowFilter", "true"]);
|
|
63
|
+
await git(dir, ["config", "uploadpack.allowAnySHA1InWant", "true"]);
|
|
64
|
+
writeFiles(dir, {
|
|
65
|
+
"package.json": json({ name: "fixture", private: true }),
|
|
66
|
+
Makefile: makefile,
|
|
67
|
+
"apps/one/package.json": json({ name: "@platform/one", private: true }),
|
|
68
|
+
"public/cli/package.json": json({ name: "@multiplatform.one/cli", version: "1.0.0" }),
|
|
69
|
+
"public/cli/scripts/scaffold.sh": script,
|
|
70
|
+
});
|
|
71
|
+
return commitAll(dir, "chore: stand-in monorepo");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const read = (dir: string, file: string) => readFileSync(join(dir, file), "utf-8");
|
|
75
|
+
|
|
76
|
+
describe("mpo update runs the scaffold each commit carries", () => {
|
|
77
|
+
let sandbox: string;
|
|
78
|
+
let previousCwd: string;
|
|
79
|
+
let source: string;
|
|
80
|
+
let recorded: string;
|
|
81
|
+
let next: string;
|
|
82
|
+
|
|
83
|
+
beforeAll(async () => {
|
|
84
|
+
sandbox = mkdtempSync(join(tmpdir(), "mpo-update-cleanups-spec-"));
|
|
85
|
+
previousCwd = process.cwd();
|
|
86
|
+
source = join(sandbox, "source");
|
|
87
|
+
recorded = await standIn(source, ownScript);
|
|
88
|
+
writeFileSync(join(source, "public/cli/scripts/scaffold.sh"), scaffoldWith(dropLegacy));
|
|
89
|
+
next = await commitAll(source, "feat: the scaffold drops the legacy rule");
|
|
90
|
+
}, heavy);
|
|
91
|
+
|
|
92
|
+
afterEach(() => {
|
|
93
|
+
process.chdir(previousCwd);
|
|
94
|
+
vi.restoreAllMocks();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
afterAll(() => {
|
|
98
|
+
process.chdir(previousCwd);
|
|
99
|
+
rmSync(sandbox, { recursive: true, force: true });
|
|
100
|
+
}, heavy);
|
|
101
|
+
|
|
102
|
+
/** `mpo init` at the recorded commit, committed; optionally edited and committed again. */
|
|
103
|
+
async function project(
|
|
104
|
+
name: string,
|
|
105
|
+
edit?: (dir: string) => void,
|
|
106
|
+
from = { source, checkout: recorded },
|
|
107
|
+
): Promise<string> {
|
|
108
|
+
const parent = join(sandbox, name);
|
|
109
|
+
mkdirSync(parent);
|
|
110
|
+
process.chdir(parent);
|
|
111
|
+
await init("shop", {
|
|
112
|
+
...from,
|
|
113
|
+
services: [],
|
|
114
|
+
skipInstall: true,
|
|
115
|
+
skipGit: true,
|
|
116
|
+
yes: true,
|
|
117
|
+
});
|
|
118
|
+
const dir = join(parent, "shop");
|
|
119
|
+
await newRepo(dir);
|
|
120
|
+
await commitAll(dir, "feat: scaffold (mpo init)");
|
|
121
|
+
expect(read(dir, "Makefile")).toBe(makefile);
|
|
122
|
+
if (edit) {
|
|
123
|
+
edit(dir);
|
|
124
|
+
await commitAll(dir, "feat: the project's own edit");
|
|
125
|
+
}
|
|
126
|
+
process.chdir(dir);
|
|
127
|
+
return dir;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async function expectQuietRerun(dir: string, from: string): Promise<void> {
|
|
131
|
+
const committed = await commitAll(dir, "chore: mpo update");
|
|
132
|
+
await updateApp({ source: from, skipInstall: true });
|
|
133
|
+
expect(await git(dir, ["rev-parse", "HEAD"])).toBe(committed);
|
|
134
|
+
expect(await git(dir, ["status", "--porcelain"])).toBe("");
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
it(
|
|
138
|
+
"a cleanup added after the recorded commit leaves the project with the update",
|
|
139
|
+
async () => {
|
|
140
|
+
const dir = await project("untouched");
|
|
141
|
+
await updateApp({ source, skipInstall: true });
|
|
142
|
+
expect(read(dir, "Makefile")).toBe(cleaned);
|
|
143
|
+
expect(await git(dir, ["diff", "--cached", "--name-only"])).toBe("Makefile");
|
|
144
|
+
expect(readProvenance(dir)?.source?.sha).toBe(next);
|
|
145
|
+
await expectQuietRerun(dir, source);
|
|
146
|
+
},
|
|
147
|
+
heavy,
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
it(
|
|
151
|
+
"reads each commit's scaffold from a git url as well",
|
|
152
|
+
async () => {
|
|
153
|
+
const dir = await project("from-url");
|
|
154
|
+
const url = `file://${source}`;
|
|
155
|
+
await updateApp({ source: url, skipInstall: true });
|
|
156
|
+
expect(read(dir, "Makefile")).toBe(cleaned);
|
|
157
|
+
expect(readProvenance(dir)?.source?.sha).toBe(next);
|
|
158
|
+
await expectQuietRerun(dir, url);
|
|
159
|
+
},
|
|
160
|
+
heavy,
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
it(
|
|
164
|
+
"the project's own edit to that file survives the cleanup",
|
|
165
|
+
async () => {
|
|
166
|
+
const deploy = ".PHONY: deploy\ndeploy:\n\t@echo deploy\n";
|
|
167
|
+
const dir = await project("edited-elsewhere", (dir) => {
|
|
168
|
+
writeFileSync(join(dir, "Makefile"), `${makefile}\n${deploy}`);
|
|
169
|
+
});
|
|
170
|
+
await updateApp({ source, skipInstall: true });
|
|
171
|
+
expect(read(dir, "Makefile")).toBe(`${cleaned}\n${deploy}`);
|
|
172
|
+
await expectQuietRerun(dir, source);
|
|
173
|
+
},
|
|
174
|
+
heavy,
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
it(
|
|
178
|
+
"an edit to the lines the cleanup drops is a conflict the user sees, not an overwrite",
|
|
179
|
+
async () => {
|
|
180
|
+
const dir = await project("edited-rule", (dir) => {
|
|
181
|
+
writeFileSync(
|
|
182
|
+
join(dir, "Makefile"),
|
|
183
|
+
makefile.replace("\t@echo legacy", "\t@echo legacy, kept by the project"),
|
|
184
|
+
);
|
|
185
|
+
});
|
|
186
|
+
const lines: string[] = [];
|
|
187
|
+
vi.spyOn(console, "log").mockImplementation((...args) => {
|
|
188
|
+
lines.push(args.join(" "));
|
|
189
|
+
});
|
|
190
|
+
await updateApp({ source, skipInstall: true });
|
|
191
|
+
const merged = read(dir, "Makefile");
|
|
192
|
+
expect(merged).toContain("<<<<<<<");
|
|
193
|
+
expect(merged).toContain("legacy, kept by the project");
|
|
194
|
+
expect(merged).toContain(">>>>>>>");
|
|
195
|
+
const warning = lines.findIndex((line) => line.includes("merge conflicts"));
|
|
196
|
+
expect(warning).toBeGreaterThan(-1);
|
|
197
|
+
expect(lines.slice(warning + 1)).toContain(" Makefile");
|
|
198
|
+
},
|
|
199
|
+
heavy,
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
it(
|
|
203
|
+
"a project that already dropped the rule is left as it is",
|
|
204
|
+
async () => {
|
|
205
|
+
const dir = await project("already-clean", (dir) => {
|
|
206
|
+
writeFileSync(join(dir, "Makefile"), cleaned);
|
|
207
|
+
});
|
|
208
|
+
await updateApp({ source, skipInstall: true });
|
|
209
|
+
expect(read(dir, "Makefile")).toBe(cleaned);
|
|
210
|
+
expect(await git(dir, ["diff", "--cached", "--name-only"])).toBe("");
|
|
211
|
+
expect(await git(dir, ["status", "--porcelain"])).toBe("M .mpo.json");
|
|
212
|
+
await expectQuietRerun(dir, source);
|
|
213
|
+
},
|
|
214
|
+
heavy,
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
it(
|
|
218
|
+
"a recorded commit that carries no scaffold.sh gets this CLI's, and update says so",
|
|
219
|
+
async () => {
|
|
220
|
+
const bare = join(sandbox, "bare-source");
|
|
221
|
+
await standIn(bare, ownScript);
|
|
222
|
+
rmSync(join(bare, "public/cli/scripts"), { recursive: true });
|
|
223
|
+
const old = await commitAll(bare, "chore: no scaffold.sh yet");
|
|
224
|
+
writeFiles(bare, { "public/cli/scripts/scaffold.sh": scaffoldWith(dropLegacy) });
|
|
225
|
+
await commitAll(bare, "feat: scaffold.sh, dropping the legacy rule");
|
|
226
|
+
const dir = await project("no-script", undefined, { source: bare, checkout: old });
|
|
227
|
+
const warnings: string[] = [];
|
|
228
|
+
vi.spyOn(console, "warn").mockImplementation((...args) => {
|
|
229
|
+
warnings.push(args.join(" "));
|
|
230
|
+
});
|
|
231
|
+
await updateApp({ source: bare, skipInstall: true });
|
|
232
|
+
expect(warnings).toContain(
|
|
233
|
+
`⚠️ ${old} carries no public/cli/scripts/scaffold.sh; using this CLI's for that side`,
|
|
234
|
+
);
|
|
235
|
+
expect(read(dir, "Makefile")).toBe(cleaned);
|
|
236
|
+
},
|
|
237
|
+
heavy,
|
|
238
|
+
);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
describe("mpo add-app runs the scaffold at the recorded commit", () => {
|
|
242
|
+
let sandbox: string;
|
|
243
|
+
let previousCwd: string;
|
|
244
|
+
|
|
245
|
+
beforeAll(() => {
|
|
246
|
+
sandbox = mkdtempSync(join(tmpdir(), "mpo-add-app-scaffold-spec-"));
|
|
247
|
+
previousCwd = process.cwd();
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
afterEach(() => {
|
|
251
|
+
process.chdir(previousCwd);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
afterAll(() => {
|
|
255
|
+
process.chdir(previousCwd);
|
|
256
|
+
rmSync(sandbox, { recursive: true, force: true });
|
|
257
|
+
}, heavy);
|
|
258
|
+
|
|
259
|
+
it(
|
|
260
|
+
"root wiring that scaffold writes per app follows the new app",
|
|
261
|
+
async () => {
|
|
262
|
+
// Root wiring this CLI's scaffold never writes: only the recorded
|
|
263
|
+
// commit's scaffold can add the new app to it.
|
|
264
|
+
const script = scaffoldWith(`printf 'apps:%s\\n' " $APPS" >"$TREE/apps.txt"`);
|
|
265
|
+
const source = join(sandbox, "source");
|
|
266
|
+
const recorded = await standIn(source, script);
|
|
267
|
+
const scriptFile = join(sandbox, "scaffold.sh");
|
|
268
|
+
writeFileSync(scriptFile, script);
|
|
269
|
+
const dir = join(sandbox, "shop");
|
|
270
|
+
await spawn("sh", [scriptFile, "--app", "shop", "--", source, recorded, dir]);
|
|
271
|
+
await newRepo(dir);
|
|
272
|
+
await commitAll(dir, "feat: scaffold");
|
|
273
|
+
expect(read(dir, "apps.txt")).toBe("apps: shop\n");
|
|
274
|
+
|
|
275
|
+
process.chdir(dir);
|
|
276
|
+
await addApp("kiosk", { source, skipInstall: true });
|
|
277
|
+
expect(read(dir, "apps.txt")).toBe("apps: shop kiosk\n");
|
|
278
|
+
expect(readProvenance(dir)?.apps).toEqual(["shop", "kiosk"]);
|
|
279
|
+
},
|
|
280
|
+
heavy,
|
|
281
|
+
);
|
|
282
|
+
});
|