import { Command } from 'commander' import Path from 'path' import fs from 'fs' import { compile } from '../compiler' import Glob from 'glob' const ensureDirectoryExistence = (filePath: string): void => { const dirname = Path.dirname(filePath) if (fs.existsSync(dirname)) { return } ensureDirectoryExistence(dirname) fs.mkdirSync(dirname) } export default (program: Command): void => { program.command('compile') .description('Compile Templates files') .argument('') .action((paths: string[]) => { paths .flatMap(path => Glob.sync(path)) .filter((path, index, self) => index === self.findIndex(item => item === path)) .filter(path => !path.endsWith('rjt.jsx') && !path.endsWith('rjt.tsx')) .forEach(path => { const code = compile(path) const filePath = path .replace(/\.jsx$/, '.rjt.jsx') .replace(/\.tsx$/, '.rjt.tsx') ensureDirectoryExistence(filePath) fs.writeFileSync(filePath, code) }) }) }