UNPKG

5.16 kBJavaScriptView Raw
1#!/usr/bin/env node
2'use strict';
3const chalk = require('chalk');
4const semver = require('semver');
5const program = require('commander');
6const platforms = require('../consts/platforms');
7const { BUILD_OPTIONS } = require('../consts/index');
8const path = require('path');
9
10
11function checkNodeVersion(version){
12 if (semver.lt(process.version, version)) {
13 // eslint-disable-next-line
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}
22checkNodeVersion('8.6.0');
23
24
25function injectChaikEnv(){
26 let pkg = {};
27 try {
28 pkg = require(path.join(process.cwd(), 'package.json'));
29 } catch (err) {
30 // eslint-disable-next-line
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
38injectChaikEnv();
39
40/**
41 * 注册命令
42 * @param {String} command 命令名
43 * @param {String} desc 命令描述
44 * @param {Object} options 命令参数
45 * @param {Function} action 回调函数
46 */
47function 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//获取参数的布尔值
58function getArgValue(cmd){
59 let args = {};
60 cmd.options.forEach(function(op){
61 let key = op.long.replace(/^--/, '');
62 //commander中会把 --beta-ui 风格的参数变为cmd上betaUi驼峰
63 //beta-ui => betaUi
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
75program
76 .version(require('../package.json').version)
77 .usage('<command> [options]');
78
79
80registeCommand('init <app-name>', 'description: 初始化项目', {}, (appName)=>{
81 require('../commands/init')(appName);
82});
83
84program
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 // eslint-disable-next-line
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 //nanachi install package.json中配置的所有包
97 downloadInfo = {
98 type: 'all',
99 lib: ''
100 };
101 }
102 if (name && !/\.git$/.test(name) ) {
103 //nanachi install xxx@kkk
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
131function 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
140function registeBuildfCommand({compileType, buildType, isDefault, desc}) {
141 registeCommand(`${compileType}${isDefault ? '' : ':' + buildType}`, desc, BUILD_OPTIONS, buildAction(buildType, compileType));
142}
143//注册其他命令
144platforms.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
164program
165 .arguments('<command>')
166 .action(()=>{
167 // eslint-disable-next-line
168 console.log(chalk.yellow('无效 nanachi 命令'));
169 program.outputHelp();
170 });
171
172program.parse(process.argv);
173
174
175if (!process.argv.slice(2).length) {
176 program.outputHelp();
177}
\No newline at end of file