create-seia-app
Version:
Create Seia.js apps with one command
77 lines (76 loc) • 2.81 kB
JavaScript
import { cp, rm, writeFile } from 'node:fs/promises';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { Args, Command } from '@oclif/core';
import { Liquid } from 'liquidjs';
import prompts from 'prompts';
import walkdir from 'walkdir';
export default class Index extends Command {
static args = {
name: Args.string({
description: 'Name for the new project'
})
};
static description = 'Create a new Seia project';
static examples = [
'<%= config.bin %>',
'<%= config.bin %> <Name>'
];
static flags = {};
static templateExtension = '.liquid';
static normalizeProjectName(name) {
return name.trim();
}
static isValidPackageName(name) {
return /^(?:@[a-z\d\-*~][a-z\d\-*._~]*\/)?[a-z\d\-~][a-z\d\-._~]*$/.test(name);
}
async render(path, context) {
const liquid = new Liquid();
// TODO: use Promise.all to parallelize
for (const template of (await walkdir.async(path, {
filter: (_, files)=>files.filter((file)=>file.endsWith(Index.templateExtension))
}))){
await writeFile(template.slice(0, -Index.templateExtension.length), await liquid.renderFile(template, context));
await rm(template);
}
}
async run() {
const { args, flags } = await this.parse(Index);
const result = await prompts([
{
type: args.name ? null : 'text',
name: 'name',
message: 'Project name:',
initial: 'seia-app'
},
{
type: (previous)=>Index.isValidPackageName(previous) ? null : 'text',
name: 'package',
message: 'Package name:',
initial: 'seia-app'
},
{
type: 'toggle',
name: 'ts',
message: 'Use TypeScript?',
active: 'yes',
inactive: 'no',
initial: true
}
]);
const { name, package: _package, ts } = result;
const template = ts ? 'typescript' : 'javascript';
const currentFilename = fileURLToPath(import.meta.url);
const currentDirname = dirname(currentFilename);
await cp(`${currentDirname}/../templates/${template}`, name, {
recursive: true
});
const context = {
name,
package: _package
};
await this.render(name, context);
const indent = ' ';
console.log(`\n${indent}Created project ${name} with ${template} template!\n\n${indent}$ cd ${name}\n${indent}$ npm install\n${indent}$ npm run build\n${indent}$ npm run start\n\n${indent}Happy hacking! 🚀`);
}
}