Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions packages/cli/src/utils/copyDir.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, parse } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { copyDirSync } from "./copyDir.js";

const made: string[] = [];
afterEach(() => {
for (const dir of made.splice(0)) rmSync(dir, { recursive: true, force: true });
});

function source(): string {
const dir = mkdtempSync(join(tmpdir(), "copydir-src-"));
made.push(dir);
mkdirSync(join(dir, "compositions", "café"), { recursive: true });
writeFileSync(join(dir, "index.html"), "<html></html>");
writeFileSync(join(dir, "compositions", "café", "scène.html"), "<div></div>");
return dir;
}

describe("copyDirSync", () => {
it("copies every file, non-ASCII names included, into a folder that already holds others", () => {
const dest = mkdtempSync(join(tmpdir(), "copydir-dest-"));
made.push(dest);
writeFileSync(join(dest, "hyperframes.json"), "{}");
copyDirSync(source(), dest);
expect(readFileSync(join(dest, "index.html"), "utf8")).toBe("<html></html>");
expect(readFileSync(join(dest, "compositions", "café", "scène.html"), "utf8")).toBe(
"<div></div>",
);
expect(readFileSync(join(dest, "hyperframes.json"), "utf8")).toBe("{}");
});

// windows-latest: fs.cpSync into this folder ends the process with 0xC0000409, so this test fails without the fix.
it.skipIf(process.platform !== "win32")(
"copies into an existing folder directly under a drive root",
() => {
const dest = join(parse(tmpdir()).root, `copydir-root-${process.pid}-${Date.now()}`);
mkdirSync(dest);
made.push(dest);
copyDirSync(source(), dest);
expect(readFileSync(join(dest, "index.html"), "utf8")).toBe("<html></html>");
},
);
});
20 changes: 12 additions & 8 deletions packages/cli/src/utils/copyDir.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { cpSync, type CopySyncOptions } from "node:fs";
import { copyFileSync, mkdirSync, readdirSync, readlinkSync, symlinkSync } from "node:fs";
import { join } from "node:path";

export function copyDirSync(
src: string,
dest: string,
options: Omit<CopySyncOptions, "recursive" | "filter"> = {},
): void {
// Any filter keeps Node 22 off its native copy, which writes nothing under a non-ASCII path on Windows.
cpSync(src, dest, { ...options, recursive: true, filter: () => true });
/** Not fs.cpSync: Node dies (0xC0000409) copying into an existing folder directly under a Windows drive root. */
export function copyDirSync(src: string, dest: string): void {
mkdirSync(dest, { recursive: true });
for (const entry of readdirSync(src, { withFileTypes: true })) {
const from = join(src, entry.name);
const to = join(dest, entry.name);
if (entry.isDirectory()) copyDirSync(from, to);
else if (entry.isSymbolicLink()) symlinkSync(readlinkSync(from), to);
else copyFileSync(from, to);
}
}
Loading