@spcsn/taro-cli 0.1.4 → 0.1.6

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.
Files changed (38) hide show
  1. package/README.md +34 -0
  2. package/dist/presets/commands/doctor.js +1 -7
  3. package/dist/presets/commands/doctor.js.map +1 -1
  4. package/package.json +12 -12
  5. package/src/doctor/validators.ts +1 -1
  6. package/src/presets/commands/doctor.ts +9 -9
  7. package/src/__tests__/__mocks__/presets.ts +0 -15
  8. package/src/__tests__/build-config.spec.ts +0 -66
  9. package/src/__tests__/cli.spec.ts +0 -226
  10. package/src/__tests__/config.spec.ts +0 -224
  11. package/src/__tests__/doctor-config.spec.ts +0 -920
  12. package/src/__tests__/doctor-recommand.spec.ts +0 -136
  13. package/src/__tests__/doctor.spec.ts +0 -94
  14. package/src/__tests__/dotenv-parse.spec.ts +0 -105
  15. package/src/__tests__/env/.env +0 -2
  16. package/src/__tests__/fixtures/default/.env +0 -3
  17. package/src/__tests__/fixtures/default/.env.development +0 -1
  18. package/src/__tests__/fixtures/default/.env.local +0 -1
  19. package/src/__tests__/fixtures/default/.env.pre +0 -3
  20. package/src/__tests__/fixtures/default/.env.production +0 -1
  21. package/src/__tests__/fixtures/default/.env.uat +0 -5
  22. package/src/__tests__/fixtures/default/.env.uat.local +0 -1
  23. package/src/__tests__/fixtures/default/babel.config.js +0 -10
  24. package/src/__tests__/fixtures/default/config/dev.js +0 -9
  25. package/src/__tests__/fixtures/default/config/index.js +0 -84
  26. package/src/__tests__/fixtures/default/config/prod.js +0 -18
  27. package/src/__tests__/fixtures/default/package.json +0 -27
  28. package/src/__tests__/fixtures/default/src/app.config.js +0 -11
  29. package/src/__tests__/fixtures/default/src/app.js +0 -16
  30. package/src/__tests__/fixtures/default/src/app.scss +0 -0
  31. package/src/__tests__/fixtures/default/src/index.html +0 -19
  32. package/src/__tests__/fixtures/default/src/pages/index/index.config.js +0 -3
  33. package/src/__tests__/fixtures/default/src/pages/index/index.jsx +0 -22
  34. package/src/__tests__/fixtures/default/src/pages/index/index.scss +0 -0
  35. package/src/__tests__/info.spec.ts +0 -72
  36. package/src/__tests__/inspect.spec.ts +0 -160
  37. package/src/__tests__/update.spec.ts +0 -310
  38. package/src/__tests__/utils/index.ts +0 -54
@@ -1,160 +0,0 @@
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
- });
@@ -1,310 +0,0 @@
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
- });
@@ -1,54 +0,0 @@
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
- }