mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(cli): templates pull reuses only a template it pulled, not any path in the way (#7116)
* fix(cli): templates pull reuses only a template it pulled, not any path in the way * fix(cli): templates pull names an older pull, with the reasons in names, not comments
This commit is contained in:
@@ -69,13 +69,53 @@ describe("pullTemplate", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("leaves a template that is already there alone and spends no call", async () => {
|
||||
it("leaves a template it pulled before alone and spends no call", async () => {
|
||||
const fetchMock = serve({});
|
||||
mkdirSync(join(dir, "templates", "sample"), { recursive: true });
|
||||
await pullTemplate("sample", dir);
|
||||
fetchMock.mockClear();
|
||||
await pullTemplate("sample", dir);
|
||||
expect(fetchMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("fills an empty folder where the template goes", async () => {
|
||||
serve({});
|
||||
mkdirSync(join(dir, "templates", "sample"), { recursive: true });
|
||||
const path = await pullTemplate("sample", dir);
|
||||
expect(readFileSync(join(path, "SKILL.md"), "utf8")).toBe("# sample\n");
|
||||
});
|
||||
|
||||
it("stops at a file or someone else's folder where the template goes, and leaves it as it was", async () => {
|
||||
const fetchMock = serve({});
|
||||
const dest = join(dir, "templates", "sample");
|
||||
mkdirSync(join(dir, "templates"), { recursive: true });
|
||||
writeFileSync(dest, "notes");
|
||||
await expect(pullTemplate("sample", dir)).rejects.toThrow("not this template");
|
||||
expect(readFileSync(dest, "utf8")).toBe("notes");
|
||||
rmSync(dest);
|
||||
mkdirSync(dest);
|
||||
writeFileSync(join(dest, "mine.md"), "mine");
|
||||
await expect(pullTemplate("sample", dir)).rejects.toThrow("not this template");
|
||||
expect(readFileSync(join(dest, "mine.md"), "utf8")).toBe("mine");
|
||||
expect(fetchMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("names a pull of an older pin as such and leaves it", async () => {
|
||||
const fetchMock = serve({});
|
||||
const dest = join(dir, "templates", "sample");
|
||||
mkdirSync(dest, { recursive: true });
|
||||
writeFileSync(join(dest, ".template-sha256"), "0".repeat(64));
|
||||
await expect(pullTemplate("sample", dir)).rejects.toThrow("older pull of this template");
|
||||
expect(readFileSync(join(dest, ".template-sha256"), "utf8")).toBe("0".repeat(64));
|
||||
expect(fetchMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("keeps an empty folder when the pull is refused", async () => {
|
||||
serve({ status: 402 });
|
||||
mkdirSync(join(dir, "templates", "sample"), { recursive: true });
|
||||
await expect(pullTemplate("sample", dir)).rejects.toThrow("billing subscribe");
|
||||
expect(existsSync(join(dir, "templates", "sample"))).toBe(true);
|
||||
});
|
||||
|
||||
it("turns a 402 into the upgrade step and writes nothing", async () => {
|
||||
serve({ status: 402 });
|
||||
await expect(pullTemplate("sample", dir)).rejects.toThrow("billing subscribe");
|
||||
|
||||
@@ -1,6 +1,17 @@
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { existsSync, mkdirSync, mkdtempSync, renameSync, rmSync } from "node:fs";
|
||||
import {
|
||||
existsSync,
|
||||
mkdirSync,
|
||||
mkdtempSync,
|
||||
readFileSync,
|
||||
readdirSync,
|
||||
renameSync,
|
||||
rmSync,
|
||||
rmdirSync,
|
||||
statSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
import { dirname, join, resolve } from "node:path";
|
||||
import { resolveCloudAuthHeaders } from "../cloud/auth.js";
|
||||
import { billingBaseUrl } from "../heygen/plan.js";
|
||||
@@ -95,8 +106,10 @@ function assertSafeEntries(data: Buffer): void {
|
||||
throw new PullRefused(`The template holds an unsafe path (${name}); nothing was unpacked.`);
|
||||
}
|
||||
|
||||
const PULLED_HASH = ".template-sha256";
|
||||
|
||||
/** Unpacks the pack beside `dest` and moves it in whole, so `dest` only ever holds a complete template. */
|
||||
function unpack(data: Buffer, dest: string): void {
|
||||
function unpack(data: Buffer, dest: string, pack: TemplatePack): void {
|
||||
mkdirSync(dirname(dest), { recursive: true });
|
||||
const work = mkdtempSync(`${dest}.part-`);
|
||||
try {
|
||||
@@ -104,13 +117,15 @@ function unpack(data: Buffer, dest: string): void {
|
||||
assertSafeEntries(data);
|
||||
mkdirSync(out);
|
||||
execFileSync("tar", ["-xzf", "-", "-C", out, "--no-same-owner"], { input: data });
|
||||
writeFileSync(join(out, PULLED_HASH), pack.sha256);
|
||||
removeEmptyPlaceholder(dest);
|
||||
renameSync(out, dest);
|
||||
} finally {
|
||||
rmSync(work, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
/** Fetches the template into `<dir>/templates/<slug>/` and returns that path; one already there stays as it is. */
|
||||
/** Fetches the template into `<dir>/templates/<slug>/` and returns that path; one this pull left there is kept. */
|
||||
export async function pullTemplate(slug: string, dir: string): Promise<string> {
|
||||
const pack = TEMPLATES[slug];
|
||||
if (!pack)
|
||||
@@ -118,11 +133,29 @@ export async function pullTemplate(slug: string, dir: string): Promise<string> {
|
||||
`No template "${slug}". Templates: ${Object.keys(TEMPLATES).join(", ")}.`,
|
||||
);
|
||||
const dest = resolve(dir, "templates", slug);
|
||||
if (existsSync(dest)) return dest;
|
||||
unpack(await download(await bundleUrl(pack), pack), dest);
|
||||
if (alreadyPulled(dest, pack)) return dest;
|
||||
unpack(await download(await bundleUrl(pack), pack), dest, pack);
|
||||
return dest;
|
||||
}
|
||||
|
||||
const removeEmptyPlaceholder = (dest: string) => existsSync(dest) && rmdirSync(dest);
|
||||
|
||||
function alreadyPulled(dest: string, pack: TemplatePack): boolean {
|
||||
if (!existsSync(dest)) return false;
|
||||
const folder = statSync(dest).isDirectory();
|
||||
const record = join(dest, PULLED_HASH);
|
||||
const pulledHash = folder && existsSync(record) ? readFileSync(record, "utf8") : null;
|
||||
if (pulledHash === pack.sha256) return true;
|
||||
if (folder && readdirSync(dest).length === 0) return false;
|
||||
if (pulledHash)
|
||||
throw new PullRefused(
|
||||
`${dest} holds an older pull of this template; nothing was downloaded. Move it aside and run the pull again to update.`,
|
||||
);
|
||||
throw new PullRefused(
|
||||
`${dest} is there but is not this template; nothing was downloaded. Move it aside and run the pull again.`,
|
||||
);
|
||||
}
|
||||
|
||||
/** Downloads a style template for a HyperFrames Pro account. The server decides who may have it. */
|
||||
export default defineCommand({
|
||||
meta: {
|
||||
|
||||
Reference in New Issue
Block a user