Skip to content

Commit 9e6b39a

Browse files
committed
hook up type of template with the generation command
1 parent efbe093 commit 9e6b39a

2 files changed

Lines changed: 45 additions & 39 deletions

File tree

scripts/docker-build.ts

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// docker-build: builds per-function Docker images using Dockerfile templates.
1+
// docker-build: builds per-function Docker images from generated Dockerfiles.
22
//
3-
// Reads each function's handler.json to determine template type, processes
4-
// the template Dockerfile with placeholder replacement, and runs docker build.
3+
// Expects `pnpm generate` to have been run first, which produces
4+
// generated/<name>/Dockerfile from the template with placeholders resolved.
55
//
66
// Usage:
77
// node --experimental-strip-types scripts/docker-build.ts --all
@@ -14,9 +14,8 @@ const { execSync } = require('child_process') as typeof import('child_process');
1414

1515
const ROOT: string = process.cwd();
1616
const FUNCTIONS_DIR: string = path.resolve(ROOT, 'functions');
17-
const TEMPLATES_DIR: string = path.resolve(ROOT, 'templates');
17+
const GENERATED_DIR: string = path.resolve(ROOT, 'generated');
1818

19-
const DEFAULT_TEMPLATE = 'node-graphql';
2019
const DEFAULT_REGISTRY = 'ghcr.io/constructive-io';
2120

2221
interface FunctionManifest {
@@ -72,34 +71,21 @@ function readManifest(fnDir: string): FunctionManifest {
7271
// --- Docker build ---
7372

7473
function buildDockerImage(fnName: string, manifest: FunctionManifest): void {
75-
const templateType = manifest.type || DEFAULT_TEMPLATE;
76-
const dockerfilePath = path.join(TEMPLATES_DIR, templateType, 'Dockerfile');
74+
const dockerfilePath = path.join(GENERATED_DIR, fnName, 'Dockerfile');
7775

7876
if (!fs.existsSync(dockerfilePath)) {
79-
console.error(`No Dockerfile found for template "${templateType}" at ${dockerfilePath}`);
77+
console.error(`No Dockerfile found at ${dockerfilePath}. Run "pnpm generate" first.`);
8078
process.exit(1);
8179
}
8280

83-
// Read and process Dockerfile template
84-
const dockerfile = fs.readFileSync(dockerfilePath, 'utf-8')
85-
.replace(/\{\{name\}\}/g, manifest.name);
86-
8781
const imageName = `${registry}/${manifest.name}-fn`;
88-
const tmpDockerfile = path.join(ROOT, `.Dockerfile.${manifest.name}`);
89-
90-
fs.writeFileSync(tmpDockerfile, dockerfile, 'utf-8');
91-
92-
try {
93-
console.log(`\nBuilding ${imageName}:${tag}...`);
94-
execSync(
95-
`docker build -t ${imageName}:${tag} -f ${tmpDockerfile} .`,
96-
{ cwd: ROOT, stdio: 'inherit' }
97-
);
98-
console.log(`Built ${imageName}:${tag}`);
99-
} finally {
100-
// Clean up temp Dockerfile
101-
try { fs.unlinkSync(tmpDockerfile); } catch { /* ignore */ }
102-
}
82+
83+
console.log(`\nBuilding ${imageName}:${tag}...`);
84+
execSync(
85+
`docker build -t ${imageName}:${tag} -f ${dockerfilePath} .`,
86+
{ cwd: ROOT, stdio: 'inherit' }
87+
);
88+
console.log(`Built ${imageName}:${tag}`);
10389
}
10490

10591
// --- Main ---

scripts/generate.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ const TEMPLATES_DIR: string = path.resolve(ROOT, 'templates');
1515

1616
const DEFAULT_TEMPLATE = 'node-graphql';
1717

18-
// Files from the template that get copied into generated/<name>/
19-
const WORKSPACE_FILES = ['package.json', 'tsconfig.json', 'index.ts'];
20-
2118
interface FunctionManifest {
2219
name: string;
2320
version: string;
@@ -70,6 +67,24 @@ function resolveTemplateDir(manifest: FunctionManifest): string {
7067
return templateDir;
7168
}
7269

70+
// --- Template file discovery ---
71+
72+
function walkTemplateFiles(dir: string, base: string = ''): string[] {
73+
const results: string[] = [];
74+
const entries = fs.readdirSync(dir) as string[];
75+
for (const entry of entries) {
76+
const fullPath = path.join(dir, entry);
77+
const relPath = base ? path.join(base, entry) : entry;
78+
const stat = fs.statSync(fullPath);
79+
if (stat.isDirectory()) {
80+
results.push(...walkTemplateFiles(fullPath, relPath));
81+
} else {
82+
results.push(relPath);
83+
}
84+
}
85+
return results;
86+
}
87+
7388
// --- Placeholder replacement ---
7489

7590
function replacePlaceholders(content: string, manifest: FunctionManifest): string {
@@ -183,18 +198,23 @@ function main(): void {
183198

184199
console.log(` Generating ${fnName} (template: ${manifest.type || DEFAULT_TEMPLATE})...`);
185200

186-
// Copy and process template files
187-
for (const fileName of WORKSPACE_FILES) {
188-
const templateFile = path.join(templateDir, fileName);
189-
if (!fs.existsSync(templateFile)) {
190-
console.warn(` ! Template file missing: ${fileName}`);
191-
continue;
201+
// Walk all template files and copy/process them
202+
const templateFiles = walkTemplateFiles(templateDir);
203+
for (const relPath of templateFiles) {
204+
const templateFile = path.join(templateDir, relPath);
205+
const outputFile = path.join(genDir, relPath);
206+
207+
// Ensure subdirectories exist (e.g., k8s/)
208+
const outputDir = path.dirname(outputFile);
209+
if (!fs.existsSync(outputDir)) {
210+
fs.mkdirSync(outputDir, { recursive: true });
192211
}
193212

194213
const templateContent = fs.readFileSync(templateFile, 'utf-8');
195-
const processed = processTemplateFile(fileName, templateContent, manifest, fnDir);
196-
const changed = writeIfChanged(path.join(genDir, fileName), processed);
197-
if (changed) console.log(` - ${fileName}`);
214+
const baseName = path.basename(relPath);
215+
const processed = processTemplateFile(baseName, templateContent, manifest, fnDir);
216+
const changed = writeIfChanged(outputFile, processed);
217+
if (changed) console.log(` - ${relPath}`);
198218
}
199219

200220
// Symlink handler.ts

0 commit comments

Comments
 (0)