Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support select template from templates dir #1370

Merged
merged 8 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/create-mako/mako-project/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Creating angular project...');
kiner-tang marked this conversation as resolved.
Show resolved Hide resolved
52 changes: 40 additions & 12 deletions packages/create-mako/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import fs from 'fs';
import path from 'path';
import { globSync } from 'glob';
import type { QuestionCollection } from 'inquirer';
import yargs from 'yargs-parser';

const args = yargs(process.argv.slice(2));
const baseTemplatesPath = path.join(__dirname, '../templates');

async function init(projectName: string) {
let templatePath = path.join(__dirname, '../templates/react');
type InitOptions = {
projectName: string;
template: string;
};

async function init({ projectName, template }: InitOptions) {
kiner-tang marked this conversation as resolved.
Show resolved Hide resolved
let templatePath = path.join(baseTemplatesPath, template);
let files = globSync('**/*', { cwd: templatePath, nodir: true });
let cwd = path.join(process.cwd(), projectName);

Expand Down Expand Up @@ -41,21 +48,42 @@ async function init(projectName: string) {
console.log('Happy coding!');
}

type InitQuestion = {
name: string;
template: string;
};
async function main() {
let name = args._[0];
let { template } = args;
let questions: QuestionCollection[] = [];
if (!name) {
questions.push({
type: 'input',
name: 'name',
message: 'Project name:',
default: 'mako-project',
});
}
if (!template) {
const templates = globSync('**/', {
cwd: baseTemplatesPath,
maxDepth: 1,
}).filter((dir) => dir !== '.');
questions.push({
type: 'list',
name: 'template',
message: 'Select a template:',
choices: templates,
default: 'react',
});
}
if (questions.length > 0) {
const inquirer = (await import('inquirer')).default;
let answers = await inquirer.prompt([
{
type: 'input',
name: 'name',
message: 'Project name:',
default: 'mako-project',
},
]);
name = answers.name;
let answers = await inquirer.prompt<InitQuestion>(questions);
name = name || answers.name;
template = template || answers.template;
}
return init(String(name));
return init({ projectName: String(name), template });
}

main().catch((err) => {
Expand Down
Loading