-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·130 lines (119 loc) · 4.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#! /usr/bin/env node
let program = require('commander');
let path = require('path');
let fs = require('fs');
let inquirer = require('inquirer');
let chalk = require('chalk');
let config = require('./package.json');
let cmdInit = require('./bin/init');
let cmdPage = require('./bin/page');
let cmdRun = require('./bin/run');
let cmdBuild = require('./bin/build');
program.allowUnknownOption();
program.version(config.version);
//init project command
program.command('init')
.description('在当前目录初始化创建工程')
.action(function () {
let projectPath = process.cwd();
let projectName = path.basename(projectPath);
fs.readdir(projectPath, function (error, files) {
if (error) throw error;
if (files.length) {
inquirer.prompt([{
type: 'confirm',
name: 'ok',
message: `目录 <${projectName}> 不为空,继续将清空目录,是否继续?`,
default: true
}]).then(function (answers) {
if (answers.ok) {
cmdInit(projectPath, __dirname, projectName);
} else {
console.log(chalk.red(`停止创建工程 <${projectName}>`))
}
})
} else {
cmdInit(projectPath, __dirname, projectName);
}
});
})
.on('--help', function () {
console.log('');
console.log('示例 :');
console.log(chalk.grey('sbr init'));
});
//create project command
program.command('create <projectName>')
.description('在当前目录创建子目录 <projectName> 并初始化工程')
.action(function (projectName) {
let projectPath = path.join(process.cwd(), projectName);
fs.stat(projectPath, function (error, stats) {
if (error) {
cmdInit(projectPath, __dirname, projectName)
} else {
inquirer.prompt([{
type: 'confirm',
name: 'ok',
message: `目录 <${projectName}> 不为空,继续将清空目录,是否继续?`,
default: true
}]).then(function (answers) {
if (answers.ok) {
cmdInit(projectPath, __dirname, projectName);
} else {
console.log(chalk.red(`停止创建工程 <${projectName}>`))
}
})
}
});
})
.on('--help', function () {
console.log('');
console.log('示例 :');
console.log(chalk.grey('sbr create hello-world'));
});
//add page command
program.command('add-page <pageName>')
.description('添加页面<pageName>到工程目录下的 src/app/<pageName>')
.alias('page')
.action(function (pageName) {
cmdPage(process.cwd(), __dirname, pageName)
})
.on('--help', function () {
console.log('');
console.log('示例 :');
console.log(chalk.grey('sbr page user/list'));
});
// run page
program.command('run [pageName]')
.description('运行服务器,并启动打开页面 [pageName]')
.action(function (pageName) {
cmdRun(process.cwd(), __dirname, pageName)
})
.on('--help', function () {
console.log('');
console.log('示例 :');
console.log(chalk.grey('sbr run user/list'));
console.log(chalk.grey('sbr run'))
});
program.command('build')
.description('build 工程')
.action(function () {
cmdBuild(process.cwd(), __dirname)
})
.on('--help', function () {
console.log('');
console.log('示例 :');
console.log(chalk.grey('sbr build'));
});
//show help message when run sbr -h
program.on('--help', function () {
console.log('');
console.log(' Version:');
console.log(chalk.green(` ${config.version}`));
console.log('');
console.log(' Helps:');
console.log(chalk.green(' 想知道命令的具体详情请运行'), chalk.grey('sbr [command] --help'));
console.log(chalk.green(' 例如:'), chalk.grey('sbr init --help'));
console.log('')
});
program.parse(process.argv);