forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrender.ts
More file actions
75 lines (67 loc) · 2.42 KB
/
Copy pathrender.ts
File metadata and controls
75 lines (67 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import Handlebars from 'handlebars';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
// Register custom Handlebars helpers
Handlebars.registerHelper('eq', (a: unknown, b: unknown) => a === b);
Handlebars.registerHelper('includes', (array: unknown[], value: unknown) => {
if (!Array.isArray(array)) return false;
return array.includes(value);
});
Handlebars.registerHelper('snakeCase', (str: string) => {
return str.replace(/[^a-zA-Z0-9]/g, '_').toLowerCase();
});
/**
* Renames template files to their actual names.
* e.g., "gitignore.template" -> ".gitignore"
*/
function resolveTemplateName(filename: string): string {
if (filename === 'gitignore.template') return '.gitignore';
if (filename === 'npmignore.template') return '.npmignore';
if (filename === 'dockerignore.template') return '.dockerignore';
return filename;
}
/**
* Recursively copies a directory from src to dest.
* Handles template file renaming (e.g., gitignore.template -> .gitignore).
*/
export async function copyDir(src: string, dest: string): Promise<void> {
await fs.mkdir(dest, { recursive: true });
const entries = await fs.readdir(src, { withFileTypes: true });
for (const entry of entries) {
const srcPath = path.join(src, entry.name);
const destName = resolveTemplateName(entry.name);
const destPath = path.join(dest, destName);
if (entry.isDirectory()) {
await copyDir(srcPath, destPath);
} else {
await fs.copyFile(srcPath, destPath);
}
}
}
/**
* Recursively copies a directory, rendering Handlebars templates.
*/
export async function copyAndRenderDir<T extends object>(
src: string,
dest: string,
data: T,
options?: { exclude?: Set<string> }
): Promise<void> {
await fs.mkdir(dest, { recursive: true });
const entries = await fs.readdir(src, { withFileTypes: true });
for (const entry of entries) {
const destName = resolveTemplateName(entry.name);
if (options?.exclude?.has(destName)) continue;
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, destName);
if (entry.isDirectory()) {
await copyAndRenderDir(srcPath, destPath, data);
} else {
const content = await fs.readFile(srcPath, 'utf-8');
const template = Handlebars.compile(content);
const rendered = template(data);
await fs.mkdir(path.dirname(destPath), { recursive: true });
await fs.writeFile(destPath, rendered, 'utf-8');
}
}
}