/* eslint-disable no-console */ import path from 'path' import fs from 'fs-extra' import handlebars from 'handlebars' import { packageName } from './package' type Template = { name: string outputPath: string data: { [key: string]: unknown } } const getTemplatePath = () => { if (__dirname.includes('dist')) { return path.join(__dirname, '..', '..', '..', '..', 'templates') } return path.join(__dirname, '..', '..', '..', 'templates') } export const compileTemplate = ({ name, data, outputPath }: Template) => { const templatePath = path.join(getTemplatePath(), name) const templateSource = fs.readFileSync(templatePath, 'utf8') const template = handlebars.compile(templateSource) const result = template(data) const outputDir = path.dirname(outputPath) if (!fs.existsSync(outputDir)) { fs.mkdirSync(outputDir, { recursive: true }) } fs.writeFileSync(outputPath, result) } export const runInitialStructureCompilation = (targetDir: string) => { const templates: Template[] = [ { name: 'Example.tsx.hbs', outputPath: path.join(targetDir, 'src', 'components', 'Example.tsx'), data: {}, }, { name: 'index.ts.hbs', outputPath: path.join(targetDir, 'src', 'index.ts'), data: { packageName, }, }, { name: '.gitignore.hbs', outputPath: path.join(targetDir, '.gitignore'), data: {}, }, { name: 'package.json.hbs', outputPath: path.join(targetDir, 'package.json'), data: {}, }, ] for (const template of templates) { compileTemplate(template) } }