Skip to content

Commit

Permalink
Merge pull request #104 from WeBankFinTech/mn3-create
Browse files Browse the repository at this point in the history
feat(cli): 创建项目,支持直接传入项目名
  • Loading branch information
szyuan authored Jan 28, 2021
2 parents ba44622 + 9a8e9f1 commit c65eede
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
21 changes: 11 additions & 10 deletions packages/wxa-cli/src/creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import fs from 'fs';
import path from 'path';
import shell from 'shelljs';
import https from 'https';
import {isUri} from 'valid-url';
import inquirer from 'inquirer';

let remoteMap = new Map([
Expand All @@ -29,8 +28,11 @@ function getQATemplate(templateConfigs) {
}
];
}

function getQAProjectName() {
function getQAProjectName(projectName) {
// 如果已经带name参数,不需要再次询问项目名
if (projectName) {
return [];
}
return [
{
type: 'input',
Expand All @@ -42,29 +44,29 @@ function getQAProjectName() {
}
];
}

function getQA(templateConfigs) {
function getQA(templateConfigs, projectName) {
return [
...getQAProjectName(),
...getQAProjectName(projectName),
...getQATemplate(templateConfigs)
]
}
class Creator {
constructor(cmd) {
this.cmdOptions = cmd;
this.prefix = isUri(cmd.repo) ? cmd.repo : remoteMap.get(cmd.repo);
this.prefix = remoteMap.get(cmd.repo) || cmd.repo;
}

async run() {
let options;
try {
// 先尝试从github、gitee远程拉取模版配置json,并询问项目名+模板类型
let configs = await this.getRemoteConfigs();
options = await inquirer.prompt(getQA(JSON.parse(configs)));
options = await inquirer.prompt(getQA(JSON.parse(configs), this.cmdOptions.projectName));
} catch (err) {
// getRemoteConfigs失败后,先询问项目名,clone git模版仓库,拿到模版再配置询问模版类型
options = await inquirer.prompt(getQAProjectName());
options = await inquirer.prompt(getQAProjectName(this.cmdOptions.projectName));
}
options.projectName = options.projectName || this.cmdOptions.projectName;
this.create(options);
}

Expand Down Expand Up @@ -130,7 +132,6 @@ class Creator {
});
}


getLocalConfigs(projectName) {
const data = fs.readFileSync(path.resolve('./', projectName, 'configs.json'));
return data;
Expand Down
1 change: 1 addition & 0 deletions packages/wxa-cli/src/wxa.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ commander
.command('create')
.description('新建模板')
.option('--repo <repo>', '仓库地址,可选github或gitee,允许传自定义的repo地址,网速考虑,默认gitee', 'gitee')
.option('--project-name <projectName>', '项目名')
.action(async (cmd)=>{
showSlogan();
console.info('🦊 Creating 新建项目中');
Expand Down

0 comments on commit c65eede

Please sign in to comment.