allow to select template from cli

This commit is contained in:
weboko 2022-12-21 22:21:39 +01:00
parent 9ab90ef7d2
commit 545bd66d37
No known key found for this signature in database
2 changed files with 25 additions and 7 deletions

View File

@ -1,16 +1,18 @@
const path = require("path");
const fs = require("fs-extra");
const enquirer = require("enquirer");
const execSync = require("child_process").execSync;
const { Command } = require("commander");
const validateProjectName = require("validate-npm-package-name");
const DEFAULT_TEMPLATE = "light-chat";
const supportedExamplesDir = path.resolve(__dirname, "./examples");
const init = (name, description, version, supportedExamples) => {
const init = async (name, description, version, supportedExamples) => {
let appName;
const program = new Command()
let template;
const options = new Command()
.name(name)
.description(description)
.version(version, "-v, --version", "output the version number")
@ -20,13 +22,28 @@ const init = (name, description, version, supportedExamples) => {
})
.option(
"-t, --template <path-to-template>",
"specify a template for the created project"
"specify a template for the created project or you can skip and select template manually"
)
.allowUnknownOption()
.parse();
.parse()
.opts();
const options = program.opts();
const template = options.template || DEFAULT_TEMPLATE;
template = options.template;
if (!template) {
const templatePrompt = new enquirer.Select({
name: "template",
message: "Select template",
choices: Object.keys(supportedExamples),
});
try {
template = await templatePrompt.run();
} catch (e) {
console.error(`Failed at selecting a template: ${e.message}`);
process.exit(1);
}
}
if (!supportedExamples[template]) {
const supportedExamplesMessage = Object.keys(supportedExamples).reduce((acc, v) => {

View File

@ -49,6 +49,7 @@
},
"dependencies": {
"commander": "^9.4.1",
"enquirer": "^2.3.6",
"fs-extra": "^11.1.0",
"semver": "^7.3.8",
"validate-npm-package-name": "^5.0.0"