@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
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { IPluginContext } from '@spcsn/taro-service';
|
|
2
|
+
|
|
3
|
+
export default (ctx: IPluginContext) => {
|
|
4
|
+
ctx.registerCommand({
|
|
5
|
+
name: 'init',
|
|
6
|
+
optionsMap: {
|
|
7
|
+
'--name [name]': '项目名称',
|
|
8
|
+
'--description [description]': '项目介绍',
|
|
9
|
+
'--typescript': '使用TypeScript',
|
|
10
|
+
'--build-es5': '是否需要编译为es5',
|
|
11
|
+
'--npm [npm]': '包管理工具',
|
|
12
|
+
'--template-source [templateSource]': '项目模板源',
|
|
13
|
+
'--clone [clone]': '拉取远程模板时使用git clone',
|
|
14
|
+
'--template [template]': '项目模板',
|
|
15
|
+
'--css [css]': 'CSS预处理器(sass/less/stylus/none)',
|
|
16
|
+
'--autoInstall': '自动安装依赖',
|
|
17
|
+
'-h, --help': 'output usage information',
|
|
18
|
+
},
|
|
19
|
+
async fn(opts) {
|
|
20
|
+
// init project
|
|
21
|
+
const { appPath } = ctx.paths;
|
|
22
|
+
const {
|
|
23
|
+
projectName,
|
|
24
|
+
templateSource,
|
|
25
|
+
clone,
|
|
26
|
+
template,
|
|
27
|
+
description,
|
|
28
|
+
typescript,
|
|
29
|
+
buildEs5,
|
|
30
|
+
css,
|
|
31
|
+
npm,
|
|
32
|
+
framework,
|
|
33
|
+
compiler,
|
|
34
|
+
hideDefaultTemplate,
|
|
35
|
+
sourceRoot,
|
|
36
|
+
autoInstall,
|
|
37
|
+
ask,
|
|
38
|
+
} = opts.options;
|
|
39
|
+
|
|
40
|
+
const Project = require('../../create/project').default;
|
|
41
|
+
const project = new Project({
|
|
42
|
+
sourceRoot,
|
|
43
|
+
projectName,
|
|
44
|
+
projectDir: appPath,
|
|
45
|
+
npm,
|
|
46
|
+
templateSource,
|
|
47
|
+
clone,
|
|
48
|
+
template,
|
|
49
|
+
description,
|
|
50
|
+
typescript,
|
|
51
|
+
buildEs5,
|
|
52
|
+
framework,
|
|
53
|
+
compiler,
|
|
54
|
+
hideDefaultTemplate,
|
|
55
|
+
autoInstall,
|
|
56
|
+
css,
|
|
57
|
+
ask,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
project.create();
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
};
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import { ENTRY, OUTPUT_DIR, resolveScriptPath, SOURCE_DIR } from '@spcsn/taro-helper';
|
|
4
|
+
import { getPlatformType } from '@spcsn/taro-shared';
|
|
5
|
+
|
|
6
|
+
import * as hooks from '../constant';
|
|
7
|
+
|
|
8
|
+
import type { IPluginContext } from '@spcsn/taro-service';
|
|
9
|
+
|
|
10
|
+
export default (ctx: IPluginContext) => {
|
|
11
|
+
ctx.registerCommand({
|
|
12
|
+
name: 'inspect',
|
|
13
|
+
optionsMap: {
|
|
14
|
+
'-t, --type [typeName]': 'Build type, weapp',
|
|
15
|
+
'-o, --output [outputPath]': 'output config to outputPath',
|
|
16
|
+
},
|
|
17
|
+
synopsisList: [
|
|
18
|
+
'taro inspect --type weapp',
|
|
19
|
+
'taro inspect --type weapp --output inspect.config.js',
|
|
20
|
+
'taro inspect --type weapp plugins',
|
|
21
|
+
'taro inspect --type weapp module.rules.0',
|
|
22
|
+
],
|
|
23
|
+
async fn({ _, options }) {
|
|
24
|
+
const { fs, chalk } = ctx.helper;
|
|
25
|
+
const platform = options.type || options.t;
|
|
26
|
+
|
|
27
|
+
verifyIsTaroProject(ctx);
|
|
28
|
+
verifyPlatform(platform, chalk);
|
|
29
|
+
|
|
30
|
+
const configName = ctx.platforms.get(platform)?.useConfigName || '';
|
|
31
|
+
process.env.TARO_ENV = platform;
|
|
32
|
+
process.env.TARO_PLATFORM = getPlatformType(platform, configName);
|
|
33
|
+
|
|
34
|
+
let config = getConfig(ctx, platform);
|
|
35
|
+
config = {
|
|
36
|
+
...config,
|
|
37
|
+
...config[configName],
|
|
38
|
+
};
|
|
39
|
+
delete config.mini;
|
|
40
|
+
delete config.h5;
|
|
41
|
+
|
|
42
|
+
const isProduction = process.env.NODE_ENV === 'production';
|
|
43
|
+
const outputPath = options.output || options.o;
|
|
44
|
+
const mode = outputPath ? 'output' : 'console';
|
|
45
|
+
const extractPath = _[1];
|
|
46
|
+
|
|
47
|
+
await ctx.applyPlugins({
|
|
48
|
+
name: platform,
|
|
49
|
+
opts: {
|
|
50
|
+
config: {
|
|
51
|
+
...config,
|
|
52
|
+
isWatch: !isProduction,
|
|
53
|
+
mode: isProduction ? 'production' : 'development',
|
|
54
|
+
async modifyWebpackChain(chain, webpack, data) {
|
|
55
|
+
await ctx.applyPlugins({
|
|
56
|
+
name: hooks.MODIFY_WEBPACK_CHAIN,
|
|
57
|
+
initialVal: chain,
|
|
58
|
+
opts: {
|
|
59
|
+
chain,
|
|
60
|
+
webpack,
|
|
61
|
+
data,
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
onWebpackChainReady(chain) {
|
|
66
|
+
const webpackConfig = chain.toConfig();
|
|
67
|
+
const { toString } = chain.constructor;
|
|
68
|
+
const config = extractConfig(webpackConfig, extractPath);
|
|
69
|
+
const res = toString(config);
|
|
70
|
+
|
|
71
|
+
if (mode === 'console') {
|
|
72
|
+
const highlight = require('cli-highlight').default;
|
|
73
|
+
console.info(highlight(res, { language: 'js' }));
|
|
74
|
+
} else if (mode === 'output' && outputPath) {
|
|
75
|
+
fs.writeFileSync(outputPath, res);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
process.exit(0);
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/** 是否 Taro 项目根路径 */
|
|
88
|
+
function verifyIsTaroProject(ctx: IPluginContext) {
|
|
89
|
+
const { fs, chalk, PROJECT_CONFIG } = ctx.helper;
|
|
90
|
+
const { configPath } = ctx.paths;
|
|
91
|
+
|
|
92
|
+
if (!configPath || !fs.existsSync(configPath)) {
|
|
93
|
+
console.log(chalk.red(`找不到项目配置文件${PROJECT_CONFIG},请确定当前目录是 Taro 项目根目录!`));
|
|
94
|
+
process.exit(1);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** 检查平台类型 */
|
|
99
|
+
function verifyPlatform(platform, chalk) {
|
|
100
|
+
if (typeof platform !== 'string') {
|
|
101
|
+
console.log(chalk.red('请传入正确的编译类型!'));
|
|
102
|
+
process.exit(0);
|
|
103
|
+
}
|
|
104
|
+
if (platform !== 'weapp') {
|
|
105
|
+
console.log(chalk.red('当前 Fork 仅支持 weapp 平台检查。'));
|
|
106
|
+
process.exit(0);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** 整理 config */
|
|
111
|
+
function getConfig(ctx: IPluginContext, platform: string) {
|
|
112
|
+
const { initialConfig } = ctx;
|
|
113
|
+
const sourceDirName = initialConfig.sourceRoot || SOURCE_DIR;
|
|
114
|
+
const outputDirName = initialConfig.outputRoot || OUTPUT_DIR;
|
|
115
|
+
const sourceDir = path.join(ctx.appPath, sourceDirName);
|
|
116
|
+
const entryFilePath = resolveScriptPath(path.join(sourceDir, ENTRY));
|
|
117
|
+
|
|
118
|
+
const entry = {
|
|
119
|
+
[ENTRY]: [entryFilePath],
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
...initialConfig,
|
|
124
|
+
entry,
|
|
125
|
+
sourceRoot: sourceDirName,
|
|
126
|
+
outputRoot: outputDirName,
|
|
127
|
+
platform,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** 按路径取出 webpackConfig 内的对应值 */
|
|
132
|
+
function extractConfig(webpackConfig, extractPath: string | undefined) {
|
|
133
|
+
if (!extractPath) return webpackConfig;
|
|
134
|
+
|
|
135
|
+
const list = extractPath.split('.');
|
|
136
|
+
return list.reduce((config, current) => config[current], webpackConfig);
|
|
137
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import * as inquirer from 'inquirer';
|
|
4
|
+
import getLatestVersion from 'latest-version';
|
|
5
|
+
import ora from 'ora';
|
|
6
|
+
import * as semver from 'semver';
|
|
7
|
+
|
|
8
|
+
import packagesManagement from '../../config/packagesManagement';
|
|
9
|
+
import { execCommand, getPkgItemByKey } from '../../util';
|
|
10
|
+
|
|
11
|
+
import type { IPluginContext } from '@spcsn/taro-service';
|
|
12
|
+
|
|
13
|
+
export default (ctx: IPluginContext) => {
|
|
14
|
+
ctx.registerCommand({
|
|
15
|
+
name: 'update',
|
|
16
|
+
synopsisList: ['taro update self [version]', 'taro update project [version]'],
|
|
17
|
+
optionsMap: {
|
|
18
|
+
'--npm [npm]': '包管理工具',
|
|
19
|
+
'-h, --help': 'output usage information',
|
|
20
|
+
},
|
|
21
|
+
async fn({ _, options }) {
|
|
22
|
+
const { npm } = options;
|
|
23
|
+
const [, updateType, version] = _ as [string, ('self' | 'project')?, string?];
|
|
24
|
+
const { appPath, configPath } = ctx.paths;
|
|
25
|
+
const { chalk, fs, PROJECT_CONFIG, UPDATE_PACKAGE_LIST } = ctx.helper;
|
|
26
|
+
|
|
27
|
+
const pkgPath = path.join(appPath, 'package.json');
|
|
28
|
+
const pkgName = getPkgItemByKey('name');
|
|
29
|
+
const conf = {
|
|
30
|
+
npm: null,
|
|
31
|
+
};
|
|
32
|
+
const prompts: Record<string, unknown>[] = [];
|
|
33
|
+
|
|
34
|
+
async function getTargetVersion() {
|
|
35
|
+
let targetTaroVersion;
|
|
36
|
+
|
|
37
|
+
if (version) {
|
|
38
|
+
targetTaroVersion = semver.clean(version);
|
|
39
|
+
} else {
|
|
40
|
+
try {
|
|
41
|
+
targetTaroVersion = await getLatestVersion(pkgName, {
|
|
42
|
+
version: 'latest',
|
|
43
|
+
});
|
|
44
|
+
} catch (e) {
|
|
45
|
+
targetTaroVersion = await getLatestVersion(pkgName);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (!semver.valid(targetTaroVersion)) {
|
|
49
|
+
console.log(chalk.red('命令错误:无效的 version ~'));
|
|
50
|
+
throw Error('无效的 version!');
|
|
51
|
+
}
|
|
52
|
+
return targetTaroVersion;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function execUpdate(command: string, version: string, isSelf = false) {
|
|
56
|
+
const updateTarget = isSelf ? ' CLI ' : ' Taro 项目依赖';
|
|
57
|
+
const spinString = `正在更新${updateTarget}到 v${version} ...`;
|
|
58
|
+
const spinner = ora(spinString).start();
|
|
59
|
+
execCommand({
|
|
60
|
+
command,
|
|
61
|
+
successCallback(data) {
|
|
62
|
+
spinner.stop();
|
|
63
|
+
console.log(data.replace(/\n$/, ''));
|
|
64
|
+
},
|
|
65
|
+
failCallback(data) {
|
|
66
|
+
spinner.stop();
|
|
67
|
+
spinner.warn(data.replace(/\n$/, ''));
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** 更新全局的 Taro CLI */
|
|
73
|
+
async function updateSelf() {
|
|
74
|
+
const spinner = ora('正在获取最新版本信息...').start();
|
|
75
|
+
const targetTaroVersion = await getTargetVersion();
|
|
76
|
+
spinner.stop();
|
|
77
|
+
console.log(chalk.green(`Taro 最新版本:${targetTaroVersion}\n`));
|
|
78
|
+
|
|
79
|
+
askNpm(conf, prompts);
|
|
80
|
+
const answers = npm ? { npm } : await inquirer.prompt(prompts);
|
|
81
|
+
|
|
82
|
+
const command = `${packagesManagement[answers.npm].globalCommand}@${targetTaroVersion}`;
|
|
83
|
+
execUpdate(command, targetTaroVersion, true);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** 更新当前项目中的 Taro 相关依赖 */
|
|
87
|
+
async function updateProject() {
|
|
88
|
+
if (!configPath || !fs.existsSync(configPath)) {
|
|
89
|
+
console.log(chalk.red(`找不到项目配置文件 ${PROJECT_CONFIG},请确定当前目录是 Taro 项目根目录!`));
|
|
90
|
+
process.exit(1);
|
|
91
|
+
}
|
|
92
|
+
const packageMap = require(pkgPath);
|
|
93
|
+
|
|
94
|
+
const spinner = ora('正在获取最新版本信息...').start();
|
|
95
|
+
|
|
96
|
+
const version = await getTargetVersion();
|
|
97
|
+
|
|
98
|
+
spinner.stop();
|
|
99
|
+
|
|
100
|
+
const oldVersion = packageMap.dependencies['@spcsn/taro'];
|
|
101
|
+
// 更新 @spcsn/* 底座版本
|
|
102
|
+
Object.keys(packageMap.dependencies || {}).forEach((key) => {
|
|
103
|
+
if (UPDATE_PACKAGE_LIST.indexOf(key) !== -1) {
|
|
104
|
+
packageMap.dependencies[key] = version;
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
Object.keys(packageMap.devDependencies || {}).forEach((key) => {
|
|
108
|
+
if (UPDATE_PACKAGE_LIST.indexOf(key) !== -1) {
|
|
109
|
+
packageMap.devDependencies[key] = version;
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// 写入package.json
|
|
114
|
+
try {
|
|
115
|
+
await fs.writeJson(pkgPath, packageMap, { spaces: '\t' });
|
|
116
|
+
console.log(
|
|
117
|
+
chalk.green(`项目当前 Taro 版本:${oldVersion},Taro 最新版本:${version},更新项目 package.json 成功!`),
|
|
118
|
+
);
|
|
119
|
+
console.log();
|
|
120
|
+
} catch (err) {
|
|
121
|
+
console.error(err);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
askNpm(conf, prompts);
|
|
125
|
+
const answers = npm ? { npm } : await inquirer.prompt(prompts);
|
|
126
|
+
|
|
127
|
+
const command = packagesManagement[answers.npm].command;
|
|
128
|
+
|
|
129
|
+
execUpdate(command, version);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function askNpm(conf, prompts) {
|
|
133
|
+
const packages = [
|
|
134
|
+
{
|
|
135
|
+
name: 'yarn',
|
|
136
|
+
value: 'yarn',
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
name: 'pnpm',
|
|
140
|
+
value: 'pnpm',
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
name: 'npm',
|
|
144
|
+
value: 'npm',
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
name: 'cnpm',
|
|
148
|
+
value: 'cnpm',
|
|
149
|
+
},
|
|
150
|
+
];
|
|
151
|
+
|
|
152
|
+
if ((typeof conf.npm as string | undefined) !== 'string') {
|
|
153
|
+
prompts.push({
|
|
154
|
+
type: 'list',
|
|
155
|
+
name: 'npm',
|
|
156
|
+
message: '请选择包管理工具',
|
|
157
|
+
choices: packages,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (updateType === 'self') return updateSelf();
|
|
163
|
+
|
|
164
|
+
if (updateType === 'project') return updateProject();
|
|
165
|
+
|
|
166
|
+
console.log(chalk.red('命令错误:'));
|
|
167
|
+
console.log(
|
|
168
|
+
`${chalk.green('taro update self [version]')} 更新 Taro 开发工具 taro-cli 到指定版本或 Taro3 的最新版本`,
|
|
169
|
+
);
|
|
170
|
+
console.log(
|
|
171
|
+
`${chalk.green('taro update project [version]')} 更新项目所有 Taro 相关依赖到指定版本或 Taro3 的最新版本`,
|
|
172
|
+
);
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export const MODIFY_APP_CONFIG = 'modifyAppConfig';
|
|
2
|
+
export const MODIFY_WEBPACK_CHAIN = 'modifyWebpackChain';
|
|
3
|
+
export const MODIFY_VITE_CONFIG = 'modifyViteConfig';
|
|
4
|
+
export const MODIFY_BUILD_ASSETS = 'modifyBuildAssets';
|
|
5
|
+
export const MODIFY_MINI_CONFIGS = 'modifyMiniConfigs';
|
|
6
|
+
export const MODIFY_COMPONENT_CONFIG = 'modifyComponentConfig';
|
|
7
|
+
export const MODIFY_RUNNER_OPTS = 'modifyRunnerOpts';
|
|
8
|
+
export const ON_COMPILER_MAKE = 'onCompilerMake';
|
|
9
|
+
export const ON_PARSE_CREATE_ELEMENT = 'onParseCreateElement';
|
|
10
|
+
export const ON_BUILD_START = 'onBuildStart';
|
|
11
|
+
export const ON_BUILD_FINISH = 'onBuildFinish';
|
|
12
|
+
export const ON_BUILD_COMPLETE = 'onBuildComplete';
|
|
13
|
+
|
|
14
|
+
export const MODIFY_CREATE_TEMPLATE = 'modifyCreateTemplate';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './hooks';
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { getPkgVersion } from '../../util';
|
|
2
|
+
|
|
3
|
+
import type { IPluginContext } from '@spcsn/taro-service';
|
|
4
|
+
|
|
5
|
+
export default (ctx: IPluginContext) => {
|
|
6
|
+
ctx.registerMethod('generateFrameworkInfo', () => {
|
|
7
|
+
const { getInstalledNpmPkgVersion, processTypeEnum, printLog, chalk } = ctx.helper;
|
|
8
|
+
const { nodeModulesPath } = ctx.paths;
|
|
9
|
+
const { date, outputRoot } = ctx.initialConfig;
|
|
10
|
+
const frameworkInfoFileName = '.frameworkinfo';
|
|
11
|
+
const frameworkName = '@spcsn/taro-runtime';
|
|
12
|
+
const frameworkVersion = getInstalledNpmPkgVersion(frameworkName, nodeModulesPath);
|
|
13
|
+
|
|
14
|
+
if (frameworkVersion) {
|
|
15
|
+
const frameworkinfo = {
|
|
16
|
+
toolName: 'Taro',
|
|
17
|
+
toolCliVersion: getPkgVersion(),
|
|
18
|
+
toolFrameworkVersion: frameworkVersion,
|
|
19
|
+
createTime: date ? new Date(date).getTime() : Date.now(),
|
|
20
|
+
};
|
|
21
|
+
ctx.writeFileToDist({
|
|
22
|
+
filePath: frameworkInfoFileName,
|
|
23
|
+
content: JSON.stringify(frameworkinfo, null, 2),
|
|
24
|
+
});
|
|
25
|
+
printLog(processTypeEnum.GENERATE, '框架信息', `${outputRoot}/${frameworkInfoFileName}`);
|
|
26
|
+
} else {
|
|
27
|
+
printLog(processTypeEnum.WARNING, '依赖安装', chalk.red(`项目依赖 ${frameworkName} 未安装,或安装有误!`));
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import type { IPluginContext } from '@spcsn/taro-service';
|
|
4
|
+
|
|
5
|
+
export default (ctx: IPluginContext) => {
|
|
6
|
+
ctx.registerMethod('generateProjectConfig', ({ srcConfigName, distConfigName }) => {
|
|
7
|
+
// 混合模式不需要生成项目配置
|
|
8
|
+
const { blended, newBlended } = ctx.runOpts;
|
|
9
|
+
if (blended || newBlended) return;
|
|
10
|
+
|
|
11
|
+
const { appPath, sourcePath, outputPath } = ctx.paths;
|
|
12
|
+
const { printLog, processTypeEnum, fs } = ctx.helper;
|
|
13
|
+
// 生成 project.config.json
|
|
14
|
+
const projectConfigFileName = srcConfigName;
|
|
15
|
+
let projectConfigPath = path.join(appPath, projectConfigFileName);
|
|
16
|
+
if (!fs.existsSync(projectConfigPath)) {
|
|
17
|
+
// 若项目根目录不存在对应平台的 projectConfig 文件,则尝试从源代码目录查找
|
|
18
|
+
projectConfigPath = path.join(sourcePath, projectConfigFileName);
|
|
19
|
+
if (!fs.existsSync(projectConfigPath)) return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const origProjectConfig = fs.readJSONSync(projectConfigPath);
|
|
23
|
+
// 优先从环境变量中获取 appid, 以应对多环境appid不同的情况
|
|
24
|
+
origProjectConfig.appid = process.env.TARO_APP_ID || origProjectConfig.appid;
|
|
25
|
+
|
|
26
|
+
// compileType 是 plugin 时不修改 miniprogramRoot 字段
|
|
27
|
+
let distProjectConfig = origProjectConfig;
|
|
28
|
+
if (origProjectConfig.compileType !== 'plugin') {
|
|
29
|
+
distProjectConfig = Object.assign({}, origProjectConfig, { miniprogramRoot: './' });
|
|
30
|
+
}
|
|
31
|
+
ctx.writeFileToDist({
|
|
32
|
+
filePath: distConfigName,
|
|
33
|
+
content: JSON.stringify(distProjectConfig, null, 2),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
if (ctx.initialConfig.logger?.quiet === false) {
|
|
37
|
+
printLog(processTypeEnum.REMIND, 'appid', `${origProjectConfig.appid}`);
|
|
38
|
+
printLog(processTypeEnum.GENERATE, '工具配置', `${outputPath}/${distConfigName}`);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import type { IPluginContext } from '@spcsn/taro-service';
|
|
4
|
+
|
|
5
|
+
export default (ctx: IPluginContext) => {
|
|
6
|
+
ctx.registerMethod('writeFileToDist', ({ filePath, content }) => {
|
|
7
|
+
const { outputPath } = ctx.paths;
|
|
8
|
+
const { printLog, processTypeEnum, fs } = ctx.helper;
|
|
9
|
+
if (path.isAbsolute(filePath)) {
|
|
10
|
+
printLog(processTypeEnum.ERROR, 'ctx.writeFileToDist 不能接受绝对路径');
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const absFilePath = path.join(outputPath, filePath);
|
|
14
|
+
fs.ensureDirSync(path.dirname(absFilePath));
|
|
15
|
+
fs.writeFileSync(absFilePath, content);
|
|
16
|
+
});
|
|
17
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import * as hooks from '../constant';
|
|
2
|
+
|
|
3
|
+
import type { IPluginContext } from '@spcsn/taro-service';
|
|
4
|
+
|
|
5
|
+
export default (ctx: IPluginContext) => {
|
|
6
|
+
[
|
|
7
|
+
hooks.MODIFY_APP_CONFIG,
|
|
8
|
+
hooks.MODIFY_WEBPACK_CHAIN,
|
|
9
|
+
hooks.MODIFY_VITE_CONFIG,
|
|
10
|
+
hooks.MODIFY_BUILD_ASSETS,
|
|
11
|
+
hooks.MODIFY_MINI_CONFIGS,
|
|
12
|
+
hooks.MODIFY_COMPONENT_CONFIG,
|
|
13
|
+
hooks.ON_COMPILER_MAKE,
|
|
14
|
+
hooks.ON_PARSE_CREATE_ELEMENT,
|
|
15
|
+
hooks.ON_BUILD_START,
|
|
16
|
+
hooks.ON_BUILD_FINISH,
|
|
17
|
+
hooks.ON_BUILD_COMPLETE,
|
|
18
|
+
hooks.MODIFY_RUNNER_OPTS,
|
|
19
|
+
].forEach((methodName) => {
|
|
20
|
+
ctx.registerMethod(methodName);
|
|
21
|
+
});
|
|
22
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
|
|
3
|
+
export default () => {
|
|
4
|
+
return {
|
|
5
|
+
plugins: [
|
|
6
|
+
// hooks
|
|
7
|
+
path.resolve(__dirname, 'hooks', 'build.js'),
|
|
8
|
+
path.resolve(__dirname, 'hooks', 'create.js'),
|
|
9
|
+
// 兼容其他平台小程序插件
|
|
10
|
+
path.resolve(__dirname, 'files', 'writeFileToDist.js'),
|
|
11
|
+
path.resolve(__dirname, 'files', 'generateProjectConfig.js'),
|
|
12
|
+
path.resolve(__dirname, 'files', 'generateFrameworkInfo.js'),
|
|
13
|
+
],
|
|
14
|
+
};
|
|
15
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import { fs } from '@spcsn/taro-helper';
|
|
4
|
+
import { getPlatformType } from '@spcsn/taro-shared';
|
|
5
|
+
|
|
6
|
+
import type { IPluginContext } from '@spcsn/taro-service';
|
|
7
|
+
|
|
8
|
+
const configName = 'mini';
|
|
9
|
+
export default (ctx: IPluginContext) => {
|
|
10
|
+
ctx.registerPlatform({
|
|
11
|
+
name: 'plugin',
|
|
12
|
+
useConfigName: configName,
|
|
13
|
+
async fn({ config }) {
|
|
14
|
+
const { options, _ } = ctx.runOpts;
|
|
15
|
+
const { chalk, PLATFORMS } = ctx.helper;
|
|
16
|
+
const { WEAPP } = PLATFORMS;
|
|
17
|
+
const typeMap = {
|
|
18
|
+
[WEAPP]: '微信',
|
|
19
|
+
};
|
|
20
|
+
const { plugin, isWatch } = options;
|
|
21
|
+
if (plugin !== WEAPP) {
|
|
22
|
+
console.log(chalk.red('当前 Fork 仅支持微信小程序插件编译!'));
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
console.log(chalk.green(`开始编译${typeMap[plugin]}小程序插件`));
|
|
26
|
+
async function buildPlugin(platform) {
|
|
27
|
+
process.env.TARO_ENV = platform;
|
|
28
|
+
process.env.TARO_PLATFORM = getPlatformType(platform, configName);
|
|
29
|
+
await ctx.applyPlugins({
|
|
30
|
+
name: 'build',
|
|
31
|
+
opts: {
|
|
32
|
+
config: {
|
|
33
|
+
...config,
|
|
34
|
+
isBuildPlugin: true,
|
|
35
|
+
isWatch,
|
|
36
|
+
outputRoot: `${config.outputRoot}/plugin`,
|
|
37
|
+
platform,
|
|
38
|
+
},
|
|
39
|
+
options: Object.assign({}, options, {
|
|
40
|
+
platform,
|
|
41
|
+
}),
|
|
42
|
+
_,
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
await ctx.applyPlugins({
|
|
46
|
+
name: 'build',
|
|
47
|
+
opts: {
|
|
48
|
+
config: {
|
|
49
|
+
...config,
|
|
50
|
+
isBuildPlugin: false,
|
|
51
|
+
isWatch,
|
|
52
|
+
outputRoot: `${config.outputRoot}/miniprogram`,
|
|
53
|
+
platform,
|
|
54
|
+
output: { ...(config.output || {}), clean: false },
|
|
55
|
+
},
|
|
56
|
+
options: Object.assign({}, options, {
|
|
57
|
+
platform,
|
|
58
|
+
}),
|
|
59
|
+
_,
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
await buildPlugin(plugin);
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
const docSrcPath = path.join(process.cwd(), 'src/plugin/doc');
|
|
68
|
+
const docDestPath = path.join(process.cwd(), 'miniprogram/doc');
|
|
69
|
+
fs.copy(docSrcPath, docDestPath);
|
|
70
|
+
} catch (err) {
|
|
71
|
+
console.error('[@spcsn/taro-cli] build plugin doc failed: ', err);
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
};
|