@ohos-ports/tarojs-cli 0.0.0-stage → 4.3.0-beta.1
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 -3
- package/bin/taro +7 -0
- package/dist/cli.d.ts +6 -0
- package/dist/cli.js +252 -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 +31 -0
- package/dist/create/creator.js.map +1 -0
- package/dist/create/fetchTemplate.d.ts +8 -0
- package/dist/create/fetchTemplate.js +137 -0
- package/dist/create/fetchTemplate.js.map +1 -0
- package/dist/create/page.d.ts +65 -0
- package/dist/create/page.js +252 -0
- package/dist/create/page.js.map +1 -0
- package/dist/create/plugin.d.ts +16 -0
- package/dist/create/plugin.js +48 -0
- package/dist/create/plugin.js.map +1 -0
- package/dist/create/project.d.ts +80 -0
- package/dist/create/project.js +519 -0
- package/dist/create/project.js.map +1 -0
- package/dist/doctor/index.d.ts +4 -0
- package/dist/doctor/index.js +32 -0
- package/dist/doctor/index.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/presets/commands/build.d.ts +3 -0
- package/dist/presets/commands/build.js +239 -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 +88 -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 +91 -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 +139 -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 +29 -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 +79 -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 +59 -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 +118 -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 +171 -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 +40 -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 +17 -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 +22 -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 +11 -0
- package/dist/presets/hooks/create.js.map +1 -0
- package/dist/presets/index.d.ts +4 -0
- package/dist/presets/index.js +17 -0
- package/dist/presets/index.js.map +1 -0
- package/dist/presets/platforms/plugin.d.ts +3 -0
- package/dist/presets/platforms/plugin.js +76 -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 +101 -0
- package/dist/presets/platforms/rn.js.map +1 -0
- package/dist/util/appConfig.d.ts +11 -0
- package/dist/util/appConfig.js +25 -0
- package/dist/util/appConfig.js.map +1 -0
- package/dist/util/createPage.d.ts +9 -0
- package/dist/util/createPage.js +117 -0
- package/dist/util/createPage.js.map +1 -0
- package/dist/util/defineConfig.d.ts +17 -0
- package/dist/util/defineConfig.js +12 -0
- package/dist/util/defineConfig.js.map +1 -0
- package/dist/util/index.d.ts +23 -0
- package/dist/util/index.js +125 -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 +76 -4
- package/postinstall.js +14 -0
- package/src/__tests__/__mocks__/presets.ts +15 -0
- package/src/__tests__/build-config.spec.ts +99 -0
- package/src/__tests__/cli.spec.ts +233 -0
- package/src/__tests__/config.spec.ts +232 -0
- package/src/__tests__/doctor-config.spec.ts +805 -0
- package/src/__tests__/doctor-eslint.spec.ts +31 -0
- package/src/__tests__/doctor-recommand.spec.ts +158 -0
- package/src/__tests__/doctor.spec.ts +77 -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 +38 -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 +74 -0
- package/src/__tests__/inspect.spec.ts +163 -0
- package/src/__tests__/project.spec.ts +304 -0
- package/src/__tests__/update.spec.ts +324 -0
- package/src/__tests__/utils/index.ts +54 -0
- package/src/cli.ts +250 -0
- package/src/commands/customCommand.ts +26 -0
- package/src/config/index.ts +7 -0
- package/src/config/manifest.default.json +35 -0
- package/src/config/packagesManagement.ts +20 -0
- package/src/config/rn-stylelint.json +5 -0
- package/src/config/tsconfig.json +24 -0
- package/src/create/constants.ts +1 -0
- package/src/create/creator.ts +37 -0
- package/src/create/fetchTemplate.ts +152 -0
- package/src/create/page.ts +299 -0
- package/src/create/plugin.ts +55 -0
- package/src/create/project.ts +591 -0
- package/src/doctor/index.ts +27 -0
- package/src/index.ts +24 -0
- package/src/presets/commands/build.ts +227 -0
- package/src/presets/commands/config.ts +86 -0
- package/src/presets/commands/create.ts +105 -0
- package/src/presets/commands/global-config.ts +140 -0
- package/src/presets/commands/help.ts +19 -0
- package/src/presets/commands/info.ts +77 -0
- package/src/presets/commands/init.ts +66 -0
- package/src/presets/commands/inspect.ts +138 -0
- package/src/presets/commands/update.ts +179 -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 +11 -0
- package/src/presets/index.ts +15 -0
- package/src/presets/platforms/plugin.ts +81 -0
- package/src/presets/platforms/rn.ts +113 -0
- package/src/util/appConfig.ts +29 -0
- package/src/util/createPage.ts +132 -0
- package/src/util/defineConfig.ts +22 -0
- package/src/util/index.ts +127 -0
- package/src/util/types.ts +81 -0
- package/templates/default/.husky/commit-msg +4 -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/_eslintrc +11 -0
- package/templates/default/_gitignore +8 -0
- package/templates/default/babel.config.js +14 -0
- package/templates/default/commitlint.config.mjs +1 -0
- package/templates/default/config/dev.js +10 -0
- package/templates/default/config/index.js +106 -0
- package/templates/default/config/prod.js +46 -0
- package/templates/default/package.json.tmpl +158 -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 +44 -0
- package/templates/default/src/index.html +17 -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/src/pages/index/index.vue +23 -0
- package/templates/default/stylelint.config.mjs +4 -0
- package/templates/default/template_creator.js +77 -0
- package/templates/default/tsconfig.json +32 -0
- package/templates/default/types/global.d.ts +35 -0
- package/templates/default/types/solid.d.ts +12 -0
- package/templates/default/types/vue.d.ts +10 -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 +29 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import * as path from 'node:path'
|
|
2
|
+
|
|
3
|
+
import { chalk } from '@tarojs/helper'
|
|
4
|
+
|
|
5
|
+
import { getPkgVersion } from '../util'
|
|
6
|
+
import { run } from './utils'
|
|
7
|
+
|
|
8
|
+
jest.mock('envinfo', () => {
|
|
9
|
+
const envinfo = jest.requireActual('envinfo')
|
|
10
|
+
return {
|
|
11
|
+
__esModule: true,
|
|
12
|
+
async run(data, options) {
|
|
13
|
+
const res = await envinfo.run(data, { ...options, json: true })
|
|
14
|
+
return JSON.parse(res)
|
|
15
|
+
},
|
|
16
|
+
}
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
const runInfo = run('info', ['commands/info'])
|
|
20
|
+
|
|
21
|
+
describe('info', () => {
|
|
22
|
+
it("should exit because there isn't a Taro project", async () => {
|
|
23
|
+
const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>
|
|
24
|
+
const logSpy = jest.spyOn(console, 'log')
|
|
25
|
+
|
|
26
|
+
exitSpy.mockImplementation(() => {
|
|
27
|
+
throw new Error()
|
|
28
|
+
})
|
|
29
|
+
logSpy.mockImplementation(() => {})
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
await runInfo('')
|
|
33
|
+
} catch (error) {} // eslint-disable-line no-empty
|
|
34
|
+
|
|
35
|
+
expect(exitSpy).toBeCalledWith(1)
|
|
36
|
+
expect(logSpy).toBeCalledWith(chalk.red('找不到项目配置文件config/index,请确定当前目录是 Taro 项目根目录!'))
|
|
37
|
+
|
|
38
|
+
exitSpy.mockRestore()
|
|
39
|
+
logSpy.mockRestore()
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('should log information', async () => {
|
|
43
|
+
const logSpy = jest.spyOn(console, 'log')
|
|
44
|
+
logSpy.mockImplementation(() => {})
|
|
45
|
+
|
|
46
|
+
const appPath = path.resolve(__dirname, 'fixtures/default')
|
|
47
|
+
await runInfo(appPath)
|
|
48
|
+
|
|
49
|
+
expect(logSpy).toBeCalledTimes(1)
|
|
50
|
+
const res = logSpy.mock.calls[0][0]
|
|
51
|
+
const title = `Taro CLI ${getPkgVersion()} environment info`
|
|
52
|
+
expect(res.hasOwnProperty(title)).toBeTruthy()
|
|
53
|
+
const info = res[title]
|
|
54
|
+
expect('System' in info).toBeTruthy()
|
|
55
|
+
expect('Binaries' in info).toBeTruthy()
|
|
56
|
+
// envinfo 还不支持 yarn workspace
|
|
57
|
+
// expect('npmPackages' in info).toBeTruthy()
|
|
58
|
+
// Note: windows 操作系统可能不存在 System.Shell
|
|
59
|
+
expect(Object.keys(info.System)).toEqual(expect.arrayContaining(['OS']))
|
|
60
|
+
// Note: 环境内可能不包括 Yarn
|
|
61
|
+
expect(Object.keys(info.Binaries)).toEqual(expect.arrayContaining(['Node', 'npm']))
|
|
62
|
+
// expect(info.npmPackages.hasOwnProperty('@tarojs/helper')).toBeTruthy()
|
|
63
|
+
// expect(info.npmPackages.hasOwnProperty('@tarojs/service')).toBeTruthy()
|
|
64
|
+
// expect(info.npmPackages.hasOwnProperty('@tarojs/taro')).toBeTruthy()
|
|
65
|
+
// expect(info.npmPackages.hasOwnProperty('@tarojs/taroize')).toBeTruthy()
|
|
66
|
+
// expect(info.npmPackages.hasOwnProperty('@tarojs/webpack-runner')).toBeTruthy()
|
|
67
|
+
// expect(info.npmPackages.hasOwnProperty('babel-plugin-transform-taroapi')).toBeTruthy()
|
|
68
|
+
// expect(info.npmPackages.hasOwnProperty('eslint-config-taro')).toBeTruthy()
|
|
69
|
+
// expect(info.npmPackages.hasOwnProperty('eslint-plugin-taro')).toBeTruthy()
|
|
70
|
+
// expect(info.npmPackages.hasOwnProperty('postcss-pxtransform')).toBeTruthy()
|
|
71
|
+
|
|
72
|
+
logSpy.mockRestore()
|
|
73
|
+
})
|
|
74
|
+
})
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import * as path from 'node:path'
|
|
2
|
+
|
|
3
|
+
import { chalk, fs } from '@tarojs/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('@tarojs/helper', () => {
|
|
17
|
+
const helper = jest.requireActual('@tarojs/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('@tarojs/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: 'h5'
|
|
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('\'main:h5\'')
|
|
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(
|
|
157
|
+
outputPath,
|
|
158
|
+
expect.stringMatching(/^'(browser|main:mini)'$/)
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
exitSpy.mockRestore()
|
|
162
|
+
})
|
|
163
|
+
})
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import Project from '../create/project'
|
|
2
|
+
|
|
3
|
+
jest.mock('@tarojs/helper', () => ({
|
|
4
|
+
chalk: {
|
|
5
|
+
blueBright: jest.fn((text) => text),
|
|
6
|
+
green: jest.fn((text) => text),
|
|
7
|
+
red: jest.fn((text) => text)
|
|
8
|
+
},
|
|
9
|
+
DEFAULT_TEMPLATE_SRC: 'https://github.com/NervJS/taro-project-templates',
|
|
10
|
+
DEFAULT_TEMPLATE_SRC_GITEE: 'https://gitee.com/o2team/taro-project-templates',
|
|
11
|
+
fs: {
|
|
12
|
+
ensureDirSync: jest.fn(),
|
|
13
|
+
existsSync: jest.fn(() => true)
|
|
14
|
+
},
|
|
15
|
+
getUserHomeDir: jest.fn(() => '/tmp'),
|
|
16
|
+
SOURCE_DIR: 'src',
|
|
17
|
+
TARO_BASE_CONFIG: 'index.json',
|
|
18
|
+
TARO_CONFIG_FOLDER: '.taro'
|
|
19
|
+
}))
|
|
20
|
+
|
|
21
|
+
jest.mock('@tarojs/shared', () => ({
|
|
22
|
+
isArray: Array.isArray
|
|
23
|
+
}), { virtual: true })
|
|
24
|
+
|
|
25
|
+
jest.mock('@tarojs/binding', () => ({
|
|
26
|
+
CompilerType: {
|
|
27
|
+
Solid: 'solid',
|
|
28
|
+
Vite: 'vite',
|
|
29
|
+
Webpack5: 'webpack5'
|
|
30
|
+
},
|
|
31
|
+
createProject: jest.fn(() => Promise.resolve()),
|
|
32
|
+
CSSType: {
|
|
33
|
+
None: 'none'
|
|
34
|
+
},
|
|
35
|
+
FrameworkType: {
|
|
36
|
+
Preact: 'preact',
|
|
37
|
+
React: 'react',
|
|
38
|
+
Solid: 'solid',
|
|
39
|
+
Vue3: 'vue3'
|
|
40
|
+
},
|
|
41
|
+
NpmType: {
|
|
42
|
+
Npm: 'npm'
|
|
43
|
+
},
|
|
44
|
+
PeriodType: {
|
|
45
|
+
CreateAPP: 'createApp'
|
|
46
|
+
}
|
|
47
|
+
}))
|
|
48
|
+
|
|
49
|
+
describe('create project', () => {
|
|
50
|
+
let logSpy: jest.SpyInstance
|
|
51
|
+
|
|
52
|
+
beforeEach(() => {
|
|
53
|
+
logSpy = jest.spyOn(console, 'log').mockImplementation()
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
afterEach(() => {
|
|
57
|
+
logSpy.mockRestore()
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('should resolve dot project name to current directory', () => {
|
|
61
|
+
const project = new Project({
|
|
62
|
+
projectDir: '/a/b/current-project',
|
|
63
|
+
projectName: '.',
|
|
64
|
+
sourceRoot: __dirname,
|
|
65
|
+
template: 'default',
|
|
66
|
+
templateSource: 'default-template',
|
|
67
|
+
npm: 'npm',
|
|
68
|
+
css: 'none',
|
|
69
|
+
framework: 'react'
|
|
70
|
+
} as any)
|
|
71
|
+
|
|
72
|
+
expect(project.conf.projectDir).toBe('/a/b')
|
|
73
|
+
expect(project.conf.projectName).toBe('current-project')
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('should resolve current directory path project name to current directory', () => {
|
|
77
|
+
const project = new Project({
|
|
78
|
+
projectDir: '/a/b/current-project',
|
|
79
|
+
projectName: './',
|
|
80
|
+
sourceRoot: __dirname,
|
|
81
|
+
template: 'default',
|
|
82
|
+
templateSource: 'default-template',
|
|
83
|
+
npm: 'npm',
|
|
84
|
+
css: 'none',
|
|
85
|
+
framework: 'react'
|
|
86
|
+
} as any)
|
|
87
|
+
|
|
88
|
+
expect(project.conf.projectDir).toBe('/a/b')
|
|
89
|
+
expect(project.conf.projectName).toBe('current-project')
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('should resolve windows-style ".\\" project name to current directory', () => {
|
|
93
|
+
const project = new Project({
|
|
94
|
+
projectDir: '/a/b/current-project',
|
|
95
|
+
projectName: '.\\',
|
|
96
|
+
sourceRoot: __dirname,
|
|
97
|
+
template: 'default',
|
|
98
|
+
templateSource: 'default-template',
|
|
99
|
+
npm: 'npm',
|
|
100
|
+
css: 'none',
|
|
101
|
+
framework: 'react'
|
|
102
|
+
} as any)
|
|
103
|
+
|
|
104
|
+
expect(project.conf.projectDir).toBe('/a/b')
|
|
105
|
+
expect(project.conf.projectName).toBe('current-project')
|
|
106
|
+
expect(project.conf.initInCurrentDir).toBe(true)
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it('should fall back to process.cwd() when projectDir is empty', () => {
|
|
110
|
+
const cwdSpy = jest.spyOn(process, 'cwd').mockReturnValue('/x/y/z')
|
|
111
|
+
try {
|
|
112
|
+
const project = new Project({
|
|
113
|
+
projectName: '.',
|
|
114
|
+
sourceRoot: __dirname,
|
|
115
|
+
template: 'default',
|
|
116
|
+
templateSource: 'default-template',
|
|
117
|
+
npm: 'npm',
|
|
118
|
+
css: 'none',
|
|
119
|
+
framework: 'react'
|
|
120
|
+
} as any)
|
|
121
|
+
|
|
122
|
+
expect(project.conf.projectDir).toBe('/x/y')
|
|
123
|
+
expect(project.conf.projectName).toBe('z')
|
|
124
|
+
expect(project.conf.initInCurrentDir).toBe(true)
|
|
125
|
+
} finally {
|
|
126
|
+
cwdSpy.mockRestore()
|
|
127
|
+
}
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
it.each(['..', '../', '.foo', 'foo', 'my-app'])(
|
|
131
|
+
'should keep project name %p unchanged',
|
|
132
|
+
(projectName) => {
|
|
133
|
+
const project = new Project({
|
|
134
|
+
projectDir: '/a/b',
|
|
135
|
+
projectName,
|
|
136
|
+
sourceRoot: __dirname,
|
|
137
|
+
template: 'default',
|
|
138
|
+
templateSource: 'default-template',
|
|
139
|
+
npm: 'npm',
|
|
140
|
+
css: 'none',
|
|
141
|
+
framework: 'react'
|
|
142
|
+
} as any)
|
|
143
|
+
|
|
144
|
+
expect(project.conf.projectDir).toBe('/a/b')
|
|
145
|
+
expect(project.conf.projectName).toBe(projectName)
|
|
146
|
+
expect(project.conf.initInCurrentDir).toBeUndefined()
|
|
147
|
+
}
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
it('should throw when initializing at the filesystem root', () => {
|
|
151
|
+
expect(() => new Project({
|
|
152
|
+
projectDir: '/',
|
|
153
|
+
projectName: '.',
|
|
154
|
+
sourceRoot: __dirname,
|
|
155
|
+
template: 'default',
|
|
156
|
+
templateSource: 'default-template',
|
|
157
|
+
npm: 'npm',
|
|
158
|
+
css: 'none',
|
|
159
|
+
framework: 'react'
|
|
160
|
+
} as any)).toThrow('无法在文件系统根目录下初始化项目')
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
it('should skip existing-name prompt when initializing in current directory', () => {
|
|
164
|
+
// fs.existsSync 被 mock 为恒 true,模拟目标目录(当前目录)已存在。
|
|
165
|
+
// `.` 场景下不应因此推入“同名目录已存在”的询问。
|
|
166
|
+
const project = new Project({
|
|
167
|
+
projectDir: '/a/b/current-project',
|
|
168
|
+
projectName: '.',
|
|
169
|
+
sourceRoot: __dirname,
|
|
170
|
+
template: 'default',
|
|
171
|
+
templateSource: 'default-template',
|
|
172
|
+
npm: 'npm',
|
|
173
|
+
css: 'none',
|
|
174
|
+
framework: 'react'
|
|
175
|
+
} as any)
|
|
176
|
+
|
|
177
|
+
expect(project.conf.initInCurrentDir).toBe(true)
|
|
178
|
+
|
|
179
|
+
const prompts: Record<string, unknown>[] = []
|
|
180
|
+
project.askProjectName(project.conf, prompts)
|
|
181
|
+
|
|
182
|
+
expect(prompts).toHaveLength(0)
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
it('should prompt for a new name when a same-name directory exists (normal init)', () => {
|
|
186
|
+
// 普通命名 + 目标位置已存在同名目录时,仍应提示换名。
|
|
187
|
+
const project = new Project({
|
|
188
|
+
projectDir: '/a/b',
|
|
189
|
+
projectName: 'existing-project',
|
|
190
|
+
sourceRoot: __dirname,
|
|
191
|
+
template: 'default',
|
|
192
|
+
templateSource: 'default-template',
|
|
193
|
+
npm: 'npm',
|
|
194
|
+
css: 'none',
|
|
195
|
+
framework: 'react'
|
|
196
|
+
} as any)
|
|
197
|
+
|
|
198
|
+
expect(project.conf.initInCurrentDir).toBeUndefined()
|
|
199
|
+
|
|
200
|
+
const prompts: Record<string, unknown>[] = []
|
|
201
|
+
project.askProjectName(project.conf, prompts)
|
|
202
|
+
|
|
203
|
+
expect(prompts).toHaveLength(1)
|
|
204
|
+
expect(prompts[0]).toMatchObject({ name: 'projectName' })
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
it('should prompt for platforms when conf.platforms is undefined', () => {
|
|
208
|
+
const project = new Project({
|
|
209
|
+
projectDir: '/a/b',
|
|
210
|
+
projectName: 'my-project',
|
|
211
|
+
sourceRoot: __dirname,
|
|
212
|
+
template: 'default',
|
|
213
|
+
templateSource: 'default-template',
|
|
214
|
+
npm: 'npm',
|
|
215
|
+
css: 'none',
|
|
216
|
+
framework: 'react'
|
|
217
|
+
} as any)
|
|
218
|
+
|
|
219
|
+
const prompts: Record<string, unknown>[] = []
|
|
220
|
+
project.askPlatforms(project.conf, prompts)
|
|
221
|
+
|
|
222
|
+
expect(prompts).toHaveLength(1)
|
|
223
|
+
expect(prompts[0]).toMatchObject({ type: 'checkbox', name: 'platforms' })
|
|
224
|
+
})
|
|
225
|
+
|
|
226
|
+
it('should not prompt for platforms when conf.platforms is already an array', () => {
|
|
227
|
+
const project = new Project({
|
|
228
|
+
projectDir: '/a/b',
|
|
229
|
+
projectName: 'my-project',
|
|
230
|
+
sourceRoot: __dirname,
|
|
231
|
+
template: 'default',
|
|
232
|
+
templateSource: 'default-template',
|
|
233
|
+
npm: 'npm',
|
|
234
|
+
css: 'none',
|
|
235
|
+
framework: 'react',
|
|
236
|
+
platforms: ['weapp', 'h5']
|
|
237
|
+
} as any)
|
|
238
|
+
|
|
239
|
+
const prompts: Record<string, unknown>[] = []
|
|
240
|
+
project.askPlatforms(project.conf, prompts)
|
|
241
|
+
|
|
242
|
+
expect(prompts).toHaveLength(0)
|
|
243
|
+
expect(project.conf.platforms).toEqual(['weapp', 'h5'])
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
it('should normalize a comma-separated platforms string into an array', () => {
|
|
247
|
+
const project = new Project({
|
|
248
|
+
projectDir: '/a/b',
|
|
249
|
+
projectName: 'my-project',
|
|
250
|
+
sourceRoot: __dirname,
|
|
251
|
+
template: 'default',
|
|
252
|
+
templateSource: 'default-template',
|
|
253
|
+
npm: 'npm',
|
|
254
|
+
css: 'none',
|
|
255
|
+
framework: 'react',
|
|
256
|
+
platforms: 'weapp, h5'
|
|
257
|
+
} as any)
|
|
258
|
+
|
|
259
|
+
const prompts: Record<string, unknown>[] = []
|
|
260
|
+
project.askPlatforms(project.conf, prompts)
|
|
261
|
+
|
|
262
|
+
expect(project.conf.platforms).toEqual(['weapp', 'h5'])
|
|
263
|
+
expect(prompts).toHaveLength(0)
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
it.each([
|
|
267
|
+
[[], '未选择平台'],
|
|
268
|
+
[['unknown'], 'unknown']
|
|
269
|
+
])('should reject invalid platforms %p', (platforms, message) => {
|
|
270
|
+
const project = new Project({
|
|
271
|
+
projectDir: '/a/b',
|
|
272
|
+
projectName: 'my-project',
|
|
273
|
+
sourceRoot: __dirname,
|
|
274
|
+
template: 'default',
|
|
275
|
+
templateSource: 'default-template',
|
|
276
|
+
npm: 'npm',
|
|
277
|
+
css: 'none',
|
|
278
|
+
framework: 'react',
|
|
279
|
+
platforms
|
|
280
|
+
} as any)
|
|
281
|
+
|
|
282
|
+
expect(() => project.askPlatforms(project.conf, [])).toThrow(message)
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
it('should require at least one platform when prompting', () => {
|
|
286
|
+
const project = new Project({
|
|
287
|
+
projectDir: '/a/b',
|
|
288
|
+
projectName: 'my-project',
|
|
289
|
+
sourceRoot: __dirname,
|
|
290
|
+
template: 'default',
|
|
291
|
+
templateSource: 'default-template',
|
|
292
|
+
npm: 'npm',
|
|
293
|
+
css: 'none',
|
|
294
|
+
framework: 'react'
|
|
295
|
+
} as any)
|
|
296
|
+
|
|
297
|
+
const prompts: Record<string, unknown>[] = []
|
|
298
|
+
project.askPlatforms(project.conf, prompts)
|
|
299
|
+
const validate = prompts[0].validate as (input: string[]) => true | string
|
|
300
|
+
|
|
301
|
+
expect(validate([])).toBe('请至少选择一个平台!')
|
|
302
|
+
expect(validate(['weapp'])).toBe(true)
|
|
303
|
+
})
|
|
304
|
+
})
|