fix(media-use): escape control characters in the adopt skip line (#8087)

scanExistingAssets printed the project's own file and folder names to
stderr as they are. A name holding escape sequences could rewrite or
hide terminal lines. The line now goes through terminalText, like the
rest of media-use's output.
This commit is contained in:
Michael Wang
2026-11-17 17:19:34 +00:00
committed by GitHub
parent 820d63d7f9
commit 2749aafb36
2 changed files with 22 additions and 2 deletions
+2 -1
View File
@@ -4,6 +4,7 @@ import { readManifest, appendRecord, nextId } from "./manifest.mjs";
import { regenerateIndex } from "./index-gen.mjs";
import { probe } from "./probe.mjs";
import { matchTokens } from "./match.mjs";
import { terminalText } from "./terminal-text.mjs";
const AUDIO_EXT = new Set([".mp3", ".wav", ".ogg", ".m4a", ".aac"]);
const IMAGE_EXT = new Set([".jpg", ".jpeg", ".png", ".gif", ".webp", ".svg", ".ico"]);
@@ -59,7 +60,7 @@ export function scanExistingAssets(projectDir) {
if (stat.size === 0) {
// A 0-byte asset would register clean but fail at render — skip it loudly
// rather than adopt a broken file.
console.error(`media-use: skipping 0-byte asset assets/${rel}`);
console.error(terminalText(`media-use: skipping 0-byte asset assets/${rel}`));
continue;
}
const meta = probe(fullPath);
+20 -1
View File
@@ -2,7 +2,7 @@ import { strict as assert } from "node:assert";
import { mkdtempSync, rmSync, mkdirSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { findExistingAsset, adoptExistingAssets } from "./adopt.mjs";
import { findExistingAsset, adoptExistingAssets, scanExistingAssets } from "./adopt.mjs";
let tmp;
function setup() {
@@ -122,6 +122,25 @@ function runTests() {
cleanup();
});
test("prints a skipped file name from the project with control characters escaped", () => {
setup();
const name = "bad\x1b]0;owned\x07\r\nok.mp3";
mkdirSync(join(tmp, "assets", "dir\x1b[2J"), { recursive: true });
writeFileSync(join(tmp, "assets", "dir\x1b[2J", name), "");
const lines = [];
const original = console.error;
console.error = (line) => lines.push(String(line));
try {
assert.deepEqual(scanExistingAssets(tmp), []);
} finally {
console.error = original;
}
assert.deepEqual(lines, [
"media-use: skipping 0-byte asset assets/dir\\x1b[2J/bad\\x1b]0;owned\\x07\\x0d\\x0aok.mp3",
]);
cleanup();
});
let passed = 0;
let failed = 0;
for (const { name, fn } of tests) {