From e02a63020517460609b37620b89f6347af8aedbf Mon Sep 17 00:00:00 2001 From: Mark Tang Date: Wed, 18 May 2022 09:29:00 +0800 Subject: [PATCH] initialize component --- commands/init/lib/index.js | 108 ++++++++++++++++++++++--------------- 1 file changed, 66 insertions(+), 42 deletions(-) diff --git a/commands/init/lib/index.js b/commands/init/lib/index.js index 1dbec68..1589656 100644 --- a/commands/init/lib/index.js +++ b/commands/init/lib/index.js @@ -129,7 +129,6 @@ class InitCommand extends Command { spinner.stop(true); log.success('Template installed successfully.'); } - console.log(this.templateInfo); const templateIgnore = this.templateInfo.ignore || []; const ignore = ['node_modules/**', ...templateIgnore]; await this.ejsRender({ ignore }); @@ -253,72 +252,97 @@ class InitCommand extends Command { }); log.verbose('type', type); this.templates = this.templates.filter(template => template.tag.includes(type)); + const title = type === TYPE_PROJECT ? 'project' : 'component'; - if (type === TYPE_PROJECT) { - const projectNamePrompt = { - type: 'input', - name: 'projectName', - message: 'Please enter the project name', - validate: function(v) { - const done = this.async(); - setTimeout(function () { - if (!isValidName(v)) { - done('Please enter the project name in the correct format'); - return; - } - done(null, true); - }, 0); - }, - filter: function(v) { + const projectNamePrompt = { + type: 'input', + name: 'projectName', + message: `Please enter the ${title} name`, + validate: function(v) { + const done = this.async(); + setTimeout(function () { + if (!isValidName(v)) { + done(`Please enter the ${title} name in the correct format`); + return; + } + done(null, true); + }, 0); + }, + filter: function(v) { + return v; + } + }; + const projectPrompt = []; + if (!isProjectNameValid) { + projectPrompt.push(projectNamePrompt); + } + projectPrompt.push({ + type: 'input', + name: 'projectVersion', + message: `Please enter the ${title} version number`, + default: '1.0.0', + validate: function(v) { + const done = this.async(); + setTimeout(function () { + if (!(!!semver.valid(v))) { + done(`Please enter the ${title} version number in the correct format`); + return; + } + done(null, true); + }, 0); + }, + filter: function(v) { + if (!!semver.valid(v)) { + return semver.valid(v); + } else { return v; } - }; - const projectPrompt = []; - if (!isProjectNameValid) { - projectPrompt.push(projectNamePrompt); } - projectPrompt.push({ + }, { + type: 'list', + name: 'projectTemplate', + message: `Please select a ${title} template`, + choices: this.createTemplateChoice() + }); + if (type === TYPE_PROJECT) { + const project = await inquirer.prompt(projectPrompt); + projectInfo = { + ...projectInfo, + type, + ...project + } + } else if (type === TYPE_COMPONENT) { + const descriptionPrompt = { type: 'input', - name: 'projectVersion', - message: 'Please enter the project version number', - default: '1.0.0', + name: 'componentDescription', + message: 'Please enter the component description', validate: function(v) { const done = this.async(); setTimeout(function () { - if (!(!!semver.valid(v))) { - done('Please enter the project version number in the correct format'); + if (!v) { + done('Please enter the component description'); return; } done(null, true); }, 0); - }, - filter: function(v) { - if (!!semver.valid(v)) { - return semver.valid(v); - } else { - return v; - } } - }, { - type: 'list', - name: 'projectTemplate', - message: 'Please select a project template', - choices: this.createTemplateChoice() - }); + }; + projectPrompt.push(descriptionPrompt); const project = await inquirer.prompt(projectPrompt); projectInfo = { ...projectInfo, type, ...project } - } else if (type === TYPE_COMPONENT) { - } // formattedName if (projectInfo.projectName) { projectInfo.formattedName = require('kebab-case')(projectInfo.projectName).replace(/^-/, ''); } + if (projectPrompt.componentDescription) { + projectInfo.description = projectInfo.componentDescription; + } return projectInfo; }