1 | #!/usr/bin/env node
|
2 | 'use strict';
|
3 | const chalk = require('chalk');
|
4 | const semver = require('semver');
|
5 | const program = require('commander');
|
6 | const platforms = require('../consts/platforms');
|
7 | const { BUILD_OPTIONS } = require('../consts/index');
|
8 | const path = require('path');
|
9 |
|
10 |
|
11 | function checkNodeVersion(version){
|
12 | if (semver.lt(process.version, version)) {
|
13 |
|
14 | console.log(
|
15 | chalk`nanachi only support {green.bold v8.6.0} or later (current {green.bold ${
|
16 | process.version
|
17 | }}) of Node.js`
|
18 | );
|
19 | process.exit(1);
|
20 | }
|
21 | }
|
22 | checkNodeVersion('8.6.0');
|
23 |
|
24 |
|
25 | function injectChaikEnv(){
|
26 | let pkg = {};
|
27 | try {
|
28 | pkg = require(path.join(process.cwd(), 'package.json'));
|
29 | } catch (err) {
|
30 |
|
31 | }
|
32 | let chaikaMode = pkg.nanachi && pkg.nanachi.chaika_mode
|
33 | ? 'CHAIK_MODE'
|
34 | : 'NOT_CHAIK_MODE';
|
35 | process.env.NANACHI_CHAIK_MODE = chaikaMode;
|
36 | }
|
37 |
|
38 | injectChaikEnv();
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | function registeCommand(command, desc, options = {}, action) {
|
48 | const cmd = program.command(command).description(desc);
|
49 | Object.keys(options).forEach(key => {
|
50 | const option = options[key];
|
51 |
|
52 | cmd.option(`${option.alias ? '-' + option.alias + ' ,' : ''}--${key}`, option.desc);
|
53 | });
|
54 | cmd.action(action);
|
55 | }
|
56 |
|
57 |
|
58 | function getArgValue(cmd){
|
59 | let args = {};
|
60 | cmd.options.forEach(function(op){
|
61 | let key = op.long.replace(/^--/, '');
|
62 |
|
63 |
|
64 | key = key.split('-').map((el, index)=>{
|
65 | return index > 0 ? `${el[0].toUpperCase()}${el.slice(1)}` : el;
|
66 | }).join('');
|
67 |
|
68 | if (typeof cmd[key] !== 'undefined') {
|
69 | args[key] = cmd[key];
|
70 | }
|
71 | });
|
72 | return args;
|
73 | }
|
74 |
|
75 | program
|
76 | .version(require('../package.json').version)
|
77 | .usage('<command> [options]');
|
78 |
|
79 |
|
80 | registeCommand('init <app-name>', 'description: 初始化项目', {}, (appName)=>{
|
81 | require('../commands/init')(appName);
|
82 | });
|
83 |
|
84 | program
|
85 | .command('install [name]')
|
86 | .description('description: 安装拆库模块. 文档: https://rubylouvre.github.io/nanachi/documents/chaika.html')
|
87 | .option('-b, --branch [branchName]', '指定分支')
|
88 | .action(function(name, opts){
|
89 | if (process.env.NANACHI_CHAIK_MODE != 'CHAIK_MODE') {
|
90 |
|
91 | console.log(chalk.bold.red('需在package.json中配置{"nanachi": {"chaika_mode": true }}, 拆库开发功能请查阅文档: https://rubylouvre.github.io/nanachi/documents/chaika.html'));
|
92 | process.exit(1);
|
93 | }
|
94 | let downloadInfo = {};
|
95 | if (!name && !opts.branch) {
|
96 |
|
97 | downloadInfo = {
|
98 | type: 'all',
|
99 | lib: ''
|
100 | };
|
101 | }
|
102 | if (name && !/\.git$/.test(name) ) {
|
103 |
|
104 | downloadInfo = {
|
105 | type: 'binary',
|
106 | lib: name
|
107 | };
|
108 | }
|
109 | if (/\.git$/.test(name) && opts.branch && typeof opts.branch === 'string' ) {
|
110 | downloadInfo = {
|
111 | type: 'git',
|
112 | lib: name,
|
113 | version: opts.branch
|
114 | };
|
115 | }
|
116 | require('../commands/install')(downloadInfo);
|
117 | });
|
118 |
|
119 | ['page', 'component'].forEach(type => {
|
120 | registeCommand(
|
121 | `${type} <page-name>`,
|
122 | `description: 创建${type}s/<${type}-name>/index.js模版`,
|
123 | {},
|
124 | (name)=>{
|
125 | const isPage = type === 'page';
|
126 | require('../commands/createPage')( {name, isPage} );
|
127 | });
|
128 | });
|
129 |
|
130 |
|
131 | function buildAction(buildType, compileType) {
|
132 | return function(cmd) {
|
133 | const args = getArgValue(cmd);
|
134 | args['buildType'] = buildType;
|
135 | args['watch'] = compileType === 'watch';
|
136 | require('../commands/build')(args);
|
137 | };
|
138 | }
|
139 |
|
140 | function registeBuildfCommand({compileType, buildType, isDefault, desc}) {
|
141 | registeCommand(`${compileType}${isDefault ? '' : ':' + buildType}`, desc, BUILD_OPTIONS, buildAction(buildType, compileType));
|
142 | }
|
143 |
|
144 | platforms.forEach(function(el){
|
145 | const { buildType, des, isDefault } = el;
|
146 | ['build', 'watch'].forEach(function (compileType) {
|
147 | if (isDefault) {
|
148 | registeBuildfCommand({
|
149 | compileType,
|
150 | buildType,
|
151 | isDefault,
|
152 | des
|
153 | });
|
154 | }
|
155 | registeBuildfCommand({
|
156 | compileType,
|
157 | buildType,
|
158 | isDefault: false,
|
159 | des
|
160 | });
|
161 | });
|
162 | });
|
163 |
|
164 | program
|
165 | .arguments('<command>')
|
166 | .action(()=>{
|
167 |
|
168 | console.log(chalk.yellow('无效 nanachi 命令'));
|
169 | program.outputHelp();
|
170 | });
|
171 |
|
172 | program.parse(process.argv);
|
173 |
|
174 |
|
175 | if (!process.argv.slice(2).length) {
|
176 | program.outputHelp();
|
177 | } |
\ | No newline at end of file |