kubricate
Version:
A TypeScript framework for building reusable, type-safe Kubernetes infrastructure — without the YAML mess.
62 lines • 1.9 kB
JavaScript
import fs from 'node:fs';
import path from 'node:path';
import { MARK_BULLET, MARK_CHECK } from '../../internal/constant.js';
export class GenerateRunner {
options;
generateOptions;
renderedFiles;
logger;
constructor(options, generateOptions, renderedFiles, logger) {
this.options = options;
this.generateOptions = generateOptions;
this.renderedFiles = renderedFiles;
this.logger = logger;
}
async run() {
if (this.generateOptions.cleanOutputDir) {
this.cleanOutputDir(path.join(this.options.root ?? '', this.generateOptions.outputDir));
}
const stats = {
written: 0
};
this.logger.info(`Rendering with output mode "${this.generateOptions.outputMode}"`);
if (this.generateOptions.cleanOutputDir) {
this.logger.info(`Cleaning output directory: ${this.options.outDir}`);
}
this.logger.log(`\nGenerating stacks...`);
for (const file of this.renderedFiles) {
this.processOutput(file, stats);
}
if (stats.written === 0) {
this.logger.warn(`No files generated.`);
return;
}
this.logger.log(`\n${MARK_CHECK} Generated ${stats.written} file${stats.written > 1 ? 's' : ''} into "${this.generateOptions.outputDir}/"`);
}
cleanOutputDir(dir) {
if (fs.existsSync(dir)) {
fs.rmSync(dir, {
recursive: true,
force: true
});
}
}
ensureDir(filePath) {
const dir = path.dirname(filePath);
fs.mkdirSync(dir, {
recursive: true
});
}
processOutput(file, stats) {
if (this.options.stdout) {
console.log(file.content);
return;
}
const outputPath = path.join(this.options.root ?? '', file.filePath);
this.ensureDir(outputPath);
fs.writeFileSync(outputPath, file.content);
this.logger.log(`${MARK_BULLET} Written: ${outputPath}`);
stats.written++;
}
}
//# sourceMappingURL=GenerateRunner.js.map