UNPKG

8.19 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const fs = require("fs-extra");
4const path = require("path");
5const minimist = require("minimist");
6const service_1 = require("@tarojs/service");
7const customCommand_1 = require("./commands/customCommand");
8const util_1 = require("./util");
9class CLI {
10 constructor(appPath) {
11 this.appPath = appPath || process.cwd();
12 }
13 run() {
14 this.parseArgs();
15 }
16 parseArgs() {
17 var _a;
18 var _b;
19 const args = minimist(process.argv.slice(2), {
20 alias: {
21 version: ['v'],
22 help: ['h'],
23 port: ['p'],
24 resetCache: ['reset-cache'],
25 publicPath: ['public-path'],
26 bundleOutput: ['bundle-output'],
27 sourcemapOutput: ['sourcemap-output'],
28 sourceMapUrl: ['sourcemap-use-absolute-path'],
29 sourcemapSourcesRoot: ['sourcemap-sources-root'],
30 assetsDest: ['assets-dest'] // specially for rn, Directory name where to store assets referenced in the bundle.
31 },
32 boolean: ['version', 'help']
33 });
34 const _ = args._;
35 const command = _[0];
36 if (command) {
37 const appPath = this.appPath;
38 const presetsPath = path.resolve(__dirname, 'presets');
39 const commandsPath = path.resolve(presetsPath, 'commands');
40 const platformsPath = path.resolve(presetsPath, 'platforms');
41 const commandPlugins = fs.readdirSync(commandsPath);
42 const targetPlugin = `${command}.js`;
43 // 设置环境变量
44 (_b = process.env).NODE_ENV || (_b.NODE_ENV = args.env);
45 if (process.env.NODE_ENV === 'undefined' && (command === 'build' || command === 'inspect')) {
46 process.env.NODE_ENV = (args.watch ? 'development' : 'production');
47 }
48 if (args.type) {
49 process.env.TARO_ENV = args.type;
50 }
51 if (typeof args.plugin === 'string') {
52 process.env.TARO_ENV = 'plugin';
53 }
54 const kernel = new service_1.Kernel({
55 appPath,
56 presets: [
57 path.resolve(__dirname, '.', 'presets', 'index.js')
58 ],
59 plugins: []
60 });
61 kernel.optsPlugins || (kernel.optsPlugins = []);
62 // 针对不同的内置命令注册对应的命令插件
63 if (commandPlugins.includes(targetPlugin)) {
64 kernel.optsPlugins.push(path.resolve(commandsPath, targetPlugin));
65 }
66 switch (command) {
67 case 'inspect':
68 case 'build': {
69 let plugin;
70 let platform = args.type;
71 const { publicPath, bundleOutput, sourcemapOutput, sourceMapUrl, sourcemapSourcesRoot, assetsDest } = args;
72 // 针对不同的内置平台注册对应的端平台插件
73 switch (platform) {
74 case 'weapp':
75 case 'alipay':
76 case 'swan':
77 case 'tt':
78 case 'qq':
79 case 'jd':
80 kernel.optsPlugins.push(`@tarojs/plugin-platform-${platform}`);
81 break;
82 default: {
83 // h5, rn
84 const platformPlugins = fs.readdirSync(platformsPath);
85 const targetPlugin = `${platform}.js`;
86 if (platformPlugins.includes(targetPlugin)) {
87 kernel.optsPlugins.push(path.resolve(platformsPath, targetPlugin));
88 }
89 break;
90 }
91 }
92 // 根据 framework 启用插件
93 const framework = (_a = kernel.config) === null || _a === void 0 ? void 0 : _a.initialConfig.framework;
94 switch (framework) {
95 case 'vue':
96 kernel.optsPlugins.push('@tarojs/plugin-framework-vue2');
97 break;
98 case 'vue3':
99 kernel.optsPlugins.push('@tarojs/plugin-framework-vue3');
100 break;
101 default:
102 kernel.optsPlugins.push('@tarojs/plugin-framework-react');
103 break;
104 }
105 // 编译小程序插件
106 if (typeof args.plugin === 'string') {
107 plugin = args.plugin;
108 platform = 'plugin';
109 kernel.optsPlugins.push(path.resolve(platformsPath, 'plugin.js'));
110 }
111 // 传递 inspect 参数即可
112 if (command === 'inspect') {
113 customCommand_1.default(command, kernel, args);
114 break;
115 }
116 customCommand_1.default(command, kernel, {
117 _,
118 platform,
119 plugin,
120 isWatch: Boolean(args.watch),
121 port: args.port,
122 env: args.env,
123 deviceType: args.platform,
124 resetCache: !!args.resetCache,
125 publicPath,
126 bundleOutput,
127 sourcemapOutput,
128 sourceMapUrl,
129 sourcemapSourcesRoot,
130 assetsDest,
131 qr: !!args.qr,
132 blended: Boolean(args.blended),
133 h: args.h
134 });
135 break;
136 }
137 case 'init': {
138 customCommand_1.default(command, kernel, {
139 _,
140 appPath,
141 projectName: _[1] || args.name,
142 description: args.description,
143 typescript: args.typescript,
144 templateSource: args['template-source'],
145 clone: !!args.clone,
146 template: args.template,
147 css: args.css,
148 h: args.h
149 });
150 break;
151 }
152 default:
153 customCommand_1.default(command, kernel, args);
154 break;
155 }
156 }
157 else {
158 if (args.h) {
159 console.log('Usage: taro <command> [options]');
160 console.log();
161 console.log('Options:');
162 console.log(' -v, --version output the version number');
163 console.log(' -h, --help output usage information');
164 console.log();
165 console.log('Commands:');
166 console.log(' init [projectName] Init a project with default templete');
167 console.log(' config <cmd> Taro config');
168 console.log(' create Create page for project');
169 console.log(' build Build a project with options');
170 console.log(' update Update packages of taro');
171 console.log(' info Diagnostics Taro env info');
172 console.log(' doctor Diagnose taro project');
173 console.log(' inspect Inspect the webpack config');
174 console.log(' convert Convert native WeiXin-Mini-App to Taro app');
175 console.log(' help [cmd] display help for [cmd]');
176 }
177 else if (args.v) {
178 console.log(util_1.getPkgVersion());
179 }
180 }
181 }
182}
183exports.default = CLI;
184//# sourceMappingURL=cli.js.map
\No newline at end of file