提交 5b57a372 作者: 潘亚楠

create 命令

上级 e053fd09
......@@ -2,6 +2,7 @@
import * as program from 'commander';
import { hasTemplate } from '../config/template.config';
import listTemplates from '../lib/listTemplates';
import { create } from '../lib/create'
// 版本号
program
.version(`${require('../../package.json').version}`)
......@@ -11,9 +12,8 @@ program
program
.command('create <templateName> <projectName>')
.description('create a project from a template')
.action((templateName: string, projectName: string, cmd) => {
console.log(templateName);
console.log(projectName);
.action((templateName: string, projectName: string, options) => {
create(templateName, projectName, options);
})
// ls command
program
......
......@@ -3,18 +3,13 @@
* @author panyanan
* @information 框架模板配置文件
*/
export interface Repo {
readonly url: string, // 仓库地址
readonly username: string, // 用户名
readonly password: string, // 用户密码
}
export interface Template {
readonly name: string, // 模板名称
readonly desc: string, // 模板描述
readonly repo: Repo, // 模板仓库信息
readonly repo: string, // 模板仓库信息
}
export const Templates: Template[] = [
{ name: 'vue2.0', desc: '千家vue2.0项目模板,对接了组件库/标准库/用户操作埋点', repo: { url: 'git@git.allhome.com.cn:panyanan/my-vue-template.git', username: '', password: ''} },
{ name: 'vue2', desc: '千家vue2项目模板,对接了组件库/标准库/用户操作埋点', repo: 'git@git.allhome.com.cn:panyanan/my-vue-template.git' },
// todo other template
];
/**
......
import { Templates, hasTemplate } from '../config/template.config'
import * as chalk from 'chalk'
import * as path from 'path'
import { CreateVue2 } from './create_vue2'
export async function create(templateName: string, projectName: string, options: any) {
// 验证模板名称
let requireMessage = [];
if (!hasTemplate(templateName)) {
requireMessage.push('模板名称不正确')
}
// 验证项目名称
if (!projectName) {
requireMessage.push('缺少项目名')
}
if (requireMessage.length) {
console.log(chalk.red(requireMessage.join(' && ')));
return;
}
// 项目目录
const targetDir = path.join(process.cwd(), projectName);
// 模板
const template = Templates.find( ({ name }) => name === templateName );
// 不同的框架模板实现各自的创建逻辑
switch(templateName) {
case 'vue2':
const creator = new CreateVue2( template, projectName, targetDir);
await creator.create(options);
default:
}
}
import * as ora from 'ora'
import { Git } from '../util/Git'
import { Templates, Template } from '../config/template.config'
/**
* @date 2019.12.18
* @author panyanan
* @information 创建vue2项目
*/
export class CreateVue2 {
// 模板
private template: Template;
// 项目名称
private projectName: string;
// 项目目录
private targetDir: string;
constructor(template: Template, projectName: string, targetDir: string) {
this.template = template;
this.projectName = projectName;
this.targetDir = targetDir;
}
create(options: any): void{
try {
const spinner = ora('create new a project');
spinner.start();
const git = new Git(this.template.repo, this.targetDir);
git.pull();
spinner.stop();
spinner.succeed('clone succeed');
} catch (error) {
console.error(error);
}
}
}
\ No newline at end of file
......@@ -15,9 +15,9 @@ export default function () {
const tip = `
**********************************************
当你使用create命令时如:
${chalk.blueBright('"qj-cli create vue2.0 hello"')}
中的${chalk.blueBright('"vue2.0"')}就指的是下面框架名: vue2.0,
欢迎同学们积极提供框架模板,我们还需要vue2.0_ts, vue3.0, vue3.0_ts,node服务框架等等,
${chalk.blueBright('"qj-cli create vue2 hello"')}
中的${chalk.blueBright('"vue2"')}就指的是下面框架名: vue2,
欢迎同学们积极提供框架模板,我们还需要vue2_ts, vue3, vue3_ts,node服务框架等等,
**********************************************
`;
log(`${chalk.green(tip)}`);
......
import Git from '../../util/Git'
import { Git } from '../../util/Git'
import * as assert from 'power-assert'
import * as path from 'path'
import * as spawn from 'cross-spawn'
......
......@@ -8,7 +8,7 @@ import * as assert from 'power-assert'
import * as fs from 'fs-extra'
import * as chalk from 'chalk'
import * as spawn from 'cross-spawn'
class Git {
export class Git {
private gitURL: string; // 仓库地址
private dir: string; // 拉取或推送的目录
constructor(gitURL: string, dir: string) {
......@@ -21,22 +21,15 @@ class Git {
* @author panyanan
* @information 拉取
*/
async pull(): Promise<boolean> {
let res = false;
try {
// 检测目录是否存在
const dirExits = await fs.pathExists(this.dir);
// 目录存在需要删除,才能clone
if (dirExits) {
console.log(chalk.red(`delete ${this.dir}`));
await fs.remove(this.dir);
}
await execa('git', ['clone', this.gitURL, this.dir]);
res = true;
} catch (error) {
assert.ok(false, `拉取 ${this.gitURL} 失败: ${ error.message }`);
pull(): void {
// 检测目录是否存在
const dirExits = fs.pathExistsSync(this.dir);
// 目录存在需要删除,才能clone
if (dirExits) {
console.log(`${chalk.red('\n delete ')}${this.dir}`);
fs.removeSync(this.dir);
}
return res;
execa.sync('git', ['clone', this.gitURL, this.dir]);
}
/**
* @date 2019.12.17
......@@ -91,5 +84,4 @@ class Git {
} catch (error) {}
return res;
}
}
export default Git;
\ No newline at end of file
}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论