Skip to content

Commit aeacdb0

Browse files
committed
refactor(cli): use fs.cp for bundled docs copy
Replace the hand-rolled recursive walker with the same fs.cp({ recursive: true, filter }) pattern already used in packages/core/build.ts. Drops 41 lines, the redundant per-file mkdir, and the sync readdirSync inside an async function.
1 parent 41053c2 commit aeacdb0

1 file changed

Lines changed: 11 additions & 49 deletions

File tree

packages/cli/build.ts

Lines changed: 11 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
import { execSync } from 'node:child_process';
2222
import { existsSync, readdirSync, statSync } from 'node:fs';
23-
import { copyFile, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
24-
import { dirname, join } from 'node:path';
23+
import { copyFile, cp, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
24+
import { dirname, join, relative } from 'node:path';
2525
import { fileURLToPath } from 'node:url';
2626
import { parseArgs } from 'node:util';
2727

@@ -402,12 +402,17 @@ async function copyBundledDocs() {
402402
return;
403403
}
404404

405-
// Clean and recreate target directory
405+
const skipPrefixes = ['node_modules', '.vitepress/cache', '.vitepress/dist'];
406406
await rm(docsTargetDir, { recursive: true, force: true });
407-
await mkdir(docsTargetDir, { recursive: true });
407+
await cp(docsSourceDir, docsTargetDir, {
408+
recursive: true,
409+
filter: (src) => {
410+
const rel = relative(docsSourceDir, src).replaceAll('\\', '/');
411+
return !skipPrefixes.some((prefix) => rel === prefix || rel.startsWith(`${prefix}/`));
412+
},
413+
});
408414

409-
const copied = await copyDocsTree(docsSourceDir, docsTargetDir);
410-
console.log(` Copied ${copied} docs files to docs/ (with paths preserved)`);
415+
console.log(' Copied docs to docs/ (with paths preserved)');
411416
}
412417

413418
async function syncReadmeFromRoot() {
@@ -445,49 +450,6 @@ function splitReadme(content: string, label: string) {
445450
};
446451
}
447452

448-
async function copyDocsTree(
449-
docsSourceDir: string,
450-
docsTargetDir: string,
451-
relativePath = '',
452-
): Promise<number> {
453-
let copied = 0;
454-
const currentDir = join(docsSourceDir, relativePath);
455-
456-
for (const entry of readdirSync(currentDir, { withFileTypes: true })) {
457-
const entryRelPath = relativePath ? join(relativePath, entry.name) : entry.name;
458-
const normalizedPath = entryRelPath.replaceAll('\\', '/');
459-
460-
if (shouldSkipDocsPath(normalizedPath)) {
461-
continue;
462-
}
463-
464-
const sourcePath = join(docsSourceDir, entryRelPath);
465-
const targetPath = join(docsTargetDir, entryRelPath);
466-
467-
if (entry.isDirectory()) {
468-
await mkdir(targetPath, { recursive: true });
469-
copied += await copyDocsTree(docsSourceDir, docsTargetDir, entryRelPath);
470-
continue;
471-
}
472-
473-
if (!entry.isFile()) {
474-
continue;
475-
}
476-
477-
await mkdir(dirname(targetPath), { recursive: true });
478-
await copyFile(sourcePath, targetPath);
479-
copied++;
480-
}
481-
482-
return copied;
483-
}
484-
485-
function shouldSkipDocsPath(relativePath: string) {
486-
return ['node_modules', '.vitepress/cache', '.vitepress/dist'].some(
487-
(prefix) => relativePath === prefix || relativePath.startsWith(`${prefix}/`),
488-
);
489-
}
490-
491453
type ExportValue =
492454
| string
493455
| {

0 commit comments

Comments
 (0)