Skip to content

Commit 5bcf1ed

Browse files
committed
refactor(cli): extract copy folder fn
1 parent 65fe526 commit 5bcf1ed

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

apps/cli/scripts/generate-source-files.ts

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { existsSync, promises as fs } from 'fs';
44
import path from 'path';
55
import { COMPONENTS } from '../src/items/components';
66
import { TEMPLATES } from '../src/items/templates';
7+
import { copyFolder } from '../src/utils/copy-folder';
78

89
async function main() {
910
for (const template of TEMPLATES) {
10-
await copyFolder(template.path);
11+
await copyFolder(template.path, template.name);
1112
}
1213
for (const comp of COMPONENTS) {
1314
if (Array.isArray(comp.paths)) {
@@ -34,33 +35,3 @@ async function writeFiles(paths: Array<{ from: string; to: { folder: string; fil
3435
}
3536
}
3637
}
37-
38-
async function copyFolder(src: string, destPath?: string) {
39-
if (!existsSync(src)) {
40-
throw new Error(`Source folder does not exist: ${src}`);
41-
}
42-
43-
const paths = src.split('/');
44-
const folderName = paths[paths.length - 1];
45-
46-
const dest = destPath ?? path.join('__generated', folderName);
47-
48-
if (!existsSync(dest)) {
49-
await fs.mkdir(dest, { recursive: true });
50-
}
51-
52-
const entries = await fs.readdir(src, { withFileTypes: true });
53-
54-
for (const entry of entries) {
55-
const srcPath = path.join(src, entry.name);
56-
const destPath = path.join(dest, entry.name);
57-
58-
if (entry.isDirectory()) {
59-
// Recursively copy subdirectories
60-
await copyFolder(srcPath, destPath);
61-
} else if (entry.isFile()) {
62-
// Copy files
63-
await fs.copyFile(srcPath, destPath);
64-
}
65-
}
66-
}

apps/cli/src/utils/copy-folder.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { existsSync, promises as fs } from 'fs';
2+
import path from 'path';
3+
4+
export async function copyFolder(src: string, name: string, destPath?: string) {
5+
if (!existsSync(src)) {
6+
throw new Error(`Source folder does not exist: ${src}`);
7+
}
8+
9+
const dest = destPath ?? path.join('__generated', name);
10+
11+
if (!existsSync(dest)) {
12+
await fs.mkdir(dest, { recursive: true });
13+
}
14+
15+
const entries = await fs.readdir(src, { withFileTypes: true });
16+
17+
for (const entry of entries) {
18+
const srcPath = path.join(src, entry.name);
19+
const destPath = path.join(dest, entry.name);
20+
21+
if (entry.isDirectory()) {
22+
// Recursively copy subdirectories
23+
await copyFolder(srcPath, destPath);
24+
} else if (entry.isFile()) {
25+
// Copy files
26+
await fs.copyFile(srcPath, destPath);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)