@spcsn/taro-cli 0.1.5 → 1.0.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/README.md +37 -0
- package/define-config.d.ts +1 -0
- package/define-config.js +1 -0
- package/dist/cli.js +140 -110
- package/dist/cli.js.map +1 -1
- package/dist/commands/{customCommand.d.ts → custom-command.d.ts} +1 -1
- package/dist/commands/custom-command.js +23 -0
- package/dist/commands/custom-command.js.map +1 -0
- package/dist/create/creator.js +2 -0
- package/dist/create/creator.js.map +1 -1
- package/dist/create/{fetchTemplate.js → fetch-template.js} +26 -33
- package/dist/create/fetch-template.js.map +1 -0
- package/dist/create/project.d.ts +2 -6
- package/dist/create/project.js +240 -285
- package/dist/create/project.js.map +1 -1
- package/dist/create/template-creator.d.ts +75 -0
- package/dist/create/template-creator.js +275 -0
- package/dist/create/template-creator.js.map +1 -0
- package/dist/doctor/validators.d.ts +7 -6
- package/dist/doctor/validators.js +6 -7
- package/dist/doctor/validators.js.map +1 -1
- package/dist/platform-weapp/apis-list.d.ts +1 -0
- package/dist/platform-weapp/apis-list.js +37 -0
- package/dist/platform-weapp/apis-list.js.map +1 -0
- package/dist/platform-weapp/apis.d.ts +16 -0
- package/dist/platform-weapp/apis.js +34 -0
- package/dist/platform-weapp/apis.js.map +1 -0
- package/dist/platform-weapp/components-react.d.ts +80 -0
- package/dist/platform-weapp/components-react.js +86 -0
- package/dist/platform-weapp/components-react.js.map +1 -0
- package/dist/platform-weapp/components.d.ts +4 -0
- package/dist/platform-weapp/components.js +520 -0
- package/dist/platform-weapp/components.js.map +1 -0
- package/dist/platform-weapp/index.d.ts +11 -0
- package/dist/platform-weapp/index.js +17 -0
- package/dist/platform-weapp/index.js.map +1 -0
- package/dist/platform-weapp/program.d.ts +35 -0
- package/dist/platform-weapp/program.js +64 -0
- package/dist/platform-weapp/program.js.map +1 -0
- package/dist/platform-weapp/runtime-utils.d.ts +27 -0
- package/dist/platform-weapp/runtime-utils.js +48 -0
- package/dist/platform-weapp/runtime-utils.js.map +1 -0
- package/dist/platform-weapp/runtime.d.ts +1 -0
- package/dist/platform-weapp/runtime.js +7 -0
- package/dist/platform-weapp/runtime.js.map +1 -0
- package/dist/platform-weapp/template.d.ts +39 -0
- package/dist/platform-weapp/template.js +119 -0
- package/dist/platform-weapp/template.js.map +1 -0
- package/dist/presets/commands/build.js +25 -45
- package/dist/presets/commands/build.js.map +1 -1
- package/dist/presets/commands/init.js +2 -4
- package/dist/presets/commands/init.js.map +1 -1
- package/dist/presets/constant/hooks.d.ts +0 -2
- package/dist/presets/constant/hooks.js +1 -3
- package/dist/presets/constant/hooks.js.map +1 -1
- package/dist/presets/files/generate-framework-info.js +68 -0
- package/dist/presets/files/generate-framework-info.js.map +1 -0
- package/dist/presets/files/{generateProjectConfig.js → generate-project-config.js} +1 -1
- package/dist/presets/files/generate-project-config.js.map +1 -0
- package/dist/presets/files/{writeFileToDist.js → write-file-to-dist.js} +1 -1
- package/dist/presets/files/write-file-to-dist.js.map +1 -0
- package/dist/presets/hooks/build.js +1 -3
- package/dist/presets/hooks/build.js.map +1 -1
- package/dist/presets/index.js +3 -5
- package/dist/presets/index.js.map +1 -1
- package/dist/util/{appConfig.d.ts → app-config.d.ts} +2 -2
- package/dist/util/{appConfig.js → app-config.js} +4 -3
- package/dist/util/app-config.js.map +1 -0
- package/dist/util/defineConfig.d.ts +5 -5
- package/dist/util/defineConfig.js.map +1 -1
- package/dist/util/index.d.ts +9 -1
- package/dist/util/index.js +54 -23
- package/dist/util/index.js.map +1 -1
- package/package.json +36 -39
- package/postinstall.js +1 -17
- package/src/cli.ts +185 -132
- package/src/commands/custom-command.ts +22 -0
- package/src/create/{fetchTemplate.ts → fetch-template.ts} +30 -35
- package/src/create/project.ts +31 -78
- package/src/create/template-creator.ts +325 -0
- package/src/doctor/validators.js +1 -0
- package/src/doctor/validators.ts +8 -6
- package/src/platform-weapp/apis-list.ts +33 -0
- package/src/platform-weapp/apis.ts +48 -0
- package/src/platform-weapp/components-react.ts +81 -0
- package/src/platform-weapp/components.ts +521 -0
- package/src/platform-weapp/index.ts +25 -0
- package/src/platform-weapp/program.ts +70 -0
- package/src/platform-weapp/runtime-utils.ts +57 -0
- package/src/platform-weapp/runtime.ts +6 -0
- package/src/platform-weapp/template.ts +150 -0
- package/src/presets/commands/build.ts +20 -40
- package/src/presets/commands/init.ts +1 -4
- package/src/presets/constant/hooks.js +12 -0
- package/src/presets/constant/hooks.ts +0 -2
- package/src/presets/files/{generateFrameworkInfo.ts → generate-framework-info.ts} +7 -1
- package/src/presets/hooks/build.ts +1 -3
- package/src/presets/index.ts +3 -5
- package/src/util/app-config.js +1 -0
- package/src/util/{appConfig.ts → app-config.ts} +5 -4
- package/src/util/defineConfig.ts +6 -6
- package/src/util/index.ts +67 -25
- package/templates/default/config/dev.js +1 -1
- package/templates/default/config/index.js +28 -40
- package/templates/default/config/prod.js +1 -1
- package/templates/default/package.json.tmpl +2 -17
- package/templates/default/types/global.d.ts +0 -4
- package/templates/plugin-compile/README.md +3 -1
- package/templates/plugin-compile/package.json.tmpl +1 -2
- package/templates/plugin-compile/src/index.ts +18 -28
- package/LICENSE +0 -174
- package/dist/commands/customCommand.js +0 -23
- package/dist/commands/customCommand.js.map +0 -1
- package/dist/config/packagesManagement.d.ts +0 -19
- package/dist/config/packagesManagement.js +0 -22
- package/dist/config/packagesManagement.js.map +0 -1
- package/dist/create/fetchTemplate.js.map +0 -1
- package/dist/create/page.d.ts +0 -65
- package/dist/create/page.js +0 -266
- package/dist/create/page.js.map +0 -1
- package/dist/create/plugin.d.ts +0 -16
- package/dist/create/plugin.js +0 -77
- package/dist/create/plugin.js.map +0 -1
- package/dist/presets/commands/config.js +0 -121
- package/dist/presets/commands/config.js.map +0 -1
- package/dist/presets/commands/create.js +0 -112
- package/dist/presets/commands/create.js.map +0 -1
- package/dist/presets/commands/doctor.js +0 -15
- package/dist/presets/commands/doctor.js.map +0 -1
- package/dist/presets/commands/global-config.d.ts +0 -3
- package/dist/presets/commands/global-config.js +0 -175
- package/dist/presets/commands/global-config.js.map +0 -1
- package/dist/presets/commands/help.d.ts +0 -3
- package/dist/presets/commands/help.js +0 -16
- package/dist/presets/commands/help.js.map +0 -1
- package/dist/presets/commands/info.d.ts +0 -3
- package/dist/presets/commands/info.js +0 -96
- package/dist/presets/commands/info.js.map +0 -1
- package/dist/presets/commands/inspect.d.ts +0 -3
- package/dist/presets/commands/inspect.js +0 -155
- package/dist/presets/commands/inspect.js.map +0 -1
- package/dist/presets/commands/update.d.ts +0 -3
- package/dist/presets/commands/update.js +0 -187
- package/dist/presets/commands/update.js.map +0 -1
- package/dist/presets/files/generateFrameworkInfo.d.ts +0 -3
- package/dist/presets/files/generateFrameworkInfo.js +0 -30
- package/dist/presets/files/generateFrameworkInfo.js.map +0 -1
- package/dist/presets/files/generateProjectConfig.d.ts +0 -3
- package/dist/presets/files/generateProjectConfig.js.map +0 -1
- package/dist/presets/files/writeFileToDist.d.ts +0 -3
- package/dist/presets/files/writeFileToDist.js.map +0 -1
- package/dist/presets/hooks/create.d.ts +0 -3
- package/dist/presets/hooks/create.js +0 -42
- package/dist/presets/hooks/create.js.map +0 -1
- package/dist/presets/platforms/plugin.d.ts +0 -3
- package/dist/presets/platforms/plugin.js +0 -106
- package/dist/presets/platforms/plugin.js.map +0 -1
- package/dist/presets/platforms/rn.d.ts +0 -3
- package/dist/presets/platforms/rn.js +0 -130
- package/dist/presets/platforms/rn.js.map +0 -1
- package/dist/util/appConfig.js.map +0 -1
- package/dist/util/createPage.d.ts +0 -9
- package/dist/util/createPage.js +0 -114
- package/dist/util/createPage.js.map +0 -1
- package/src/__tests__/__mocks__/presets.ts +0 -15
- package/src/__tests__/build-config.spec.ts +0 -66
- package/src/__tests__/cli.spec.ts +0 -226
- package/src/__tests__/config.spec.ts +0 -224
- package/src/__tests__/doctor-config.spec.ts +0 -918
- package/src/__tests__/doctor-recommand.spec.ts +0 -136
- package/src/__tests__/doctor.spec.ts +0 -94
- package/src/__tests__/dotenv-parse.spec.ts +0 -105
- package/src/__tests__/env/.env +0 -2
- package/src/__tests__/fixtures/default/.env +0 -3
- package/src/__tests__/fixtures/default/.env.development +0 -1
- package/src/__tests__/fixtures/default/.env.local +0 -1
- package/src/__tests__/fixtures/default/.env.pre +0 -3
- package/src/__tests__/fixtures/default/.env.production +0 -1
- package/src/__tests__/fixtures/default/.env.uat +0 -5
- package/src/__tests__/fixtures/default/.env.uat.local +0 -1
- package/src/__tests__/fixtures/default/babel.config.js +0 -10
- package/src/__tests__/fixtures/default/config/dev.js +0 -9
- package/src/__tests__/fixtures/default/config/index.js +0 -84
- package/src/__tests__/fixtures/default/config/prod.js +0 -18
- package/src/__tests__/fixtures/default/package.json +0 -27
- package/src/__tests__/fixtures/default/src/app.config.js +0 -11
- package/src/__tests__/fixtures/default/src/app.js +0 -16
- package/src/__tests__/fixtures/default/src/app.scss +0 -0
- package/src/__tests__/fixtures/default/src/index.html +0 -19
- package/src/__tests__/fixtures/default/src/pages/index/index.config.js +0 -3
- package/src/__tests__/fixtures/default/src/pages/index/index.jsx +0 -22
- package/src/__tests__/fixtures/default/src/pages/index/index.scss +0 -0
- package/src/__tests__/info.spec.ts +0 -72
- package/src/__tests__/inspect.spec.ts +0 -160
- package/src/__tests__/update.spec.ts +0 -310
- package/src/__tests__/utils/index.ts +0 -54
- package/src/commands/customCommand.ts +0 -22
- package/src/config/packagesManagement.ts +0 -20
- package/src/config/rn-stylelint.json +0 -4
- package/src/create/page.ts +0 -325
- package/src/create/plugin.ts +0 -55
- package/src/presets/commands/config.ts +0 -86
- package/src/presets/commands/create.ts +0 -104
- package/src/presets/commands/doctor.ts +0 -21
- package/src/presets/commands/global-config.ts +0 -140
- package/src/presets/commands/help.ts +0 -17
- package/src/presets/commands/info.ts +0 -81
- package/src/presets/commands/inspect.ts +0 -137
- package/src/presets/commands/update.ts +0 -175
- package/src/presets/hooks/create.ts +0 -9
- package/src/presets/platforms/plugin.ts +0 -75
- package/src/presets/platforms/rn.ts +0 -125
- package/src/util/createPage.ts +0 -139
- package/templates/default/babel.config.js +0 -14
- /package/dist/create/{fetchTemplate.d.ts → fetch-template.d.ts} +0 -0
- /package/dist/presets/{commands/config.d.ts → files/generate-framework-info.d.ts} +0 -0
- /package/dist/presets/{commands/create.d.ts → files/generate-project-config.d.ts} +0 -0
- /package/dist/presets/{commands/doctor.d.ts → files/write-file-to-dist.d.ts} +0 -0
- /package/src/presets/files/{generateProjectConfig.ts → generate-project-config.ts} +0 -0
- /package/src/presets/files/{writeFileToDist.ts → write-file-to-dist.ts} +0 -0
package/src/cli.ts
CHANGED
|
@@ -1,14 +1,117 @@
|
|
|
1
1
|
import * as path from 'node:path';
|
|
2
2
|
|
|
3
|
-
import { dotenvParse,
|
|
3
|
+
import { dotenvParse, patchEnv } from '@spcsn/taro-helper';
|
|
4
4
|
import { Config, Kernel } from '@spcsn/taro-service';
|
|
5
|
-
import minimist from 'minimist';
|
|
6
5
|
|
|
7
|
-
import customCommand from './commands/
|
|
8
|
-
import { getPkgVersion } from './util';
|
|
6
|
+
import customCommand from './commands/custom-command';
|
|
7
|
+
import { cliProfiler, getPkgVersion } from './util/index.js';
|
|
9
8
|
|
|
10
|
-
const DISABLE_GLOBAL_CONFIG_COMMANDS = ['global-config', 'doctor', 'update', 'config'];
|
|
11
9
|
const DEFAULT_FRAMEWORK = 'react';
|
|
10
|
+
const SUPPORTED_COMMANDS = new Set(['build', 'init']);
|
|
11
|
+
|
|
12
|
+
interface CliArgs {
|
|
13
|
+
_: string[];
|
|
14
|
+
[key: string]: boolean | number | string | string[] | undefined;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const ARG_ALIASES: Record<string, string[]> = {
|
|
18
|
+
envPrefix: ['env-prefix'],
|
|
19
|
+
help: ['h'],
|
|
20
|
+
port: ['p'],
|
|
21
|
+
version: ['v'],
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const BOOLEAN_ARGS = new Set(['disable-global-config', 'h', 'help', 'v', 'version']);
|
|
25
|
+
|
|
26
|
+
function parseArgValue(value: string): number | string {
|
|
27
|
+
if (value.trim() !== '' && /^-?\d+(\.\d+)?$/.test(value)) {
|
|
28
|
+
return Number(value);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function setArgValue(args: CliArgs, key: string, value: boolean | number | string) {
|
|
35
|
+
args[key] = value;
|
|
36
|
+
|
|
37
|
+
for (const [canonicalKey, aliases] of Object.entries(ARG_ALIASES)) {
|
|
38
|
+
if (key === canonicalKey || aliases.includes(key)) {
|
|
39
|
+
args[canonicalKey] = value;
|
|
40
|
+
aliases.forEach((alias) => {
|
|
41
|
+
args[alias] = value;
|
|
42
|
+
});
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function getStringArg(args: CliArgs, key: string): string | undefined {
|
|
49
|
+
const value = args[key];
|
|
50
|
+
return typeof value === 'string' ? value : undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function getNumberArg(args: CliArgs, key: string): number | undefined {
|
|
54
|
+
const value = args[key];
|
|
55
|
+
return typeof value === 'number' ? value : undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function parseCliArgs(argv: string[]): CliArgs {
|
|
59
|
+
const args: CliArgs = {
|
|
60
|
+
_: [],
|
|
61
|
+
build: true,
|
|
62
|
+
check: true,
|
|
63
|
+
h: false,
|
|
64
|
+
help: false,
|
|
65
|
+
'inject-global-style': true,
|
|
66
|
+
v: false,
|
|
67
|
+
version: false,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
for (let index = 0; index < argv.length; index++) {
|
|
71
|
+
const item = argv[index];
|
|
72
|
+
if (!item.startsWith('-') || item === '-') {
|
|
73
|
+
args._.push(item);
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (item === '--') {
|
|
78
|
+
args._.push(...argv.slice(index + 1));
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const normalizedItem = item.replace(/^--?/, '');
|
|
83
|
+
const equalIndex = normalizedItem.indexOf('=');
|
|
84
|
+
const rawKey = equalIndex >= 0 ? normalizedItem.slice(0, equalIndex) : normalizedItem;
|
|
85
|
+
const inlineValue = equalIndex >= 0 ? normalizedItem.slice(equalIndex + 1) : undefined;
|
|
86
|
+
const isNegativeBoolean = rawKey.startsWith('no-');
|
|
87
|
+
const key = isNegativeBoolean ? rawKey.slice(3) : rawKey;
|
|
88
|
+
|
|
89
|
+
if (isNegativeBoolean) {
|
|
90
|
+
setArgValue(args, key, false);
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (inlineValue !== undefined) {
|
|
95
|
+
setArgValue(args, key, parseArgValue(inlineValue));
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (BOOLEAN_ARGS.has(key)) {
|
|
100
|
+
setArgValue(args, key, true);
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const nextItem = argv[index + 1];
|
|
105
|
+
if (nextItem && !nextItem.startsWith('-')) {
|
|
106
|
+
setArgValue(args, key, parseArgValue(nextItem));
|
|
107
|
+
index++;
|
|
108
|
+
} else {
|
|
109
|
+
setArgValue(args, key, true);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return args;
|
|
114
|
+
}
|
|
12
115
|
|
|
13
116
|
export default class CLI {
|
|
14
117
|
appPath: string;
|
|
@@ -25,58 +128,45 @@ export default class CLI {
|
|
|
25
128
|
}
|
|
26
129
|
|
|
27
130
|
async parseArgs() {
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
});
|
|
131
|
+
const totalStartMs = cliProfiler.start();
|
|
132
|
+
const parseArgsStartMs = cliProfiler.start();
|
|
133
|
+
const args = parseCliArgs(process.argv.slice(2));
|
|
134
|
+
cliProfiler.end('parse args', parseArgsStartMs);
|
|
49
135
|
const _ = args._;
|
|
50
136
|
const command = _[0];
|
|
51
137
|
if (command) {
|
|
52
138
|
const appPath = this.appPath;
|
|
53
139
|
const presetsPath = path.resolve(__dirname, 'presets');
|
|
54
140
|
const commandsPath = path.resolve(presetsPath, 'commands');
|
|
55
|
-
const platformsPath = path.resolve(presetsPath, 'platforms');
|
|
56
|
-
const commandPlugins = fs.readdirSync(commandsPath);
|
|
57
141
|
const targetPlugin = `${command}.js`;
|
|
58
142
|
|
|
143
|
+
if (!SUPPORTED_COMMANDS.has(command)) {
|
|
144
|
+
console.log('当前 CLI 仅支持 build 和 init 命令。');
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
59
148
|
// 设置环境变量
|
|
60
|
-
|
|
61
|
-
|
|
149
|
+
const env = getStringArg(args, 'env');
|
|
150
|
+
const envPrefix = getStringArg(args, 'envPrefix');
|
|
151
|
+
process.env.NODE_ENV ||= env;
|
|
152
|
+
if (process.env.NODE_ENV === 'undefined' && command === 'build') {
|
|
62
153
|
process.env.NODE_ENV = args.watch ? 'development' : 'production';
|
|
63
154
|
}
|
|
64
|
-
args.type ||= args
|
|
65
|
-
|
|
66
|
-
if (!args.type && (command === 'build' || command === 'inspect') && typeof args.plugin !== 'string') {
|
|
155
|
+
args.type ||= getStringArg(args, 't');
|
|
156
|
+
if (!args.type && command === 'build') {
|
|
67
157
|
args.type = 'weapp';
|
|
68
158
|
}
|
|
69
|
-
|
|
70
|
-
|
|
159
|
+
const type = getStringArg(args, 'type');
|
|
160
|
+
if (type) {
|
|
161
|
+
process.env.TARO_ENV = type;
|
|
71
162
|
}
|
|
72
|
-
|
|
73
|
-
process.env.TARO_ENV = 'plugin';
|
|
74
|
-
}
|
|
75
|
-
const mode = args.mode || process.env.NODE_ENV;
|
|
163
|
+
const mode = getStringArg(args, 'mode') || process.env.NODE_ENV || 'production';
|
|
76
164
|
// 这里解析 dotenv 以便于 config 解析时能获取 dotenv 配置信息
|
|
77
|
-
const
|
|
165
|
+
const dotenvStartMs = cliProfiler.start();
|
|
166
|
+
const expandEnv = dotenvParse(appPath, envPrefix, mode);
|
|
167
|
+
cliProfiler.end('dotenv parse', dotenvStartMs);
|
|
78
168
|
|
|
79
|
-
const disableGlobalConfig = !!
|
|
169
|
+
const disableGlobalConfig = !!args['disable-global-config'];
|
|
80
170
|
|
|
81
171
|
const configEnv = {
|
|
82
172
|
mode,
|
|
@@ -86,14 +176,16 @@ export default class CLI {
|
|
|
86
176
|
appPath: this.appPath,
|
|
87
177
|
disableGlobalConfig: disableGlobalConfig,
|
|
88
178
|
});
|
|
89
|
-
await config.init(configEnv);
|
|
179
|
+
await cliProfiler.measure('config init', () => config.init(configEnv));
|
|
90
180
|
|
|
181
|
+
const kernelStartMs = cliProfiler.start();
|
|
91
182
|
const kernel = new Kernel({
|
|
92
183
|
appPath,
|
|
93
184
|
presets: [path.resolve(__dirname, '.', 'presets', 'index.js')],
|
|
94
185
|
config,
|
|
95
186
|
plugins: [],
|
|
96
187
|
});
|
|
188
|
+
cliProfiler.end('kernel init', kernelStartMs);
|
|
97
189
|
kernel.optsPlugins ||= [];
|
|
98
190
|
|
|
99
191
|
// 将自定义的 变量 添加到 config.env 中,实现 definePlugin 字段定义
|
|
@@ -101,28 +193,18 @@ export default class CLI {
|
|
|
101
193
|
if (initialConfig) {
|
|
102
194
|
initialConfig.env = patchEnv(initialConfig, expandEnv);
|
|
103
195
|
}
|
|
104
|
-
|
|
105
|
-
// 针对不同的内置命令注册对应的命令插件
|
|
106
|
-
kernel.optsPlugins.push(path.resolve(commandsPath, targetPlugin));
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// 把内置命令插件传递给 kernel,可以暴露给其他插件使用
|
|
196
|
+
kernel.optsPlugins.push(path.resolve(commandsPath, targetPlugin));
|
|
110
197
|
kernel.cliCommandsPath = commandsPath;
|
|
111
|
-
kernel.cliCommands =
|
|
112
|
-
.filter((commandFileName) => /^[\w-]+(\.[\w-]+)*\.js$/.test(commandFileName))
|
|
113
|
-
.map((fileName) => fileName.replace(/\.js$/, ''));
|
|
198
|
+
kernel.cliCommands = Array.from(SUPPORTED_COMMANDS);
|
|
114
199
|
|
|
115
200
|
switch (command) {
|
|
116
|
-
case 'inspect':
|
|
117
201
|
case 'build': {
|
|
118
|
-
let
|
|
119
|
-
let platform = args.type;
|
|
120
|
-
const { publicPath, bundleOutput, sourcemapOutput, sourceMapUrl, sourcemapSourcesRoot, assetsDest } = args;
|
|
202
|
+
let platform = getStringArg(args, 'type');
|
|
121
203
|
|
|
122
204
|
// 针对不同的内置平台注册对应的端平台插件
|
|
123
205
|
switch (platform) {
|
|
124
206
|
case 'weapp':
|
|
125
|
-
kernel.optsPlugins.push(
|
|
207
|
+
kernel.optsPlugins.push(path.resolve(__dirname, 'platform-weapp'));
|
|
126
208
|
break;
|
|
127
209
|
default: {
|
|
128
210
|
if (platform) {
|
|
@@ -139,78 +221,56 @@ export default class CLI {
|
|
|
139
221
|
console.log('当前 Fork 仅支持 React 框架。');
|
|
140
222
|
return;
|
|
141
223
|
}
|
|
142
|
-
kernel.optsPlugins.push('@spcsn/taro-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
// Note: 是否把 Taro 组件编译为原生自定义组件
|
|
168
|
-
isBuildNativeComp: _[1] === 'native-components',
|
|
169
|
-
// Note: 新的混合编译模式,支持把组件单独编译为原生组件
|
|
170
|
-
newBlended: Boolean(args['new-blended']),
|
|
171
|
-
// Note: 是否禁用编译
|
|
172
|
-
withoutBuild: !args.build,
|
|
173
|
-
noInjectGlobalStyle: !args['inject-global-style'],
|
|
174
|
-
noCheck: !args.check,
|
|
175
|
-
port: args.port,
|
|
176
|
-
env: args.env,
|
|
177
|
-
deviceType: args.platform,
|
|
178
|
-
resetCache: !!args.resetCache,
|
|
179
|
-
publicPath,
|
|
180
|
-
bundleOutput,
|
|
181
|
-
sourcemapOutput,
|
|
182
|
-
sourceMapUrl,
|
|
183
|
-
sourcemapSourcesRoot,
|
|
184
|
-
assetsDest,
|
|
185
|
-
qr: !!args.qr,
|
|
186
|
-
blended: Boolean(args.blended),
|
|
187
|
-
h: args.h,
|
|
188
|
-
});
|
|
224
|
+
kernel.optsPlugins.push(require.resolve('@spcsn/taro-mini-runner/framework-react'));
|
|
225
|
+
await cliProfiler.measure('build command', () =>
|
|
226
|
+
customCommand(command, kernel, {
|
|
227
|
+
args,
|
|
228
|
+
_,
|
|
229
|
+
platform,
|
|
230
|
+
isWatch: Boolean(args.watch),
|
|
231
|
+
// Note: 是否把 Taro 组件编译为原生自定义组件
|
|
232
|
+
isBuildNativeComp: _[1] === 'native-components',
|
|
233
|
+
// Note: 新的混合编译模式,支持把组件单独编译为原生组件
|
|
234
|
+
newBlended: Boolean(args['new-blended']),
|
|
235
|
+
// Note: 是否禁用编译
|
|
236
|
+
withoutBuild: !args.build,
|
|
237
|
+
noInjectGlobalStyle: !args['inject-global-style'],
|
|
238
|
+
noCheck: !args.check,
|
|
239
|
+
port: getNumberArg(args, 'port'),
|
|
240
|
+
env,
|
|
241
|
+
deviceType: getStringArg(args, 'platform'),
|
|
242
|
+
qr: !!args.qr,
|
|
243
|
+
blended: Boolean(args.blended),
|
|
244
|
+
h: args.h,
|
|
245
|
+
}),
|
|
246
|
+
);
|
|
247
|
+
cliProfiler.end('total', totalStartMs);
|
|
248
|
+
cliProfiler.print();
|
|
189
249
|
break;
|
|
190
250
|
}
|
|
191
251
|
case 'init': {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
252
|
+
await cliProfiler.measure('init command', () =>
|
|
253
|
+
customCommand(command, kernel, {
|
|
254
|
+
_,
|
|
255
|
+
appPath,
|
|
256
|
+
projectName: _[1] || getStringArg(args, 'name'),
|
|
257
|
+
description: getStringArg(args, 'description'),
|
|
258
|
+
typescript: args.typescript,
|
|
259
|
+
framework: getStringArg(args, 'framework'),
|
|
260
|
+
compiler: getStringArg(args, 'compiler'),
|
|
261
|
+
npm: getStringArg(args, 'npm'),
|
|
262
|
+
templateSource: getStringArg(args, 'template-source'),
|
|
263
|
+
clone: !!args.clone,
|
|
264
|
+
template: getStringArg(args, 'template'),
|
|
265
|
+
css: getStringArg(args, 'css'),
|
|
266
|
+
autoInstall: args.autoInstall,
|
|
267
|
+
h: args.h,
|
|
268
|
+
}),
|
|
269
|
+
);
|
|
270
|
+
cliProfiler.end('total', totalStartMs);
|
|
271
|
+
cliProfiler.print();
|
|
209
272
|
break;
|
|
210
273
|
}
|
|
211
|
-
default:
|
|
212
|
-
customCommand(command, kernel, args);
|
|
213
|
-
break;
|
|
214
274
|
}
|
|
215
275
|
} else {
|
|
216
276
|
if (args.h) {
|
|
@@ -221,15 +281,8 @@ export default class CLI {
|
|
|
221
281
|
console.log(' -h, --help output usage information');
|
|
222
282
|
console.log();
|
|
223
283
|
console.log('Commands:');
|
|
224
|
-
console.log(' init [projectName] Init a project with default
|
|
225
|
-
console.log('
|
|
226
|
-
console.log(' create Create page for project');
|
|
227
|
-
console.log(' build Build a project with options');
|
|
228
|
-
console.log(' update Update packages of taro');
|
|
229
|
-
console.log(' info Diagnostics Taro env info');
|
|
230
|
-
console.log(' doctor Diagnose taro project');
|
|
231
|
-
console.log(' inspect Inspect the webpack config');
|
|
232
|
-
console.log(' help [cmd] display help for [cmd]');
|
|
284
|
+
console.log(' init [projectName] Init a project with default template');
|
|
285
|
+
console.log(' build Build a WeApp project');
|
|
233
286
|
} else if (args.v) {
|
|
234
287
|
console.log(getPkgVersion());
|
|
235
288
|
}
|
|
@@ -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') return Promise.resolve();
|
|
5
|
+
|
|
6
|
+
const options: any = {};
|
|
7
|
+
const excludeKeys = ['_', 'version', 'v', 'help', 'h', 'disable-global-config'];
|
|
8
|
+
Object.keys(args).forEach((key) => {
|
|
9
|
+
if (!excludeKeys.includes(key)) {
|
|
10
|
+
options[key] = args[key];
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
return kernel.run({
|
|
15
|
+
name: command,
|
|
16
|
+
opts: {
|
|
17
|
+
_: args._,
|
|
18
|
+
options,
|
|
19
|
+
isHelp: args.h,
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
}
|
|
@@ -2,7 +2,6 @@ import * as path from 'node:path';
|
|
|
2
2
|
|
|
3
3
|
import { chalk, fs } from '@spcsn/taro-helper';
|
|
4
4
|
import * as AdmZip from 'adm-zip';
|
|
5
|
-
import axios from 'axios';
|
|
6
5
|
import * as download from 'download-git-repo';
|
|
7
6
|
import ora from 'ora';
|
|
8
7
|
|
|
@@ -19,7 +18,7 @@ export interface ITemplates {
|
|
|
19
18
|
|
|
20
19
|
const TEMP_DOWNLOAD_FOLDER = 'taro-temp';
|
|
21
20
|
|
|
22
|
-
export default function fetchTemplate(
|
|
21
|
+
export default async function fetchTemplate(
|
|
23
22
|
templateSource: string,
|
|
24
23
|
templateRootPath: string,
|
|
25
24
|
clone?: boolean,
|
|
@@ -27,13 +26,13 @@ export default function fetchTemplate(
|
|
|
27
26
|
const type = getTemplateSourceType(templateSource);
|
|
28
27
|
const tempPath = path.join(templateRootPath, TEMP_DOWNLOAD_FOLDER);
|
|
29
28
|
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
29
|
|
|
30
|
+
// 下载文件的缓存目录
|
|
31
|
+
if (fs.existsSync(tempPath)) await fs.remove(tempPath);
|
|
32
|
+
await fs.mkdirp(templateRootPath);
|
|
33
|
+
await fs.mkdir(tempPath);
|
|
34
|
+
|
|
35
|
+
return new Promise<void>((resolve) => {
|
|
37
36
|
const spinner = ora(`正在从 ${templateSource} 拉取远程模板...`).start();
|
|
38
37
|
|
|
39
38
|
if (type === 'git') {
|
|
@@ -55,33 +54,29 @@ export default function fetchTemplate(
|
|
|
55
54
|
name = 'from-remote-url';
|
|
56
55
|
const zipPath = path.join(tempPath, name + '.zip');
|
|
57
56
|
const unZipPath = path.join(tempPath, name);
|
|
58
|
-
|
|
59
|
-
.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
});
|
|
82
|
-
ws.on('error', (error) => {
|
|
83
|
-
throw error;
|
|
84
|
-
});
|
|
57
|
+
fetch(templateSource)
|
|
58
|
+
.then(async (response) => {
|
|
59
|
+
if (!response.ok) {
|
|
60
|
+
throw new Error(`HTTP ${response.status} ${response.statusText}`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
await fs.writeFile(zipPath, Buffer.from(await response.arrayBuffer()));
|
|
64
|
+
const zip = new AdmZip(zipPath);
|
|
65
|
+
zip.extractAllTo(unZipPath, true);
|
|
66
|
+
const files = readDirWithFileTypes(unZipPath).filter(
|
|
67
|
+
(file) => !file.name.startsWith('.') && file.isDirectory && file.name !== '__MACOSX',
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
if (files.length !== 1) {
|
|
71
|
+
spinner.color = 'red';
|
|
72
|
+
spinner.fail(chalk.red(`拉取远程模板仓库失败!\n${new Error('远程模板源组织格式错误')}`));
|
|
73
|
+
return resolve();
|
|
74
|
+
}
|
|
75
|
+
name = path.join(name, files[0].name);
|
|
76
|
+
|
|
77
|
+
spinner.color = 'green';
|
|
78
|
+
spinner.succeed(`${chalk.grey('拉取远程模板仓库成功!')}`);
|
|
79
|
+
resolve();
|
|
85
80
|
})
|
|
86
81
|
.catch(async (error) => {
|
|
87
82
|
spinner.color = 'red';
|