@spcsn/taro-cli 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +174 -0
- package/README.md +0 -0
- package/bin/taro +7 -0
- package/dist/cli.d.ts +6 -0
- package/dist/cli.js +266 -0
- package/dist/cli.js.map +1 -0
- package/dist/commands/customCommand.d.ts +5 -0
- package/dist/commands/customCommand.js +23 -0
- package/dist/commands/customCommand.js.map +1 -0
- package/dist/config/index.d.ts +8 -0
- package/dist/config/index.js +10 -0
- package/dist/config/index.js.map +1 -0
- package/dist/config/packagesManagement.d.ts +19 -0
- package/dist/config/packagesManagement.js +22 -0
- package/dist/config/packagesManagement.js.map +1 -0
- package/dist/create/constants.d.ts +1 -0
- package/dist/create/constants.js +5 -0
- package/dist/create/constants.js.map +1 -0
- package/dist/create/creator.d.ts +9 -0
- package/dist/create/creator.js +64 -0
- package/dist/create/creator.js.map +1 -0
- package/dist/create/fetchTemplate.d.ts +8 -0
- package/dist/create/fetchTemplate.js +169 -0
- package/dist/create/fetchTemplate.js.map +1 -0
- package/dist/create/page.d.ts +65 -0
- package/dist/create/page.js +266 -0
- package/dist/create/page.js.map +1 -0
- package/dist/create/plugin.d.ts +16 -0
- package/dist/create/plugin.js +77 -0
- package/dist/create/plugin.js.map +1 -0
- package/dist/create/project.d.ts +70 -0
- package/dist/create/project.js +481 -0
- package/dist/create/project.js.map +1 -0
- package/dist/doctor/index.d.ts +4 -0
- package/dist/doctor/index.js +23 -0
- package/dist/doctor/index.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/dist/presets/commands/build.d.ts +3 -0
- package/dist/presets/commands/build.js +236 -0
- package/dist/presets/commands/build.js.map +1 -0
- package/dist/presets/commands/config.d.ts +3 -0
- package/dist/presets/commands/config.js +121 -0
- package/dist/presets/commands/config.js.map +1 -0
- package/dist/presets/commands/create.d.ts +3 -0
- package/dist/presets/commands/create.js +112 -0
- package/dist/presets/commands/create.js.map +1 -0
- package/dist/presets/commands/global-config.d.ts +3 -0
- package/dist/presets/commands/global-config.js +175 -0
- package/dist/presets/commands/global-config.js.map +1 -0
- package/dist/presets/commands/help.d.ts +3 -0
- package/dist/presets/commands/help.js +16 -0
- package/dist/presets/commands/help.js.map +1 -0
- package/dist/presets/commands/info.d.ts +3 -0
- package/dist/presets/commands/info.js +96 -0
- package/dist/presets/commands/info.js.map +1 -0
- package/dist/presets/commands/init.d.ts +3 -0
- package/dist/presets/commands/init.js +46 -0
- package/dist/presets/commands/init.js.map +1 -0
- package/dist/presets/commands/inspect.d.ts +3 -0
- package/dist/presets/commands/inspect.js +155 -0
- package/dist/presets/commands/inspect.js.map +1 -0
- package/dist/presets/commands/update.d.ts +3 -0
- package/dist/presets/commands/update.js +187 -0
- package/dist/presets/commands/update.js.map +1 -0
- package/dist/presets/constant/hooks.d.ts +13 -0
- package/dist/presets/constant/hooks.js +17 -0
- package/dist/presets/constant/hooks.js.map +1 -0
- package/dist/presets/constant/index.d.ts +1 -0
- package/dist/presets/constant/index.js +18 -0
- package/dist/presets/constant/index.js.map +1 -0
- package/dist/presets/files/generateFrameworkInfo.d.ts +3 -0
- package/dist/presets/files/generateFrameworkInfo.js +30 -0
- package/dist/presets/files/generateFrameworkInfo.js.map +1 -0
- package/dist/presets/files/generateProjectConfig.d.ts +3 -0
- package/dist/presets/files/generateProjectConfig.js +72 -0
- package/dist/presets/files/generateProjectConfig.js.map +1 -0
- package/dist/presets/files/writeFileToDist.d.ts +3 -0
- package/dist/presets/files/writeFileToDist.js +50 -0
- package/dist/presets/files/writeFileToDist.js.map +1 -0
- package/dist/presets/hooks/build.d.ts +3 -0
- package/dist/presets/hooks/build.js +55 -0
- package/dist/presets/hooks/build.js.map +1 -0
- package/dist/presets/hooks/create.d.ts +3 -0
- package/dist/presets/hooks/create.js +42 -0
- package/dist/presets/hooks/create.js.map +1 -0
- package/dist/presets/index.d.ts +4 -0
- package/dist/presets/index.js +50 -0
- package/dist/presets/index.js.map +1 -0
- package/dist/presets/platforms/plugin.d.ts +3 -0
- package/dist/presets/platforms/plugin.js +106 -0
- package/dist/presets/platforms/plugin.js.map +1 -0
- package/dist/presets/platforms/rn.d.ts +3 -0
- package/dist/presets/platforms/rn.js +130 -0
- package/dist/presets/platforms/rn.js.map +1 -0
- package/dist/util/appConfig.d.ts +11 -0
- package/dist/util/appConfig.js +27 -0
- package/dist/util/appConfig.js.map +1 -0
- package/dist/util/createPage.d.ts +9 -0
- package/dist/util/createPage.js +114 -0
- package/dist/util/createPage.js.map +1 -0
- package/dist/util/defineConfig.d.ts +17 -0
- package/dist/util/defineConfig.js +11 -0
- package/dist/util/defineConfig.js.map +1 -0
- package/dist/util/index.d.ts +23 -0
- package/dist/util/index.js +149 -0
- package/dist/util/index.js.map +1 -0
- package/dist/util/types.d.ts +58 -0
- package/dist/util/types.js +3 -0
- package/dist/util/types.js.map +1 -0
- package/global.d.ts +4 -0
- package/index.js +3 -0
- package/package.json +74 -0
- package/postinstall.js +17 -0
- package/src/__tests__/__mocks__/presets.ts +15 -0
- package/src/__tests__/build-config.spec.ts +66 -0
- package/src/__tests__/cli.spec.ts +226 -0
- package/src/__tests__/config.spec.ts +224 -0
- package/src/__tests__/doctor-config.spec.ts +943 -0
- package/src/__tests__/doctor-recommand.spec.ts +136 -0
- package/src/__tests__/doctor.spec.ts +94 -0
- package/src/__tests__/dotenv-parse.spec.ts +105 -0
- package/src/__tests__/env/.env +2 -0
- package/src/__tests__/fixtures/default/.env +3 -0
- package/src/__tests__/fixtures/default/.env.development +1 -0
- package/src/__tests__/fixtures/default/.env.local +1 -0
- package/src/__tests__/fixtures/default/.env.pre +3 -0
- package/src/__tests__/fixtures/default/.env.production +1 -0
- package/src/__tests__/fixtures/default/.env.uat +5 -0
- package/src/__tests__/fixtures/default/.env.uat.local +1 -0
- package/src/__tests__/fixtures/default/babel.config.js +10 -0
- package/src/__tests__/fixtures/default/config/dev.js +9 -0
- package/src/__tests__/fixtures/default/config/index.js +84 -0
- package/src/__tests__/fixtures/default/config/prod.js +18 -0
- package/src/__tests__/fixtures/default/package.json +27 -0
- package/src/__tests__/fixtures/default/src/app.config.js +11 -0
- package/src/__tests__/fixtures/default/src/app.js +16 -0
- package/src/__tests__/fixtures/default/src/app.scss +0 -0
- package/src/__tests__/fixtures/default/src/index.html +19 -0
- package/src/__tests__/fixtures/default/src/pages/index/index.config.js +3 -0
- package/src/__tests__/fixtures/default/src/pages/index/index.jsx +22 -0
- package/src/__tests__/fixtures/default/src/pages/index/index.scss +0 -0
- package/src/__tests__/info.spec.ts +72 -0
- package/src/__tests__/inspect.spec.ts +160 -0
- package/src/__tests__/update.spec.ts +310 -0
- package/src/__tests__/utils/index.ts +54 -0
- package/src/cli.ts +240 -0
- package/src/commands/customCommand.ts +22 -0
- package/src/config/index.ts +7 -0
- package/src/config/manifest.default.json +33 -0
- package/src/config/packagesManagement.ts +20 -0
- package/src/config/rn-stylelint.json +4 -0
- package/src/config/tsconfig.json +22 -0
- package/src/create/constants.ts +1 -0
- package/src/create/creator.ts +37 -0
- package/src/create/fetchTemplate.ts +161 -0
- package/src/create/page.ts +325 -0
- package/src/create/plugin.ts +55 -0
- package/src/create/project.ts +539 -0
- package/src/doctor/index.ts +21 -0
- package/src/index.ts +24 -0
- package/src/presets/commands/build.ts +215 -0
- package/src/presets/commands/config.ts +86 -0
- package/src/presets/commands/create.ts +104 -0
- package/src/presets/commands/global-config.ts +140 -0
- package/src/presets/commands/help.ts +17 -0
- package/src/presets/commands/info.ts +81 -0
- package/src/presets/commands/init.ts +63 -0
- package/src/presets/commands/inspect.ts +137 -0
- package/src/presets/commands/update.ts +175 -0
- package/src/presets/constant/hooks.ts +14 -0
- package/src/presets/constant/index.ts +1 -0
- package/src/presets/files/generateFrameworkInfo.ts +30 -0
- package/src/presets/files/generateProjectConfig.ts +41 -0
- package/src/presets/files/writeFileToDist.ts +17 -0
- package/src/presets/hooks/build.ts +22 -0
- package/src/presets/hooks/create.ts +9 -0
- package/src/presets/index.ts +15 -0
- package/src/presets/platforms/plugin.ts +75 -0
- package/src/presets/platforms/rn.ts +125 -0
- package/src/util/appConfig.ts +32 -0
- package/src/util/createPage.ts +139 -0
- package/src/util/defineConfig.ts +28 -0
- package/src/util/index.ts +126 -0
- package/src/util/types.ts +81 -0
- package/templates/default/_editorconfig +12 -0
- package/templates/default/_env.development +2 -0
- package/templates/default/_env.production +1 -0
- package/templates/default/_env.test +1 -0
- package/templates/default/_gitignore +8 -0
- package/templates/default/babel.config.js +14 -0
- package/templates/default/config/dev.js +5 -0
- package/templates/default/config/index.js +61 -0
- package/templates/default/config/prod.js +5 -0
- package/templates/default/package.json.tmpl +52 -0
- package/templates/default/project.config.json +15 -0
- package/templates/default/src/app.config.js +11 -0
- package/templates/default/src/app.css +0 -0
- package/templates/default/src/app.js +15 -0
- package/templates/default/src/pages/index/index.config.js +3 -0
- package/templates/default/src/pages/index/index.css +0 -0
- package/templates/default/src/pages/index/index.jsx +15 -0
- package/templates/default/template_creator.js +61 -0
- package/templates/default/tsconfig.json +30 -0
- package/templates/default/types/global.d.ts +27 -0
- package/templates/global-config/index.json +4 -0
- package/templates/global-config/package.json +5 -0
- package/templates/plugin-compile/README.md +32 -0
- package/templates/plugin-compile/index.js +3 -0
- package/templates/plugin-compile/package.json.tmpl +43 -0
- package/templates/plugin-compile/src/index.ts +153 -0
- package/templates/plugin-compile/tsconfig.json +28 -0
package/src/cli.ts
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import { dotenvParse, fs, patchEnv } from '@spcsn/taro-helper';
|
|
4
|
+
import { Config, Kernel } from '@spcsn/taro-service';
|
|
5
|
+
import minimist from 'minimist';
|
|
6
|
+
|
|
7
|
+
import customCommand from './commands/customCommand';
|
|
8
|
+
import { getPkgVersion } from './util';
|
|
9
|
+
|
|
10
|
+
const DISABLE_GLOBAL_CONFIG_COMMANDS = ['global-config', 'doctor', 'update', 'config'];
|
|
11
|
+
const DEFAULT_FRAMEWORK = 'react';
|
|
12
|
+
|
|
13
|
+
export default class CLI {
|
|
14
|
+
appPath: string;
|
|
15
|
+
constructor(appPath?: string) {
|
|
16
|
+
this.appPath = appPath || process.cwd();
|
|
17
|
+
const majorVersion = parseInt(process.version.substring(1).split('.')[0], 10);
|
|
18
|
+
if (majorVersion < 20) {
|
|
19
|
+
console.warn('Taro 将不再支持 Node.js 小于 20 的版本。请升级 Node.js 至 20 或更高版本。');
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
run() {
|
|
24
|
+
return this.parseArgs();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async parseArgs() {
|
|
28
|
+
const args = minimist(process.argv.slice(2), {
|
|
29
|
+
alias: {
|
|
30
|
+
version: ['v'],
|
|
31
|
+
help: ['h'],
|
|
32
|
+
port: ['p'],
|
|
33
|
+
resetCache: ['reset-cache'], // specially for rn, Removes cached files.
|
|
34
|
+
publicPath: ['public-path'], // specially for rn, assets public path.
|
|
35
|
+
bundleOutput: ['bundle-output'], // specially for rn, File name where to store the resulting bundle.
|
|
36
|
+
sourcemapOutput: ['sourcemap-output'], // specially for rn, File name where to store the sourcemap file for resulting bundle.
|
|
37
|
+
sourceMapUrl: ['sourcemap-use-absolute-path'], // specially for rn, Report SourceMapURL using its full path.
|
|
38
|
+
sourcemapSourcesRoot: ['sourcemap-sources-root'], // specially for rn, Path to make sourcemaps sources entries relative to.
|
|
39
|
+
assetsDest: ['assets-dest'], // specially for rn, Directory name where to store assets referenced in the bundle.
|
|
40
|
+
envPrefix: ['env-prefix'],
|
|
41
|
+
},
|
|
42
|
+
boolean: ['version', 'help', 'disable-global-config'],
|
|
43
|
+
default: {
|
|
44
|
+
build: true,
|
|
45
|
+
check: true,
|
|
46
|
+
'inject-global-style': true,
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
const _ = args._;
|
|
50
|
+
const command = _[0];
|
|
51
|
+
if (command) {
|
|
52
|
+
const appPath = this.appPath;
|
|
53
|
+
const presetsPath = path.resolve(__dirname, 'presets');
|
|
54
|
+
const commandsPath = path.resolve(presetsPath, 'commands');
|
|
55
|
+
const platformsPath = path.resolve(presetsPath, 'platforms');
|
|
56
|
+
const commandPlugins = fs.readdirSync(commandsPath);
|
|
57
|
+
const targetPlugin = `${command}.js`;
|
|
58
|
+
|
|
59
|
+
// 设置环境变量
|
|
60
|
+
process.env.NODE_ENV ||= args.env;
|
|
61
|
+
if (process.env.NODE_ENV === 'undefined' && (command === 'build' || command === 'inspect')) {
|
|
62
|
+
process.env.NODE_ENV = args.watch ? 'development' : 'production';
|
|
63
|
+
}
|
|
64
|
+
args.type ||= args.t;
|
|
65
|
+
// React-only / weapp-first fork:build/inspect 命令未显式指定 --type 时默认 weapp
|
|
66
|
+
if (!args.type && (command === 'build' || command === 'inspect') && typeof args.plugin !== 'string') {
|
|
67
|
+
args.type = 'weapp';
|
|
68
|
+
}
|
|
69
|
+
if (args.type) {
|
|
70
|
+
process.env.TARO_ENV = args.type;
|
|
71
|
+
}
|
|
72
|
+
if (typeof args.plugin === 'string') {
|
|
73
|
+
process.env.TARO_ENV = 'plugin';
|
|
74
|
+
}
|
|
75
|
+
const mode = args.mode || process.env.NODE_ENV;
|
|
76
|
+
// 这里解析 dotenv 以便于 config 解析时能获取 dotenv 配置信息
|
|
77
|
+
const expandEnv = dotenvParse(appPath, args.envPrefix, mode);
|
|
78
|
+
|
|
79
|
+
const disableGlobalConfig = !!(args['disable-global-config'] || DISABLE_GLOBAL_CONFIG_COMMANDS.includes(command));
|
|
80
|
+
|
|
81
|
+
const configEnv = {
|
|
82
|
+
mode,
|
|
83
|
+
command,
|
|
84
|
+
};
|
|
85
|
+
const config = new Config({
|
|
86
|
+
appPath: this.appPath,
|
|
87
|
+
disableGlobalConfig: disableGlobalConfig,
|
|
88
|
+
});
|
|
89
|
+
await config.init(configEnv);
|
|
90
|
+
|
|
91
|
+
const kernel = new Kernel({
|
|
92
|
+
appPath,
|
|
93
|
+
presets: [path.resolve(__dirname, '.', 'presets', 'index.js')],
|
|
94
|
+
config,
|
|
95
|
+
plugins: [],
|
|
96
|
+
});
|
|
97
|
+
kernel.optsPlugins ||= [];
|
|
98
|
+
|
|
99
|
+
// 将自定义的 变量 添加到 config.env 中,实现 definePlugin 字段定义
|
|
100
|
+
const initialConfig = kernel.config?.initialConfig;
|
|
101
|
+
if (initialConfig) {
|
|
102
|
+
initialConfig.env = patchEnv(initialConfig, expandEnv);
|
|
103
|
+
}
|
|
104
|
+
if (command === 'doctor') {
|
|
105
|
+
kernel.optsPlugins.push('@tarojs/plugin-doctor');
|
|
106
|
+
} else if (commandPlugins.includes(targetPlugin)) {
|
|
107
|
+
// 针对不同的内置命令注册对应的命令插件
|
|
108
|
+
kernel.optsPlugins.push(path.resolve(commandsPath, targetPlugin));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// 把内置命令插件传递给 kernel,可以暴露给其他插件使用
|
|
112
|
+
kernel.cliCommandsPath = commandsPath;
|
|
113
|
+
kernel.cliCommands = commandPlugins
|
|
114
|
+
.filter((commandFileName) => /^[\w-]+(\.[\w-]+)*\.js$/.test(commandFileName))
|
|
115
|
+
.map((fileName) => fileName.replace(/\.js$/, ''));
|
|
116
|
+
|
|
117
|
+
switch (command) {
|
|
118
|
+
case 'inspect':
|
|
119
|
+
case 'build': {
|
|
120
|
+
let plugin;
|
|
121
|
+
let platform = args.type;
|
|
122
|
+
const { publicPath, bundleOutput, sourcemapOutput, sourceMapUrl, sourcemapSourcesRoot, assetsDest } = args;
|
|
123
|
+
|
|
124
|
+
// 针对不同的内置平台注册对应的端平台插件
|
|
125
|
+
switch (platform) {
|
|
126
|
+
case 'weapp':
|
|
127
|
+
kernel.optsPlugins.push(`@spcsn/taro-plugin-platform-${platform}`);
|
|
128
|
+
break;
|
|
129
|
+
default: {
|
|
130
|
+
if (platform) {
|
|
131
|
+
console.log('当前 Fork 仅支持微信小程序(weapp)构建。');
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// 根据 framework 启用插件
|
|
139
|
+
const framework = kernel.config?.initialConfig.framework || DEFAULT_FRAMEWORK;
|
|
140
|
+
if (framework !== 'react') {
|
|
141
|
+
console.log('当前 Fork 仅支持 React 框架。');
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
kernel.optsPlugins.push('@spcsn/taro-plugin-framework-react');
|
|
145
|
+
|
|
146
|
+
// 编译小程序插件
|
|
147
|
+
if (typeof args.plugin === 'string') {
|
|
148
|
+
plugin = args.plugin;
|
|
149
|
+
if (plugin !== 'weapp') {
|
|
150
|
+
console.log('当前 Fork 仅支持 weapp 小程序插件编译。');
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
platform = 'plugin';
|
|
154
|
+
kernel.optsPlugins.push(path.resolve(platformsPath, 'plugin.js'));
|
|
155
|
+
kernel.optsPlugins.push(`@spcsn/taro-plugin-platform-${plugin}`);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 传递 inspect 参数即可
|
|
159
|
+
if (command === 'inspect') {
|
|
160
|
+
customCommand(command, kernel, args);
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
customCommand(command, kernel, {
|
|
164
|
+
args,
|
|
165
|
+
_,
|
|
166
|
+
platform,
|
|
167
|
+
plugin,
|
|
168
|
+
isWatch: Boolean(args.watch),
|
|
169
|
+
// Note: 是否把 Taro 组件编译为原生自定义组件
|
|
170
|
+
isBuildNativeComp: _[1] === 'native-components',
|
|
171
|
+
// Note: 新的混合编译模式,支持把组件单独编译为原生组件
|
|
172
|
+
newBlended: Boolean(args['new-blended']),
|
|
173
|
+
// Note: 是否禁用编译
|
|
174
|
+
withoutBuild: !args.build,
|
|
175
|
+
noInjectGlobalStyle: !args['inject-global-style'],
|
|
176
|
+
noCheck: !args.check,
|
|
177
|
+
port: args.port,
|
|
178
|
+
env: args.env,
|
|
179
|
+
deviceType: args.platform,
|
|
180
|
+
resetCache: !!args.resetCache,
|
|
181
|
+
publicPath,
|
|
182
|
+
bundleOutput,
|
|
183
|
+
sourcemapOutput,
|
|
184
|
+
sourceMapUrl,
|
|
185
|
+
sourcemapSourcesRoot,
|
|
186
|
+
assetsDest,
|
|
187
|
+
qr: !!args.qr,
|
|
188
|
+
blended: Boolean(args.blended),
|
|
189
|
+
h: args.h,
|
|
190
|
+
});
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
case 'init': {
|
|
194
|
+
customCommand(command, kernel, {
|
|
195
|
+
_,
|
|
196
|
+
appPath,
|
|
197
|
+
projectName: _[1] || args.name,
|
|
198
|
+
description: args.description,
|
|
199
|
+
typescript: args.typescript,
|
|
200
|
+
buildEs5: args['build-es5'],
|
|
201
|
+
framework: args.framework,
|
|
202
|
+
compiler: args.compiler,
|
|
203
|
+
npm: args.npm,
|
|
204
|
+
templateSource: args['template-source'],
|
|
205
|
+
clone: !!args.clone,
|
|
206
|
+
template: args.template,
|
|
207
|
+
css: args.css,
|
|
208
|
+
autoInstall: args.autoInstall,
|
|
209
|
+
h: args.h,
|
|
210
|
+
});
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
default:
|
|
214
|
+
customCommand(command, kernel, args);
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
} else {
|
|
218
|
+
if (args.h) {
|
|
219
|
+
console.log('Usage: taro <command> [options]');
|
|
220
|
+
console.log();
|
|
221
|
+
console.log('Options:');
|
|
222
|
+
console.log(' -v, --version output the version number');
|
|
223
|
+
console.log(' -h, --help output usage information');
|
|
224
|
+
console.log();
|
|
225
|
+
console.log('Commands:');
|
|
226
|
+
console.log(' init [projectName] Init a project with default templete');
|
|
227
|
+
console.log(' config <cmd> Taro config');
|
|
228
|
+
console.log(' create Create page for project');
|
|
229
|
+
console.log(' build Build a project with options');
|
|
230
|
+
console.log(' update Update packages of taro');
|
|
231
|
+
console.log(' info Diagnostics Taro env info');
|
|
232
|
+
console.log(' doctor Diagnose taro project');
|
|
233
|
+
console.log(' inspect Inspect the webpack config');
|
|
234
|
+
console.log(' help [cmd] display help for [cmd]');
|
|
235
|
+
} else if (args.v) {
|
|
236
|
+
console.log(getPkgVersion());
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Kernel } from '@spcsn/taro-service';
|
|
2
|
+
|
|
3
|
+
export default function customCommand(command: string, kernel: Kernel, args: { _: string[]; [key: string]: any }) {
|
|
4
|
+
if (typeof command === 'string') {
|
|
5
|
+
const options: any = {};
|
|
6
|
+
const excludeKeys = ['_', 'version', 'v', 'help', 'h', 'disable-global-config'];
|
|
7
|
+
Object.keys(args).forEach((key) => {
|
|
8
|
+
if (!excludeKeys.includes(key)) {
|
|
9
|
+
options[key] = args[key];
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
kernel.run({
|
|
14
|
+
name: command,
|
|
15
|
+
opts: {
|
|
16
|
+
_: args._,
|
|
17
|
+
options,
|
|
18
|
+
isHelp: args.h,
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"package": "com.application.demo",
|
|
3
|
+
"name": "TaroQuickApp",
|
|
4
|
+
"versionName": "1.0.0",
|
|
5
|
+
"versionCode": 1,
|
|
6
|
+
"minPlatformVersion": 1040,
|
|
7
|
+
"features": [
|
|
8
|
+
{ "name": "system.prompt" },
|
|
9
|
+
{ "name": "system.router" },
|
|
10
|
+
{ "name": "system.shortcut" },
|
|
11
|
+
{ "name": "system.fetch" },
|
|
12
|
+
{ "name": "system.storage" },
|
|
13
|
+
{ "name": "system.barcode" },
|
|
14
|
+
{ "name": "system.vibrator" },
|
|
15
|
+
{ "name": "system.clipboard" },
|
|
16
|
+
{ "name": "system.sensor" },
|
|
17
|
+
{ "name": "system.geolocation" },
|
|
18
|
+
{ "name": "system.share" },
|
|
19
|
+
{ "name": "system.notification" },
|
|
20
|
+
{ "name": "system.device" },
|
|
21
|
+
{ "name": "system.webview" },
|
|
22
|
+
{ "name": "system.request" },
|
|
23
|
+
{ "name": "system.audio" },
|
|
24
|
+
{ "name": "system.record" },
|
|
25
|
+
{ "name": "system.contact" },
|
|
26
|
+
{ "name": "system.sms" }
|
|
27
|
+
],
|
|
28
|
+
"icon": "logo.png",
|
|
29
|
+
"permissions": [{ "origin": "*" }],
|
|
30
|
+
"config": {
|
|
31
|
+
"logLevel": "debug"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const packagesManagement = {
|
|
2
|
+
yarn: {
|
|
3
|
+
command: 'yarn install',
|
|
4
|
+
globalCommand: 'yarn global add @spcsn/taro-cli',
|
|
5
|
+
},
|
|
6
|
+
pnpm: {
|
|
7
|
+
command: 'pnpm install',
|
|
8
|
+
globalCommand: 'pnpm add -g @spcsn/taro-cli',
|
|
9
|
+
},
|
|
10
|
+
cnpm: {
|
|
11
|
+
command: 'cnpm install',
|
|
12
|
+
globalCommand: 'cnpm i -g @spcsn/taro-cli',
|
|
13
|
+
},
|
|
14
|
+
npm: {
|
|
15
|
+
command: 'npm install',
|
|
16
|
+
globalCommand: 'npm i -g @spcsn/taro-cli',
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export default packagesManagement;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"allowSyntheticDefaultImports": true,
|
|
4
|
+
"declaration": false,
|
|
5
|
+
"experimentalDecorators": true,
|
|
6
|
+
"jsx": "react",
|
|
7
|
+
"jsxFactory": "React.createElement",
|
|
8
|
+
"module": "commonjs",
|
|
9
|
+
"noImplicitAny": false,
|
|
10
|
+
"noUnusedLocals": true,
|
|
11
|
+
"outDir": "./dist/",
|
|
12
|
+
"preserveConstEnums": true,
|
|
13
|
+
"removeComments": false,
|
|
14
|
+
"rootDir": ".",
|
|
15
|
+
"sourceMap": true,
|
|
16
|
+
"strictNullChecks": true,
|
|
17
|
+
"target": "es6"
|
|
18
|
+
},
|
|
19
|
+
"include": ["src/**/*"],
|
|
20
|
+
"exclude": ["node_modules"],
|
|
21
|
+
"compileOnSave": false
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const TEMPLATE_CREATOR = 'template_creator.js';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import { fs } from '@spcsn/taro-helper';
|
|
4
|
+
|
|
5
|
+
import { getRootPath } from '../util';
|
|
6
|
+
|
|
7
|
+
export default class Creator {
|
|
8
|
+
protected _rootPath!: string;
|
|
9
|
+
public rootPath: string;
|
|
10
|
+
|
|
11
|
+
constructor(sourceRoot?: string) {
|
|
12
|
+
this.rootPath = this.sourceRoot(sourceRoot || path.join(getRootPath()));
|
|
13
|
+
this.init();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
init() {}
|
|
17
|
+
|
|
18
|
+
sourceRoot(rootPath?: string) {
|
|
19
|
+
if (typeof rootPath === 'string') {
|
|
20
|
+
this._rootPath = path.resolve(rootPath);
|
|
21
|
+
}
|
|
22
|
+
if (!fs.existsSync(this._rootPath)) {
|
|
23
|
+
fs.ensureDirSync(this._rootPath);
|
|
24
|
+
}
|
|
25
|
+
return this._rootPath;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
templatePath(...args: string[]): string {
|
|
29
|
+
let filepath = path.join.apply(path, args);
|
|
30
|
+
if (!path.isAbsolute(filepath)) {
|
|
31
|
+
filepath = path.join(this._rootPath, 'templates', filepath);
|
|
32
|
+
}
|
|
33
|
+
return filepath;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
write() {}
|
|
37
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import { chalk, fs } from '@spcsn/taro-helper';
|
|
4
|
+
import * as AdmZip from 'adm-zip';
|
|
5
|
+
import axios from 'axios';
|
|
6
|
+
import * as download from 'download-git-repo';
|
|
7
|
+
import ora from 'ora';
|
|
8
|
+
|
|
9
|
+
import { getTemplateSourceType, readDirWithFileTypes } from '../util';
|
|
10
|
+
import { TEMPLATE_CREATOR } from './constants';
|
|
11
|
+
|
|
12
|
+
export interface ITemplates {
|
|
13
|
+
name: string;
|
|
14
|
+
value: string;
|
|
15
|
+
platforms?: string | string[];
|
|
16
|
+
desc?: string;
|
|
17
|
+
compiler?: string[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const TEMP_DOWNLOAD_FOLDER = 'taro-temp';
|
|
21
|
+
|
|
22
|
+
export default function fetchTemplate(
|
|
23
|
+
templateSource: string,
|
|
24
|
+
templateRootPath: string,
|
|
25
|
+
clone?: boolean,
|
|
26
|
+
): Promise<ITemplates[]> {
|
|
27
|
+
const type = getTemplateSourceType(templateSource);
|
|
28
|
+
const tempPath = path.join(templateRootPath, TEMP_DOWNLOAD_FOLDER);
|
|
29
|
+
let name: string;
|
|
30
|
+
// eslint-disable-next-line no-async-promise-executor
|
|
31
|
+
return new Promise<void>(async (resolve) => {
|
|
32
|
+
// 下载文件的缓存目录
|
|
33
|
+
if (fs.existsSync(tempPath)) await fs.remove(tempPath);
|
|
34
|
+
await fs.mkdirp(templateRootPath);
|
|
35
|
+
await fs.mkdir(tempPath);
|
|
36
|
+
|
|
37
|
+
const spinner = ora(`正在从 ${templateSource} 拉取远程模板...`).start();
|
|
38
|
+
|
|
39
|
+
if (type === 'git') {
|
|
40
|
+
name = path.basename(templateSource);
|
|
41
|
+
download(templateSource, path.join(tempPath, name), { clone }, async (error) => {
|
|
42
|
+
if (error) {
|
|
43
|
+
console.log(error);
|
|
44
|
+
spinner.color = 'red';
|
|
45
|
+
spinner.fail(chalk.red('拉取远程模板仓库失败!'));
|
|
46
|
+
await fs.remove(tempPath);
|
|
47
|
+
return resolve();
|
|
48
|
+
}
|
|
49
|
+
spinner.color = 'green';
|
|
50
|
+
spinner.succeed(`${chalk.grey('拉取远程模板仓库成功!')}`);
|
|
51
|
+
resolve();
|
|
52
|
+
});
|
|
53
|
+
} else if (type === 'url') {
|
|
54
|
+
// url 模板源,因为不知道来源名称,临时取名方便后续开发者从列表中选择
|
|
55
|
+
name = 'from-remote-url';
|
|
56
|
+
const zipPath = path.join(tempPath, name + '.zip');
|
|
57
|
+
const unZipPath = path.join(tempPath, name);
|
|
58
|
+
axios
|
|
59
|
+
.get<fs.ReadStream>(templateSource, { responseType: 'stream' })
|
|
60
|
+
.then((response) => {
|
|
61
|
+
const ws = fs.createWriteStream(zipPath);
|
|
62
|
+
response.data.pipe(ws);
|
|
63
|
+
ws.on('finish', () => {
|
|
64
|
+
// unzip
|
|
65
|
+
const zip = new AdmZip(zipPath);
|
|
66
|
+
zip.extractAllTo(unZipPath, true);
|
|
67
|
+
const files = readDirWithFileTypes(unZipPath).filter(
|
|
68
|
+
(file) => !file.name.startsWith('.') && file.isDirectory && file.name !== '__MACOSX',
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
if (files.length !== 1) {
|
|
72
|
+
spinner.color = 'red';
|
|
73
|
+
spinner.fail(chalk.red(`拉取远程模板仓库失败!\n${new Error('远程模板源组织格式错误')}`));
|
|
74
|
+
return resolve();
|
|
75
|
+
}
|
|
76
|
+
name = path.join(name, files[0].name);
|
|
77
|
+
|
|
78
|
+
spinner.color = 'green';
|
|
79
|
+
spinner.succeed(`${chalk.grey('拉取远程模板仓库成功!')}`);
|
|
80
|
+
resolve();
|
|
81
|
+
});
|
|
82
|
+
ws.on('error', (error) => {
|
|
83
|
+
throw error;
|
|
84
|
+
});
|
|
85
|
+
})
|
|
86
|
+
.catch(async (error) => {
|
|
87
|
+
spinner.color = 'red';
|
|
88
|
+
spinner.fail(chalk.red(`拉取远程模板仓库失败!\n${error}`));
|
|
89
|
+
await fs.remove(tempPath);
|
|
90
|
+
return resolve();
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}).then(async () => {
|
|
94
|
+
const templateFolder = name ? path.join(tempPath, name) : '';
|
|
95
|
+
|
|
96
|
+
// 下载失败,只显示默认模板
|
|
97
|
+
if (!fs.existsSync(templateFolder)) return Promise.resolve([]);
|
|
98
|
+
|
|
99
|
+
const isTemplateGroup = !(
|
|
100
|
+
fs.existsSync(path.join(templateFolder, 'package.json')) ||
|
|
101
|
+
fs.existsSync(path.join(templateFolder, 'package.json.tmpl'))
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
if (isTemplateGroup) {
|
|
105
|
+
// 模板组
|
|
106
|
+
const files = readDirWithFileTypes(templateFolder)
|
|
107
|
+
.filter((file) => !file.name.startsWith('.') && file.isDirectory && file.name !== '__MACOSX')
|
|
108
|
+
.map((file) => file.name);
|
|
109
|
+
await Promise.all(
|
|
110
|
+
files.map((file) => {
|
|
111
|
+
const src = path.join(templateFolder, file);
|
|
112
|
+
const dest = path.join(templateRootPath, file);
|
|
113
|
+
return fs.move(src, dest, { overwrite: true });
|
|
114
|
+
}),
|
|
115
|
+
);
|
|
116
|
+
await fs.remove(tempPath);
|
|
117
|
+
|
|
118
|
+
const res: ITemplates[] = files
|
|
119
|
+
.map((name) => {
|
|
120
|
+
const creatorFile = path.join(templateRootPath, name, TEMPLATE_CREATOR);
|
|
121
|
+
|
|
122
|
+
if (!fs.existsSync(creatorFile)) return { name, value: name };
|
|
123
|
+
const { name: displayName, platforms = '', desc = '', isPrivate = false, compiler } = require(creatorFile);
|
|
124
|
+
if (isPrivate) return null;
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
name: displayName || name,
|
|
128
|
+
value: name,
|
|
129
|
+
platforms,
|
|
130
|
+
compiler,
|
|
131
|
+
desc,
|
|
132
|
+
};
|
|
133
|
+
})
|
|
134
|
+
.filter(Boolean) as ITemplates[];
|
|
135
|
+
|
|
136
|
+
return Promise.resolve(res);
|
|
137
|
+
} else {
|
|
138
|
+
// 单模板
|
|
139
|
+
await fs.move(templateFolder, path.join(templateRootPath, name), { overwrite: true });
|
|
140
|
+
await fs.remove(tempPath);
|
|
141
|
+
|
|
142
|
+
let res: ITemplates = { name, value: name, desc: type === 'url' ? templateSource : '' };
|
|
143
|
+
|
|
144
|
+
const creatorFile = path.join(templateRootPath, name, TEMPLATE_CREATOR);
|
|
145
|
+
|
|
146
|
+
if (fs.existsSync(creatorFile)) {
|
|
147
|
+
const { name: displayName, platforms = '', desc = '', compiler } = require(creatorFile);
|
|
148
|
+
|
|
149
|
+
res = {
|
|
150
|
+
name: displayName || name,
|
|
151
|
+
value: name,
|
|
152
|
+
platforms,
|
|
153
|
+
compiler,
|
|
154
|
+
desc: desc || templateSource,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return Promise.resolve([res]);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|