UNPKG

3.14 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2016-present Alibaba Group Holding Limited
3 * @author Houfeng <admin@xhou.net>
4 */
5
6const console = require('console3');
7const pkg = require('../package.json');
8const path = require('path');
9const cmdline = require('cmdline');
10const core = require('../');
11const Context = core.Context;
12const middlewares = core.middlewares;
13const debug = require('debug')('cli-core');
14const sleep = require('../lib/common/sleep');
15
16core.upgrade.check();
17
18const ALIAS = {
19 i: 'init',
20 d: 'dev',
21 s: 'start',
22 b: 'build',
23 a: 'add',
24 t: 'test',
25 p: 'publish',
26 r: 'run',
27 u: 'update',
28 c: 'config'
29};
30
31cmdline.onFail = async function (err) {
32 if (process.env.DN_DEBUG) {
33 console.error(err);
34 } else {
35 console.error(err.message);
36 }
37 debug('error', err);
38 this.emit('fail', err);
39 if (!this.disabledExit) process.exit(1);
40}.bind(cmdline);
41
42cmdline.onDone = async function (context) {
43 debug('done', context.command);
44 this.emit('done', context);
45}.bind(cmdline);
46
47cmdline
48 .version(pkg.version)
49 .help(path.normalize(`@${__dirname}/help.txt`))
50 .error(cmdline.onFail)
51
52 .root.command(['init', 'i'])
53 .option(['-t', '--template'], 'string')
54 .option(['-f', '--force'], 'switch')
55 .action(async function (cmd, template) {
56 cmd = ALIAS[cmd] || cmd;
57 this.set('command', cmd);
58 try {
59 let downloadCtx = new Context(this, {
60 template, cmd,
61 pipeline: [middlewares.init]
62 });
63 await downloadCtx.run();
64 let context = new Context(this, { template, cmd });
65 await context.run();
66 cmdline.onDone(context);
67 } catch (err) {
68 cmdline.onFail(err);
69 }
70 }, false)
71
72 .root.command([
73 'dev', 'add', 'test', 'build', 'publish', 'start', 'run',
74 'd', 'a', 't', 'b', 'p', 's', 'r'
75 ])
76 .option(['-e', '--env'], 'string')
77 .action(async function (cmd, env, $1) {
78 if (cmd == 'r' || cmd == 'run') {
79 cmd = $1 || 'dev';
80 }
81 cmd = ALIAS[cmd] || cmd;
82 this.set('command', cmd);
83 process.env.DN_CMD = cmd || '';
84 process.env.DN_ENV = env || '';
85 process.env.NODE_ENV = env || process.env.NODE_ENV || '';
86 try {
87 let context = new Context(this, { cmd, env });
88 await context.run();
89 cmdline.onDone(context);
90 } catch (err) {
91 cmdline.onFail(err);
92 }
93 }, false)
94
95 .root.command(['template', 'middleware', 'update', 'u', 'config', 'c'])
96 .action(async function (cmd, env) {
97 cmd = ALIAS[cmd] || cmd;
98 this.set('command', cmd);
99 try {
100 await core.commands[cmd].call(this, cmd);
101 cmdline.onDone(this);
102 } catch (err) {
103 cmdline.onFail(err);
104 }
105 }, false)
106
107 .root.command(['system', '$'])
108 .action(async function (cmd, $1, $2) {
109 if ($1 == 'clean') {
110 cmd = 'update';
111 } else if ($1) {
112 cmd = $1;
113 }
114 this.set('$1', $2);
115 if (!core.commands[cmd]) {
116 return cmdline.onFail(new Error(`Invalid command: ${cmd}`));
117 }
118 console.warn('The command is deprecated');
119 try {
120 await core.commands[cmd].call(this, cmd);
121 cmdline.onDone(this);
122 } catch (err) {
123 cmdline.onFail(err);
124 }
125 }, false);
126
127module.exports = cmdline;
\No newline at end of file