templates-mo
Version:
Templates is a scaffolding framework that makes code generation simple, dynamic, and reusable. Generate files, parts of your app, or whole project structures—without the repetitive copy-pasting
52 lines (37 loc) • 1.27 kB
text/typescript
import path from 'path';
import Template from '@tps/templates';
import * as TPS from '@tps/utilities/constants';
import { isDirAsync } from '@tps/utilities/fileSystem';
import { CommandModule } from 'yargs';
import { getCliArgsFromTemplate } from '@tps/cli/utils/helpers';
interface NewTemplateArgv {
template: string;
annotate: boolean;
type: 'js' | 'json';
experimental: boolean;
}
export default {
command: 'template <template>',
description: 'create a new template',
builder: (yargs) => {
const options = getCliArgsFromTemplate('new-template');
yargs.options(options);
return yargs;
},
async handler(argv) {
const tps = new Template('new-template', {
tpsPath: TPS.DEFAULT_TPS,
});
const answers: Record<string, unknown> = {};
if (argv.annotate) answers.annotate = argv.annotate;
if (argv.experimental) answers.experimental = argv.experimental;
if (argv.type) answers.type = argv.type;
tps.setAnswers(answers);
const dest = path.join(TPS.CWD, TPS.TPS_FOLDER);
if (await isDirAsync(path.join(dest, argv.template))) {
throw new Error('TPS template is already created.');
}
await tps.render(dest, argv.template);
console.log(`Template created: ${argv.template}`);
},
} as CommandModule<object, NewTemplateArgv>;