Skip to content

Commit 61fd537

Browse files
committed
fix(cli): read BOM-prefixed files for render --batch, lambda policy and transcripts
1 parent c1c9f88 commit 61fd537

6 files changed

Lines changed: 35 additions & 6 deletions

File tree

‎packages/cli/src/commands/batchRender.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@ describe("prepareBatchRender", () => {
104104
expect(prepared.manifestPath).toBe(resolve(outDir, "manifest.json"));
105105
});
106106

107+
it("reads a --batch file saved with a UTF-8 BOM", () => {
108+
const batchPath = writeJson("bom-rows.json", '\uFEFF[{"name":"Alice"}]');
109+
const prepared = prepareBatchRender({
110+
batchPath,
111+
outputTemplate: join(tmpDir, "renders", "{name}.mp4"),
112+
indexPath: writeIndex(),
113+
strictVariables: false,
114+
quiet: true,
115+
json: false,
116+
});
117+
expect(prepared.rows).toHaveLength(1);
118+
});
119+
107120
it("rejects output collisions before rendering", () => {
108121
const batchPath = writeJson("rows.json", '[{"name":"Alice"},{"name":"Bob"}]');
109122
const error = expectBatchError(

‎packages/cli/src/commands/batchRender.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { failCommand } from "../utils/commandResult.js";
2-
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
2+
import { mkdirSync, writeFileSync } from "node:fs";
3+
import { readJsonFileText } from "../utils/jsonFlag.js";
34
import { dirname, join, resolve, sep } from "node:path";
45
import { c } from "../ui/colors.js";
56
import { errorBox } from "../ui/format.js";
@@ -241,7 +242,7 @@ function reportBatchVariableIssues(
241242

242243
export function prepareBatchRender(options: PrepareBatchRenderOptions): PreparedBatchRender {
243244
const batchPath = resolve(options.batchPath);
244-
const read = options.readFile ?? ((path: string) => readFileSync(path, "utf8"));
245+
const read = options.readFile ?? readJsonFileText;
245246
let raw: string;
246247
try {
247248
raw = read(batchPath);

‎packages/cli/src/commands/lambda/policies.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ describe("policies — validatePolicy", () => {
8585
expect(result.granted).toEqual(allRequiredActions());
8686
});
8787

88+
it("reads a policy saved with a UTF-8 BOM", () => {
89+
const path = join(workdir, "bom-policy.json");
90+
writeFileSync(path, `\uFEFF${JSON.stringify({ Version: "2012-10-17", Statement: [] })}`);
91+
expect(validatePolicy(path).granted).toEqual([]);
92+
});
93+
8894
it("reports specific missing actions", () => {
8995
const path = writePolicy({
9096
Version: "2012-10-17",

‎packages/cli/src/commands/lambda/policies.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { CliUsageError, failUsage, setCommandExitCode } from "../../utils/comman
2121
* checked-in policy still covers the CLI's needs with `policies validate`.
2222
*/
2323

24-
import { readFileSync } from "node:fs";
24+
import { readJsonFileText } from "../../utils/jsonFlag.js";
2525
import { c } from "../../ui/colors.js";
2626
import { failureMessage, jsonFailure } from "../../utils/jsonError.js";
2727

@@ -334,7 +334,7 @@ export interface ValidateResult {
334334
* not by our matcher. We end-anchor only.
335335
*/
336336
export function validatePolicy(policyPath: string): ValidateResult {
337-
const raw = readFileSync(policyPath, "utf-8");
337+
const raw = readJsonFileText(policyPath);
338338
const parsed = JSON.parse(raw) as { Statement?: unknown };
339339
const statements: Array<{
340340
Effect?: string;

‎packages/cli/src/whisper/normalize.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ describe("detectFormat", () => {
4040
expect(detectFormat(path)).toBe("vtt");
4141
});
4242

43+
it("detects a JSON transcript saved with a UTF-8 BOM", () => {
44+
const path = tmpFile(
45+
"bom.json",
46+
`\uFEFF${JSON.stringify({ transcription: [{ offsets: { from: 0, to: 1000 }, text: " Hi", tokens: [{ text: " Hi", offsets: { from: 0, to: 1000 }, p: 0.9 }] }] })}`,
47+
);
48+
expect(detectFormat(path)).toBe("whisper-cpp");
49+
});
50+
4351
it("detects whisper-cpp JSON", () => {
4452
const path = tmpFile(
4553
"transcript.json",

‎packages/cli/src/whisper/normalize.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { readFileSync, readdirSync, writeFileSync } from "node:fs";
2+
import { readJsonFileText } from "../utils/jsonFlag.js";
23
import { extname, join } from "node:path";
34
import { parseExpressionAt, tokenizer } from "acorn";
45
import { parseHTML } from "linkedom";
@@ -40,7 +41,7 @@ export function detectFormat(filePath: string): TranscriptFormat {
4041
const ext = extname(filePath).toLowerCase();
4142
if (ext === ".srt") return "srt";
4243
if (ext === ".vtt") return "vtt";
43-
if (ext === ".json") return detectJsonFormat(JSON.parse(readFileSync(filePath, "utf-8")));
44+
if (ext === ".json") return detectJsonFormat(JSON.parse(readJsonFileText(filePath)));
4445
throw new Error(`Unsupported transcript file extension: ${ext}. Use .json, .srt, or .vtt`);
4546
}
4647

@@ -510,7 +511,7 @@ export function formatVtt(words: Word[], opts?: WordsToCuesOptions): string {
510511
*/
511512
export function loadTranscript(filePath: string): { words: Word[]; format: TranscriptFormat } {
512513
const ext = extname(filePath).toLowerCase();
513-
const content = readFileSync(filePath, "utf-8");
514+
const content = readJsonFileText(filePath);
514515

515516
if (ext === ".srt") {
516517
const words = parseSrt(content).map((w, i) => ({ ...w, id: w.id ?? `w${i}` }));

0 commit comments

Comments
 (0)