@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,160 @@
|
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import { chalk, fs } from '@spcsn/taro-helper';
|
|
4
|
+
|
|
5
|
+
import { run } from './utils';
|
|
6
|
+
|
|
7
|
+
jest.mock('cli-highlight', () => {
|
|
8
|
+
return {
|
|
9
|
+
__esModule: true,
|
|
10
|
+
default(str) {
|
|
11
|
+
return str;
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
jest.mock('@spcsn/taro-helper', () => {
|
|
17
|
+
const helper = jest.requireActual('@spcsn/taro-helper');
|
|
18
|
+
const fs = helper.fs;
|
|
19
|
+
return {
|
|
20
|
+
__esModule: true,
|
|
21
|
+
...helper,
|
|
22
|
+
fs: {
|
|
23
|
+
...fs,
|
|
24
|
+
writeFileSync: jest.fn(),
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const runInspect = run('inspect', [
|
|
30
|
+
'commands/build',
|
|
31
|
+
'commands/inspect',
|
|
32
|
+
require.resolve('@spcsn/taro-plugin-platform-weapp'),
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
describe('inspect', () => {
|
|
36
|
+
beforeEach(() => {
|
|
37
|
+
jest.resetModules();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("should exit because there isn't a Taro project", async () => {
|
|
41
|
+
const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>;
|
|
42
|
+
const logSpy = jest.spyOn(console, 'log');
|
|
43
|
+
|
|
44
|
+
exitSpy.mockImplementation(() => {
|
|
45
|
+
throw new Error();
|
|
46
|
+
});
|
|
47
|
+
logSpy.mockImplementation(() => {});
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
await runInspect('');
|
|
51
|
+
} catch (error) {} // eslint-disable-line no-empty
|
|
52
|
+
|
|
53
|
+
expect(exitSpy).toBeCalledWith(1);
|
|
54
|
+
expect(logSpy).toBeCalledWith(chalk.red('找不到项目配置文件config/index,请确定当前目录是 Taro 项目根目录!'));
|
|
55
|
+
|
|
56
|
+
exitSpy.mockRestore();
|
|
57
|
+
logSpy.mockRestore();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("should exit when user haven't pass correct type", async () => {
|
|
61
|
+
const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>;
|
|
62
|
+
const logSpy = jest.spyOn(console, 'log');
|
|
63
|
+
|
|
64
|
+
exitSpy.mockImplementation(() => {
|
|
65
|
+
throw new Error();
|
|
66
|
+
});
|
|
67
|
+
logSpy.mockImplementation(() => {});
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
await runInspect(path.resolve(__dirname, 'fixtures/default'));
|
|
71
|
+
} catch (error) {} // eslint-disable-line no-empty
|
|
72
|
+
|
|
73
|
+
expect(exitSpy).toBeCalledWith(0);
|
|
74
|
+
expect(logSpy).toBeCalledWith(chalk.red('请传入正确的编译类型!'));
|
|
75
|
+
|
|
76
|
+
exitSpy.mockRestore();
|
|
77
|
+
logSpy.mockRestore();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('should log config', async () => {
|
|
81
|
+
const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>;
|
|
82
|
+
const logSpy = jest.spyOn(console, 'info');
|
|
83
|
+
|
|
84
|
+
exitSpy.mockImplementation(() => {
|
|
85
|
+
throw new Error();
|
|
86
|
+
});
|
|
87
|
+
logSpy.mockImplementation(() => {});
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
const appPath = path.resolve(__dirname, 'fixtures/default');
|
|
91
|
+
await runInspect(appPath, {
|
|
92
|
+
options: {
|
|
93
|
+
type: 'weapp',
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
} catch (error) {} // eslint-disable-line no-empty
|
|
97
|
+
|
|
98
|
+
expect(exitSpy).toBeCalledWith(0);
|
|
99
|
+
expect(logSpy).toBeCalledTimes(1);
|
|
100
|
+
|
|
101
|
+
exitSpy.mockRestore();
|
|
102
|
+
logSpy.mockRestore();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('should log specific config', async () => {
|
|
106
|
+
const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>;
|
|
107
|
+
const logSpy = jest.spyOn(console, 'info');
|
|
108
|
+
const errorSpy = jest.spyOn(console, 'error');
|
|
109
|
+
|
|
110
|
+
exitSpy.mockImplementation(() => {
|
|
111
|
+
throw new Error();
|
|
112
|
+
});
|
|
113
|
+
logSpy.mockImplementation(() => {});
|
|
114
|
+
errorSpy.mockImplementation(() => {});
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
const appPath = path.resolve(__dirname, 'fixtures/default');
|
|
118
|
+
await runInspect(appPath, {
|
|
119
|
+
options: {
|
|
120
|
+
type: 'weapp',
|
|
121
|
+
},
|
|
122
|
+
args: ['resolve.mainFields.0'],
|
|
123
|
+
});
|
|
124
|
+
} catch (error) {} // eslint-disable-line no-empty
|
|
125
|
+
|
|
126
|
+
expect(exitSpy).toBeCalledWith(0);
|
|
127
|
+
expect(logSpy).toBeCalledTimes(1);
|
|
128
|
+
expect(logSpy).toBeCalledWith("'browser'");
|
|
129
|
+
|
|
130
|
+
exitSpy.mockRestore();
|
|
131
|
+
logSpy.mockRestore();
|
|
132
|
+
errorSpy.mockRestore();
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('should output config', async () => {
|
|
136
|
+
const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>;
|
|
137
|
+
const writeFileSync = fs.writeFileSync as jest.Mock<any>;
|
|
138
|
+
const outputPath = 'project-config.js';
|
|
139
|
+
|
|
140
|
+
exitSpy.mockImplementation(() => {
|
|
141
|
+
throw new Error();
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
try {
|
|
145
|
+
const appPath = path.resolve(__dirname, 'fixtures/default');
|
|
146
|
+
await runInspect(appPath, {
|
|
147
|
+
options: {
|
|
148
|
+
type: 'weapp',
|
|
149
|
+
output: outputPath,
|
|
150
|
+
},
|
|
151
|
+
args: ['resolve.mainFields.0'],
|
|
152
|
+
});
|
|
153
|
+
} catch (error) {} // eslint-disable-line no-empty
|
|
154
|
+
|
|
155
|
+
expect(exitSpy).toBeCalledWith(0);
|
|
156
|
+
expect(writeFileSync).toBeCalledWith(outputPath, "'browser'");
|
|
157
|
+
|
|
158
|
+
exitSpy.mockRestore();
|
|
159
|
+
});
|
|
160
|
+
});
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import { chalk, fs, PROJECT_CONFIG, shouldUseCnpm, shouldUseYarn } from '@spcsn/taro-helper';
|
|
4
|
+
import { exec } from 'child_process';
|
|
5
|
+
|
|
6
|
+
import { getPkgVersion } from '../util';
|
|
7
|
+
import { run } from './utils';
|
|
8
|
+
|
|
9
|
+
const runUpdate = run('update', ['commands/update']);
|
|
10
|
+
const lastestVersion = getPkgVersion();
|
|
11
|
+
|
|
12
|
+
jest.mock('child_process', () => {
|
|
13
|
+
const exec = jest.fn();
|
|
14
|
+
exec.mockReturnValue({
|
|
15
|
+
stdout: {
|
|
16
|
+
on() {},
|
|
17
|
+
},
|
|
18
|
+
stderr: {
|
|
19
|
+
on() {},
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
return {
|
|
23
|
+
__esModule: true,
|
|
24
|
+
exec,
|
|
25
|
+
};
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
jest.mock('ora', () => {
|
|
29
|
+
const ora = jest.fn();
|
|
30
|
+
ora.mockReturnValue({
|
|
31
|
+
start() {
|
|
32
|
+
return {
|
|
33
|
+
stop() {},
|
|
34
|
+
warn() {},
|
|
35
|
+
succeed() {},
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
return ora;
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
jest.mock('@spcsn/taro-helper', () => {
|
|
43
|
+
const helper = jest.requireActual('@spcsn/taro-helper');
|
|
44
|
+
const fs = helper.fs;
|
|
45
|
+
return {
|
|
46
|
+
__esModule: true,
|
|
47
|
+
...helper,
|
|
48
|
+
shouldUseCnpm: jest.fn(),
|
|
49
|
+
shouldUseYarn: jest.fn(),
|
|
50
|
+
chalk: {
|
|
51
|
+
red: jest.fn(),
|
|
52
|
+
green() {},
|
|
53
|
+
},
|
|
54
|
+
fs: {
|
|
55
|
+
...fs,
|
|
56
|
+
writeJson: jest.fn(),
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
jest.mock('latest-version', () => () => lastestVersion);
|
|
62
|
+
|
|
63
|
+
function updatePkg(pkgPath: string, version: string) {
|
|
64
|
+
let packageMap = require(pkgPath);
|
|
65
|
+
packageMap = {
|
|
66
|
+
...packageMap,
|
|
67
|
+
dependencies: {
|
|
68
|
+
...packageMap.dependencies,
|
|
69
|
+
'@spcsn/taro-shared': version,
|
|
70
|
+
'@spcsn/taro': version,
|
|
71
|
+
'@spcsn/taro-cli': version,
|
|
72
|
+
'@spcsn/taro-components': version,
|
|
73
|
+
'@spcsn/taro-api': version,
|
|
74
|
+
'@spcsn/taro-helper': version,
|
|
75
|
+
'@spcsn/taro-react': version,
|
|
76
|
+
'@spcsn/taro-runner-utils': version,
|
|
77
|
+
'@spcsn/taro-runtime': version,
|
|
78
|
+
'@spcsn/taro-service': version,
|
|
79
|
+
'@spcsn/taro-plugin-platform-weapp': version,
|
|
80
|
+
'@spcsn/taro-plugin-framework-react': version,
|
|
81
|
+
'@spcsn/taro-plugin-generator': version,
|
|
82
|
+
'@spcsn/taro-vite-runner': version,
|
|
83
|
+
'@spcsn/taro-binding': version,
|
|
84
|
+
},
|
|
85
|
+
devDependencies: {
|
|
86
|
+
...packageMap.devDependencies,
|
|
87
|
+
'babel-preset-taro': version,
|
|
88
|
+
'babel-plugin-transform-taroapi': version,
|
|
89
|
+
'postcss-plugin-constparse': version,
|
|
90
|
+
'postcss-pxtransform': version,
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
return packageMap;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
describe('update', () => {
|
|
97
|
+
const execMocked = exec as unknown as jest.Mock<any>;
|
|
98
|
+
const shouldUseCnpmMocked = shouldUseCnpm as jest.Mock<any>;
|
|
99
|
+
const shouldUseYarnMocked = shouldUseYarn as jest.Mock<any>;
|
|
100
|
+
const writeJson = fs.writeJson as jest.Mock<any>;
|
|
101
|
+
|
|
102
|
+
beforeEach(() => {
|
|
103
|
+
shouldUseCnpmMocked.mockReturnValue(false);
|
|
104
|
+
shouldUseYarnMocked.mockReturnValue(false);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
afterEach(() => {
|
|
108
|
+
execMocked.mockClear();
|
|
109
|
+
shouldUseCnpmMocked.mockReset();
|
|
110
|
+
shouldUseYarnMocked.mockReset();
|
|
111
|
+
writeJson.mockClear();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('should log errors', async () => {
|
|
115
|
+
const spy = jest.spyOn(console, 'log');
|
|
116
|
+
spy.mockImplementation(() => {});
|
|
117
|
+
await runUpdate('', {
|
|
118
|
+
options: {
|
|
119
|
+
npm: 'npm',
|
|
120
|
+
disableGlobalConfig: true,
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
expect(spy).toBeCalledTimes(3);
|
|
124
|
+
spy.mockRestore();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('should update self', async () => {
|
|
128
|
+
await runUpdate('', {
|
|
129
|
+
args: ['self'],
|
|
130
|
+
options: {
|
|
131
|
+
npm: 'npm',
|
|
132
|
+
disableGlobalConfig: true,
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
expect(execMocked).toBeCalledWith(`npm i -g @spcsn/taro-cli@${lastestVersion}`);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('should update self using yarn', async () => {
|
|
139
|
+
shouldUseCnpmMocked.mockReturnValue(true);
|
|
140
|
+
await runUpdate('', {
|
|
141
|
+
args: ['self'],
|
|
142
|
+
options: {
|
|
143
|
+
npm: 'yarn',
|
|
144
|
+
disableGlobalConfig: true,
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
expect(execMocked).toBeCalledWith(`yarn global add @spcsn/taro-cli@${lastestVersion}`);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('should update self using pnpm', async () => {
|
|
151
|
+
shouldUseCnpmMocked.mockReturnValue(true);
|
|
152
|
+
await runUpdate('', {
|
|
153
|
+
args: ['self'],
|
|
154
|
+
options: {
|
|
155
|
+
npm: 'pnpm',
|
|
156
|
+
disableGlobalConfig: true,
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
expect(execMocked).toBeCalledWith(`pnpm add -g @spcsn/taro-cli@${lastestVersion}`);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('should update self using cnpm', async () => {
|
|
163
|
+
shouldUseCnpmMocked.mockReturnValue(true);
|
|
164
|
+
await runUpdate('', {
|
|
165
|
+
args: ['self'],
|
|
166
|
+
options: {
|
|
167
|
+
npm: 'cnpm',
|
|
168
|
+
disableGlobalConfig: true,
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
expect(execMocked).toBeCalledWith(`cnpm i -g @spcsn/taro-cli@${lastestVersion}`);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('should update self to specific version', async () => {
|
|
175
|
+
const version = '3.0.0-beta.0';
|
|
176
|
+
await runUpdate('', {
|
|
177
|
+
args: ['self', version],
|
|
178
|
+
options: {
|
|
179
|
+
npm: 'npm',
|
|
180
|
+
disableGlobalConfig: true,
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
expect(execMocked).toBeCalledWith(`npm i -g @spcsn/taro-cli@${version}`);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("should throw when there isn't a Taro project", async () => {
|
|
187
|
+
const chalkMocked = chalk.red as unknown as jest.Mock<any>;
|
|
188
|
+
const exitSpy = jest.spyOn(process, 'exit');
|
|
189
|
+
const logSpy = jest.spyOn(console, 'log');
|
|
190
|
+
exitSpy.mockImplementation(() => {
|
|
191
|
+
throw new Error();
|
|
192
|
+
});
|
|
193
|
+
logSpy.mockImplementation(() => {});
|
|
194
|
+
try {
|
|
195
|
+
await runUpdate('', {
|
|
196
|
+
args: ['project'],
|
|
197
|
+
options: {
|
|
198
|
+
npm: 'npm',
|
|
199
|
+
disableGlobalConfig: true,
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
} catch (error) {} // eslint-disable-line no-empty
|
|
203
|
+
expect(exitSpy).toBeCalledWith(1);
|
|
204
|
+
expect(chalkMocked).toBeCalledWith(`找不到项目配置文件 ${PROJECT_CONFIG},请确定当前目录是 Taro 项目根目录!`);
|
|
205
|
+
exitSpy.mockRestore();
|
|
206
|
+
logSpy.mockRestore();
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('should update project', async () => {
|
|
210
|
+
const appPath = path.resolve(__dirname, 'fixtures/default');
|
|
211
|
+
const pkgPath = path.join(appPath, 'package.json');
|
|
212
|
+
const packageMap = updatePkg(pkgPath, lastestVersion);
|
|
213
|
+
|
|
214
|
+
const logSpy = jest.spyOn(console, 'log');
|
|
215
|
+
logSpy.mockImplementation(() => {});
|
|
216
|
+
|
|
217
|
+
await runUpdate(appPath, {
|
|
218
|
+
args: ['project'],
|
|
219
|
+
options: {
|
|
220
|
+
npm: 'npm',
|
|
221
|
+
disableGlobalConfig: true,
|
|
222
|
+
},
|
|
223
|
+
});
|
|
224
|
+
expect(writeJson.mock.calls[0][0]).toEqual(pkgPath);
|
|
225
|
+
expect(writeJson.mock.calls[0][1]).toEqual(packageMap);
|
|
226
|
+
expect(execMocked).toBeCalledWith('npm install');
|
|
227
|
+
|
|
228
|
+
logSpy.mockRestore();
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it('should update project to specific version', async () => {
|
|
232
|
+
const version = '3.0.0-beta.4';
|
|
233
|
+
const appPath = path.resolve(__dirname, 'fixtures/default');
|
|
234
|
+
const pkgPath = path.join(appPath, 'package.json');
|
|
235
|
+
const packageMap = updatePkg(pkgPath, version);
|
|
236
|
+
|
|
237
|
+
const logSpy = jest.spyOn(console, 'log');
|
|
238
|
+
logSpy.mockImplementation(() => {});
|
|
239
|
+
|
|
240
|
+
await runUpdate(appPath, {
|
|
241
|
+
args: ['project', version],
|
|
242
|
+
options: {
|
|
243
|
+
npm: 'npm',
|
|
244
|
+
disableGlobalConfig: true,
|
|
245
|
+
},
|
|
246
|
+
});
|
|
247
|
+
expect(writeJson.mock.calls[0][0]).toEqual(pkgPath);
|
|
248
|
+
expect(writeJson.mock.calls[0][1]).toEqual(packageMap);
|
|
249
|
+
expect(execMocked).toBeCalledWith('npm install');
|
|
250
|
+
|
|
251
|
+
logSpy.mockRestore();
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it('should update project with yarn', async () => {
|
|
255
|
+
const appPath = path.resolve(__dirname, 'fixtures/default');
|
|
256
|
+
|
|
257
|
+
const logSpy = jest.spyOn(console, 'log');
|
|
258
|
+
logSpy.mockImplementation(() => {});
|
|
259
|
+
shouldUseYarnMocked.mockReturnValue(true);
|
|
260
|
+
|
|
261
|
+
await runUpdate(appPath, {
|
|
262
|
+
args: ['project'],
|
|
263
|
+
options: {
|
|
264
|
+
npm: 'yarn',
|
|
265
|
+
disableGlobalConfig: true,
|
|
266
|
+
},
|
|
267
|
+
});
|
|
268
|
+
expect(execMocked).toBeCalledWith('yarn install');
|
|
269
|
+
|
|
270
|
+
logSpy.mockRestore();
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it('should update project with pnpm', async () => {
|
|
274
|
+
const appPath = path.resolve(__dirname, 'fixtures/default');
|
|
275
|
+
|
|
276
|
+
const logSpy = jest.spyOn(console, 'log');
|
|
277
|
+
logSpy.mockImplementation(() => {});
|
|
278
|
+
shouldUseCnpmMocked.mockReturnValue(true);
|
|
279
|
+
|
|
280
|
+
await runUpdate(appPath, {
|
|
281
|
+
args: ['project'],
|
|
282
|
+
options: {
|
|
283
|
+
npm: 'pnpm',
|
|
284
|
+
disableGlobalConfig: true,
|
|
285
|
+
},
|
|
286
|
+
});
|
|
287
|
+
expect(execMocked).toBeCalledWith('pnpm install');
|
|
288
|
+
|
|
289
|
+
logSpy.mockRestore();
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it('should update project with cnpm', async () => {
|
|
293
|
+
const appPath = path.resolve(__dirname, 'fixtures/default');
|
|
294
|
+
|
|
295
|
+
const logSpy = jest.spyOn(console, 'log');
|
|
296
|
+
logSpy.mockImplementation(() => {});
|
|
297
|
+
shouldUseCnpmMocked.mockReturnValue(true);
|
|
298
|
+
|
|
299
|
+
await runUpdate(appPath, {
|
|
300
|
+
args: ['project'],
|
|
301
|
+
options: {
|
|
302
|
+
npm: 'cnpm',
|
|
303
|
+
disableGlobalConfig: true,
|
|
304
|
+
},
|
|
305
|
+
});
|
|
306
|
+
expect(execMocked).toBeCalledWith('cnpm install');
|
|
307
|
+
|
|
308
|
+
logSpy.mockRestore();
|
|
309
|
+
});
|
|
310
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import { Config, Kernel } from '@spcsn/taro-service';
|
|
4
|
+
|
|
5
|
+
interface IRunOptions {
|
|
6
|
+
options?: Record<string, string | boolean>;
|
|
7
|
+
args?: string[];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface IRun {
|
|
11
|
+
(appPath: string, options?: IRunOptions): Promise<Kernel>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function run(name: string, presets: string[] = []): IRun {
|
|
15
|
+
return async function (appPath, opts = {}) {
|
|
16
|
+
const { options = {}, args = [] } = opts;
|
|
17
|
+
|
|
18
|
+
const config = new Config({
|
|
19
|
+
appPath: appPath,
|
|
20
|
+
disableGlobalConfig: !!options.disableGlobalConfig,
|
|
21
|
+
});
|
|
22
|
+
await config.init({
|
|
23
|
+
mode: (options.mode || process.env.NODE_ENV) as string,
|
|
24
|
+
command: name,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const kernel = new Kernel({
|
|
28
|
+
appPath: appPath,
|
|
29
|
+
presets: [
|
|
30
|
+
path.resolve(__dirname, '../__mocks__', 'presets.ts'),
|
|
31
|
+
...presets.map((e) => (path.isAbsolute(e) ? e : path.resolve(__dirname, '../../presets', `${e}.ts`))),
|
|
32
|
+
],
|
|
33
|
+
plugins: [],
|
|
34
|
+
config,
|
|
35
|
+
});
|
|
36
|
+
kernel.optsPlugins ||= [];
|
|
37
|
+
|
|
38
|
+
const type = options.type;
|
|
39
|
+
if (typeof type === 'string' && !presets.some((e) => e.includes(type))) {
|
|
40
|
+
kernel.optsPlugins.push(require.resolve(`@spcsn/taro-plugin-platform-${options.type}`));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
await kernel.run({
|
|
44
|
+
name,
|
|
45
|
+
opts: {
|
|
46
|
+
_: [name, ...args],
|
|
47
|
+
options,
|
|
48
|
+
isHelp: false,
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return kernel;
|
|
53
|
+
};
|
|
54
|
+
}
|