UNPKG

7.22 kBJavaScriptView Raw
1module.exports = (commander) => {
2 require('colors');
3
4 require('./mods/check-node-version')();
5 require('./mods/watch-error')();
6
7 const fs = require('fs');
8 const path = require('path');
9 const fse = require('fs-extra');
10 const core = require('../core/index');
11
12 const scaffoldUtil = core.scaffold.util;
13
14 const showHelp = () => {
15 console.log(['',
16 ` - init project > ${'bio init [scaffoldName]'.green}`,
17 ` - run scaffold task > ${'bio run <task>'.green}`,
18 ` - run local mock > ${'bio mock [port]'.green}`,
19 ` - show scaffold > ${'bio scaffold show <scaffoldName>'.green}`,
20 ` - create scaffold > ${'bio scaffold create'.green}`,
21 ` - init lint > ${'bio lint init [-t, --type [value]]'.green}`,
22 ` - run lint > ${'bio lint [-w, --watch]'.green}`,
23 ` - help > ${'bio help'.green}\n`,
24 ].join('\n'));
25
26 console.log(`doc: ${'https://github.com/weidian-inc/bio-cli'.green}\n`);
27 };
28
29 core.set({
30 scaffoldList: [{
31 shortName: 'pure',
32 fullName: 'bio-scaffold-pure',
33 desc: 'traditional project',
34 version: 'latest',
35 }, {
36 shortName: 'vue',
37 fullName: 'bio-scaffold-vue',
38 desc: 'vue project',
39 version: 'latest',
40 }, {
41 shortName: 'react',
42 fullName: 'bio-scaffold-react',
43 desc: 'react project',
44 version: 'latest',
45 }],
46
47 beforeScaffoldInstall(installationDir) {
48 const npmrcPath = path.join(installationDir, '.npmrc');
49
50 fse.ensureFileSync(npmrcPath);
51 fs.writeFileSync(npmrcPath, [
52 // 'sass_binary_site=https://npm.taobao.org/mirrors/node-sass/',
53 // 'phantomjs_cdnurl=https://npm.taobao.org/mirrors/phantomjs/',
54 // 'electron_mirror=https://npm.taobao.org/mirrors/electron/',
55 ].join('\n'));
56 }
57 });
58
59 /***************** init project start *************************/
60 commander
61 .command('init [scaffoldName]')
62 .description('init project.')
63 .parse(process.argv)
64 .action((scaffoldName) => {
65 core.init({ scaffoldName });
66 });
67 /***************** init project end *************************/
68
69 /***************** run project start *************************/
70 commander
71 .command('run <task>')
72 .description('run.')
73 .action((task) => {
74 core.scaffold.run(task);
75 });
76 /***************** run project end *************************/
77
78
79 /***************** scaffold start *************************/
80 commander
81 .command('scaffold-show <scaffoldName>')
82 .description('orders about scaffold.')
83 .action((scaffoldName) => {
84 core.scaffold.show(scaffoldName);
85 });
86 /***************** scaffold end *************************/
87
88 /***************** mock start *************************/
89 commander
90 .command('mock [port]')
91 .description('local mock.')
92 .action((port) => {
93 core.mock(port);
94 });
95 /***************** mock end *************************/
96
97 /***************** lint start *************************/
98 commander
99 .command('lint-run')
100 .description('lint run.')
101 .option('-w, --watch', 'watch')
102 .option('-f, --fix', 'format')
103 .action((options) => {
104 core.lint.run({
105 watch: options.watch,
106 fix: options.fix
107 });
108 });
109 commander
110 .command('lint-init')
111 .description('lint init.')
112 .action(() => {
113 core.lint.init();
114 });
115 /***************** lint end *************************/
116
117 /***************** docsite start *************************/
118 commander
119 .command('docsite-init')
120 .description('docsite')
121 .action(() => {
122 core.docsite.init();
123 });
124 commander
125 .command('docsite-serve [target]')
126 .option('-m, --multi', 'multi website')
127 .option('-i, --show-index', 'showIndex')
128 .option('-p, --port', 'port')
129 .description('docsite serve')
130 .action((target, options) => {
131 core.docsite.serve({ multi: options.multi, target, showIndex: options.showIndex, port: options.port });
132 });
133 commander
134 .command('docsite-push [branch]')
135 .option('-m, --multi', 'multi website')
136 .description('docsite')
137 .action((branch, options) => {
138 core.docsite.push({ multi: options.multi, branch });
139 });
140 commander
141 .command('docsite-clear')
142 .description('docsite clear project.')
143 .action(() => {
144 core.docsite.clear();
145 });
146 /***************** docsite end *************************/
147
148 commander
149 .command('test')
150 .description('add test configs.')
151 .action(() => {
152 core.test();
153 });
154
155 commander
156 .command('help')
157 .description('help.')
158 .action(showHelp);
159
160 commander
161 .command('update')
162 .action(() => {
163 require('child_process').execSync('npm install bio-cli@latest -g', {
164 stdio: 'inherit',
165 });
166 });
167
168 /***************** plugins start *************************/
169 commander
170 .command('plugin-init')
171 .description('plugin')
172 .action(() => {
173 core.plugin.init();
174 });
175 commander
176 .command('plugin-add <pluginName>')
177 .description('plugin')
178 .action((pluginName) => {
179 core.plugin.add({ pluginName, commanderEvents: commander._events });
180 });
181 commander
182 .command('plugin-remove <pluginName>')
183 .description('plugin')
184 .action((pluginName) => {
185 core.plugin.remove({ pluginName });
186 });
187 commander
188 .command('plugin-list')
189 .description('plugin')
190 .action(() => {
191 core.plugin.list();
192 });
193 commander
194 .command('plugin-link')
195 .description('plugin')
196 .action(() => {
197 core.plugin.link();
198 });
199 commander
200 .command('plugin-unlink')
201 .description('plugin')
202 .action(() => {
203 core.plugin.unlink();
204 });
205 /***************** plugins end *************************/
206
207 // error on unknown commands
208 commander.on('command:*', function () {
209 console.error(`Invalid command: ${'%s'.yellow}\nSee list of available commands.`, commander.args.join(' '));
210 showHelp();
211 process.exit(1);
212 });
213
214 commander.parse(process.argv);
215
216 if (process.argv.slice(2).length === 0) {
217 if (!scaffoldUtil.getScaffoldNameFromConfigFile()) {
218 core.init().then(() => {
219 core.scaffold.run('dev-daily', { watch: true });
220 });
221 } else {
222 core.scaffold.run('dev-daily', { watch: true });
223 }
224 }
225};