提交 f25e311d 作者: 潘亚楠

create 命令#6

上级 5b57a372
src
.gitlab
tsconfig.json
.gitignore
\ No newline at end of file
{ {
"name": "qj-vue-cli", "name": "qj-cli",
"version": "0.1.0", "version": "0.1.0",
"description": "qj-vue-cli", "description": "qj-vue-cli",
"main": "index.js", "main": "index.js",
...@@ -11,8 +11,8 @@ ...@@ -11,8 +11,8 @@
"watch-bin": "nodemon dist/bin/index.js create vue2.0 hello", "watch-bin": "nodemon dist/bin/index.js create vue2.0 hello",
"dev": "concurrently \"npm run watch-ts\" \"npm run watch-bin\"", "dev": "concurrently \"npm run watch-ts\" \"npm run watch-bin\"",
"build": "tsc", "build": "tsc",
"dev-link": "npm run build && npm unlink && npm link", "test-ts": "mocha -r ts-node/register src/test/**/*.test.ts",
"test": "mocha -r ts-node/register src/test/**/*.test.ts" "test": "mocha dist/test/**/*.test.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
"@types/commander": "^2.12.2", "@types/commander": "^2.12.2",
"@types/cross-spawn": "^6.0.1", "@types/cross-spawn": "^6.0.1",
"@types/fs-extra": "^8.0.1", "@types/fs-extra": "^8.0.1",
"@types/inquirer": "^6.5.0",
"@types/mocha": "^5.2.7", "@types/mocha": "^5.2.7",
"@types/node": "^12.12.17", "@types/node": "^12.12.17",
"@types/power-assert": "^1.5.2", "@types/power-assert": "^1.5.2",
...@@ -45,6 +46,7 @@ ...@@ -45,6 +46,7 @@
"cross-spawn": "^7.0.1", "cross-spawn": "^7.0.1",
"execa": "^3.4.0", "execa": "^3.4.0",
"fs-extra": "^8.1.0", "fs-extra": "^8.1.0",
"inquirer": "^7.0.1",
"ora": "^4.0.3" "ora": "^4.0.3"
} }
} }
...@@ -6,7 +6,7 @@ import { create } from '../lib/create' ...@@ -6,7 +6,7 @@ import { create } from '../lib/create'
// 版本号 // 版本号
program program
.version(`${require('../../package.json').version}`) .version(`${require('../../package.json').version}`)
.name('world') .name('qj-cli')
.usage('<command> [options]'); .usage('<command> [options]');
// create command // create command
program program
......
import * as ora from 'ora' import * as ora from 'ora'
import { Git } from '../util/Git' import { Git } from '../util/Git'
import { Templates, Template } from '../config/template.config' import { Templates, Template } from '../config/template.config'
import * as inquirer from 'inquirer'
import * as chalk from 'chalk';
/** /**
* @date 2019.12.18 * @date 2019.12.18
* @author panyanan * @author panyanan
...@@ -18,16 +20,40 @@ export class CreateVue2 { ...@@ -18,16 +20,40 @@ export class CreateVue2 {
this.projectName = projectName; this.projectName = projectName;
this.targetDir = targetDir; this.targetDir = targetDir;
} }
create(options: any): void{ /**
* @date 2019.12.19
* @author panyanan
* @information 创建
* @param { Object } commander中设置的option
*/
public async create(options: any): Promise<void>{
try { try {
const spinner = ora('create new a project'); let spinner = ora('create new a project');
spinner.start(); spinner.start(`拉取${this.template.name}模板`);
const git = new Git(this.template.repo, this.targetDir); // 通过仓库地址和输出目录创建git实例
git.pull(); const pullGit = new Git(this.template.repo, this.targetDir);
spinner.stop(); pullGit.pull();
spinner.succeed('clone succeed'); spinner.succeed('拉取模板成功');
// 是否推送到gitLab
let answer = await inquirer.prompt([
{ name: 'push', type: 'confirm', message: '是否将项目推送到gitlab',}
]);
// 如果推送
if (answer.push) {
const { repo } = await inquirer.prompt([
{ name: 'repo', type: 'input', message: '请输入gitlab项目地址:'}
]);
const pushGit = new Git(repo, this.targetDir);
try {
spinner.start(`正在推送到gitlab: ${repo}`)
pushGit.push();
spinner.succeed('推送成功')
} catch (error) {
spinner.fail(`推送失败: ${error.message}`);
}
}
} catch (error) { } catch (error) {
console.error(error); console.log(`${chalk.red('创建失败')}: ${error.message}`);
} }
} }
} }
\ No newline at end of file
...@@ -42,11 +42,11 @@ export class Git { ...@@ -42,11 +42,11 @@ export class Git {
cwd: this.dir, cwd: this.dir,
} }
// remove origin // remove origin
spawn.sync('git', ['remote', 'remove', 'origin',], options); spawn.sync('git', ['remote', 'remove', 'origin'], options);
// set origin 为当前url // set origin 为当前url
spawn.sync('git', ['remote', 'add', 'origin', this.gitURL], options); spawn.sync('git', ['remote', 'add', 'origin', this.gitURL], options);
// 推送 // 推送
const { status, stderr } = spawn.sync('git', ['push', '-u', 'origin', '--all'], options); const { status, stderr, stdout } = spawn.sync('git', ['push', '-u', 'origin', '--all'], options);
// status 为0时命令执行成功, 为1是命令执行失败 // status 为0时命令执行成功, 为1是命令执行失败
if (status === 0) { if (status === 0) {
res = true; res = true;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论