fix(cli): templates pull unpacks under .hyperframes, not beside templates/ (#7269)

* fix(cli): templates pull unpacks under .hyperframes so a watched project can take the template

* fix(cli): templates pull's staging comment fits on one line
This commit is contained in:
Miguel Ángel
2026-11-09 19:36:40 +00:00
committed by GitHub
parent 3ff0b3d81c
commit a4e16d87c6
2 changed files with 43 additions and 13 deletions
+27 -3
View File
@@ -205,7 +205,7 @@ describe("pullTemplate", () => {
});
const pulling = pullTemplate("sample", dir).catch(() => undefined);
await unpacking;
const templates = join(dir, "templates");
const templates = join(dir, ".hyperframes", "template-pulls");
const own = readdirSync(templates).find((name) => /^sample\.part-\d+-/.test(name))!;
const copy = join(templates, `sample.part-${pid}-${suffix}`);
cpSync(join(templates, own), copy, { recursive: true });
@@ -229,7 +229,7 @@ describe("pullTemplate", () => {
it("leaves every sibling a pull did not mark: folders, files, symlinks and unmarked old parts", async () => {
serve({});
const templates = join(dir, "templates");
const templates = join(dir, ".hyperframes", "template-pulls");
const outside = await partLeftBy(deadPid(), "Ou7sid");
const target = join(dir, "outside-target");
cpSync(outside, target, { recursive: true });
@@ -263,7 +263,9 @@ describe("pullTemplate", () => {
void pullTemplate("sample", dir);
await unpacking;
const parts = () =>
readdirSync(join(dir, "templates")).filter((name) => name.includes(".part-"));
readdirSync(join(dir, ".hyperframes", "template-pulls")).filter((name) =>
name.includes(".part-"),
);
expect(parts()).toHaveLength(1);
const stop = process.listeners("SIGINT").find((listener) => !before.has(listener));
stop?.("SIGINT");
@@ -272,6 +274,28 @@ describe("pullTemplate", () => {
expect(process.listeners("SIGINT").filter((listener) => !before.has(listener))).toEqual([]);
});
// Feedback, Windows: Desktop's project history read the half-unpacked files beside templates/<slug>, and the final
// rename failed with EPERM while it held one. The unpack now happens where history never looks.
it("unpacks under .hyperframes, so templates/ holds nothing but finished templates", async () => {
serve({});
let stop!: ExecFileCallback;
const unpacking = new Promise<void>((started) => {
unpackGate.hold = (done) => {
stop = done;
started();
};
});
const pulling = pullTemplate("sample", dir).catch(() => undefined);
await unpacking;
const parts = (folder: string) =>
existsSync(folder) ? readdirSync(folder).filter((name) => name.includes(".part-")) : [];
expect(parts(join(dir, "templates"))).toEqual([]);
expect(parts(join(dir, ".hyperframes", "template-pulls"))).toHaveLength(1);
unpackGate.hold = null;
stop(new Error("stopped"), "", "");
await pulling;
});
it("names the unreachable host instead of a bare fetch failure when offline", async () => {
vi.stubGlobal(
"fetch",
+16 -10
View File
@@ -179,22 +179,27 @@ function pullOwner(path: string, name: string, dest: string): number | null {
return pid !== null && markedBy(readPullMarker(path), pid) ? pid : null;
}
function removeAbandonedParts(dest: string): void {
const parent = dirname(dest);
if (!existsSync(parent)) return;
for (const entry of readdirSync(parent, { withFileTypes: true })) {
function removeAbandonedParts(stage: string, dest: string): void {
if (!existsSync(stage)) return;
for (const entry of readdirSync(stage, { withFileTypes: true })) {
if (!entry.isDirectory()) continue;
const path = join(parent, entry.name);
const path = join(stage, entry.name);
const pid = pullOwner(path, entry.name, dest);
if (pid !== null && !processIsAlive(pid)) rmSync(path, { recursive: true, force: true });
}
}
/** Unpacks the pack beside `dest` and moves it in whole, so `dest` only ever holds a complete template. */
async function unpack(data: Buffer, dest: string, pack: TemplatePack): Promise<void> {
/** Unpacks in `stage`, which watchers skip (a held file fails the rename on Windows), then moves it in whole. */
async function unpack(
data: Buffer,
dest: string,
stage: string,
pack: TemplatePack,
): Promise<void> {
makeOutputFolder(dirname(dest));
removeAbandonedParts(dest);
const work = createStagingDir(dirname(dest), `${partPrefix(dest)}${process.pid}-`);
makeOutputFolder(stage);
removeAbandonedParts(stage, dest);
const work = createStagingDir(stage, `${partPrefix(dest)}${process.pid}-`);
const stopWatching = removeOnStop(work);
try {
writeFileSync(join(work, PULL_MARKER), JSON.stringify({ owner: PULL_OWNER, pid: process.pid }));
@@ -220,7 +225,8 @@ export async function pullTemplate(slug: string, dir: string): Promise<string> {
);
const dest = resolve(dir, "templates", slug);
if (alreadyPulled(dest, pack)) return dest;
await unpack(await download(await bundleUrl(pack), pack), dest, pack);
const stage = resolve(dir, ".hyperframes", "template-pulls");
await unpack(await download(await bundleUrl(pack), pack), dest, stage, pack);
trackSkillCommandRan({ skill: STYLE_SAMPLING, command: "templates pull" });
return dest;
}