mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
fix(cli): batch outputs from a template that starts with a placeholder stay in the working folder (#7868)
* fix(cli): batch outputs from a template that starts with a placeholder stay in the working folder
* fix(cli): the bare {path} template is named, not commented
This commit is contained in:
@@ -248,9 +248,10 @@ npx hyperframes render \
|
||||
Every output stays inside the folder written before the first placeholder
|
||||
(`renders/` here). A row value may name a subfolder of it, but a value that
|
||||
leads outside it, such as `../other`, stops the batch before anything renders.
|
||||
A template that starts with a placeholder, such as `--output "{path}"`, names
|
||||
no folder, so each row's value is used as the whole output path, absolute
|
||||
paths included.
|
||||
A template that starts with a placeholder, such as `{name}.mp4`, keeps every
|
||||
output inside the folder you run the command from. Only `--output "{path}"`,
|
||||
the placeholder alone, uses each row's value as the whole output path,
|
||||
absolute paths included.
|
||||
|
||||
Start with the default single-row concurrency. Increase `--batch-concurrency`
|
||||
only after one real render is stable and the machine has enough memory for
|
||||
|
||||
@@ -164,6 +164,40 @@ describe("prepareBatchRender", () => {
|
||||
expect(error.message).toMatch(/Row 0 resolves to/);
|
||||
});
|
||||
|
||||
it.each(["{name}.mp4", join("{name}", "out.mp4")])(
|
||||
"refuses a row value that leaves the working folder when %s starts with a placeholder",
|
||||
(template) => {
|
||||
const batchPath = writeJson("rows.json", JSON.stringify([{ name: "../escaped" }]));
|
||||
const error = expectBatchError(
|
||||
() =>
|
||||
prepareBatchRender({
|
||||
batchPath,
|
||||
outputTemplate: template,
|
||||
indexPath: writeIndex(),
|
||||
strictVariables: false,
|
||||
quiet: true,
|
||||
json: false,
|
||||
}),
|
||||
"Batch output outside --output folder",
|
||||
);
|
||||
expect(error.message).toMatch(/Row 0 resolves to/);
|
||||
},
|
||||
);
|
||||
|
||||
it("keeps a template that starts with a placeholder inside the working folder", () => {
|
||||
const batchPath = writeJson("rows.json", '[{"name":"acme"}]');
|
||||
const prepared = prepareBatchRender({
|
||||
batchPath,
|
||||
outputTemplate: join("{index}", "{name}.mp4"),
|
||||
indexPath: writeIndex(),
|
||||
strictVariables: false,
|
||||
quiet: true,
|
||||
json: false,
|
||||
});
|
||||
|
||||
expect(prepared.rows.map((row) => row.outputPath)).toEqual([resolve("0", "acme.mp4")]);
|
||||
});
|
||||
|
||||
it("keeps a backslash in a row value or file name on every platform", () => {
|
||||
const batchPath = writeJson("rows.json", '[{"name":"a\\\\b"}]');
|
||||
const outDir = join(tmpDir, "renders\\");
|
||||
@@ -178,7 +212,7 @@ describe("prepareBatchRender", () => {
|
||||
expect(prepared.rows[0]?.outputPath).toBe(resolve(`${outDir}a\\b.mp4`));
|
||||
});
|
||||
|
||||
it("lets the rows name the whole path when the template starts with a placeholder", () => {
|
||||
it("lets the rows name the whole path when the template is a bare {path}", () => {
|
||||
const elsewhere = join(tmpDir, "elsewhere", "launch.mp4");
|
||||
const batchPath = writeJson("rows.json", JSON.stringify([{ path: elsewhere }]));
|
||||
const prepared = prepareBatchRender({
|
||||
|
||||
@@ -87,6 +87,7 @@ interface RunBatchRenderOptions {
|
||||
}
|
||||
|
||||
const PLACEHOLDER_RE = /\{([A-Za-z0-9_.-]+)\}/g;
|
||||
const ROWS_NAME_WHOLE_OUTPUT_PATH = "{path}";
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value);
|
||||
@@ -160,9 +161,11 @@ function folderOfPrefix(prefix: string): string {
|
||||
}
|
||||
|
||||
function checkOutputsInsideTemplateFolder(rows: readonly PreparedBatchRow[], template: string) {
|
||||
if (template === ROWS_NAME_WHOLE_OUTPUT_PATH) return;
|
||||
const firstPlaceholder = template.search(PLACEHOLDER_RE);
|
||||
if (firstPlaceholder <= 0) return;
|
||||
const folder = folderOfPrefix(template.slice(0, firstPlaceholder));
|
||||
if (firstPlaceholder < 0) return;
|
||||
const folder =
|
||||
firstPlaceholder === 0 ? resolve() : folderOfPrefix(template.slice(0, firstPlaceholder));
|
||||
for (const row of rows) {
|
||||
const rel = relative(folder, row.outputPath);
|
||||
const outside = rel === "" || rel === ".." || rel.startsWith(`..${sep}`) || isAbsolute(rel);
|
||||
|
||||
Reference in New Issue
Block a user