forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframeworks.ts
283 lines (265 loc) · 10.8 KB
/
frameworks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import path from 'path'
import fs from 'fs-extra'
import * as dependencies from './dependencies'
import componentIndexHtmlGenerator from './component-index-template'
import semver from 'semver'
export type PkgJson = { version: string, dependencies?: Record<string, string>, devDependencies?: Record<string, string> }
export type WizardBundler = typeof dependencies.WIZARD_BUNDLERS[number]
export type CodeGenFramework = typeof WIZARD_FRAMEWORKS[number]['codeGenFramework']
export type WizardDependency = typeof dependencies.WIZARD_DEPENDENCIES[number]
export interface DependencyToInstall {
dependency: WizardDependency
satisfied: boolean
loc: string | null
detectedVersion: string | null
}
export type WizardFrontendFramework = typeof WIZARD_FRAMEWORKS[number] & { specPattern?: string }
export async function isDependencyInstalled (dependency: WizardDependency, projectPath: string): Promise<DependencyToInstall> {
try {
const loc = require.resolve(path.join(dependency.package, 'package.json'), {
paths: [projectPath],
})
const pkg = await fs.readJson(loc) as PkgJson
if (!pkg.version) {
throw Error(`${pkg.version} for ${dependency.package} is not a valid semantic version.`)
}
const satisfied = Boolean(pkg.version && semver.satisfies(pkg.version, dependency.minVersion, {
includePrerelease: true,
}))
return {
dependency,
detectedVersion: pkg.version,
loc,
satisfied,
}
} catch (e) {
return {
dependency,
detectedVersion: null,
loc: null,
satisfied: false,
}
}
}
function getBundlerDependency (bundler: WizardBundler['type'], projectPath: string): Promise<DependencyToInstall> {
switch (bundler) {
case 'vite': return isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_VITE, projectPath)
case 'webpack': return isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_WEBPACK, projectPath)
default: throw Error(`Unknown bundler ${bundler}`)
}
}
export type WizardMountModule = Awaited<ReturnType<typeof WIZARD_FRAMEWORKS[number]['mountModule']>>
const mountModule = <T extends string>(mountModule: T) => (projectPath: string) => Promise.resolve(mountModule)
const reactMountModule = async (projectPath: string) => {
const reactPkg = await isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_REACT, projectPath)
if (!reactPkg.detectedVersion || !semver.valid(reactPkg.detectedVersion)) {
return 'cypress/react'
}
return semver.major(reactPkg.detectedVersion) === 18 ? 'cypress/react18' : 'cypress/react'
}
export const WIZARD_FRAMEWORKS = [
{
type: 'reactscripts',
configFramework: 'create-react-app',
category: 'template',
name: 'Create React App',
supportedBundlers: [dependencies.WIZARD_DEPENDENCY_WEBPACK],
detectors: [dependencies.WIZARD_DEPENDENCY_REACT_SCRIPTS],
dependencies: (bundler: WizardBundler['type'], projectPath: string): Promise<DependencyToInstall[]> => {
return Promise.all([
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_REACT_SCRIPTS, projectPath),
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_REACT_DOM, projectPath),
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_REACT, projectPath),
])
},
codeGenFramework: 'react',
glob: '*.{js,jsx,tsx}',
mountModule: reactMountModule,
supportStatus: 'full',
componentIndexHtml: componentIndexHtmlGenerator(),
},
{
type: 'vueclivue2',
configFramework: 'vue-cli',
category: 'template',
name: 'Vue CLI (Vue 2)',
detectors: [dependencies.WIZARD_DEPENDENCY_VUE_CLI_SERVICE, dependencies.WIZARD_DEPENDENCY_VUE_2],
supportedBundlers: [dependencies.WIZARD_DEPENDENCY_WEBPACK],
dependencies: (bundler: WizardBundler['type'], projectPath: string): Promise<DependencyToInstall[]> => {
return Promise.all([
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_VUE_CLI_SERVICE, projectPath),
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_VUE_2, projectPath),
])
},
codeGenFramework: 'vue',
glob: '*.vue',
mountModule: mountModule('cypress/vue2'),
supportStatus: 'full',
componentIndexHtml: componentIndexHtmlGenerator(),
},
{
type: 'vueclivue3',
configFramework: 'vue-cli',
category: 'template',
name: 'Vue CLI (Vue 3)',
supportedBundlers: [dependencies.WIZARD_DEPENDENCY_WEBPACK],
detectors: [dependencies.WIZARD_DEPENDENCY_VUE_CLI_SERVICE, dependencies.WIZARD_DEPENDENCY_VUE_3],
dependencies: (bundler: WizardBundler['type'], projectPath: string): Promise<DependencyToInstall[]> => {
return Promise.all([
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_VUE_CLI_SERVICE, projectPath),
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_VUE_3, projectPath),
])
},
codeGenFramework: 'vue',
glob: '*.vue',
mountModule: mountModule('cypress/vue'),
supportStatus: 'full',
componentIndexHtml: componentIndexHtmlGenerator(),
},
{
type: 'nextjs',
category: 'template',
configFramework: 'next',
name: 'Next.js',
detectors: [dependencies.WIZARD_DEPENDENCY_NEXT],
supportedBundlers: [dependencies.WIZARD_DEPENDENCY_WEBPACK],
dependencies: (bundler: WizardBundler['type'], projectPath: string): Promise<DependencyToInstall[]> => {
return Promise.all([
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_NEXT, projectPath),
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_REACT, projectPath),
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_REACT_DOM, projectPath),
])
},
codeGenFramework: 'react',
glob: '*.{js,jsx,tsx}',
mountModule: reactMountModule,
supportStatus: 'alpha',
/**
* Next.js uses style-loader to inject CSS and requires this element to exist in the HTML.
* @see: https://github.com/vercel/next.js/blob/5f3351dbb8de71bcdbc91d869c04bc862a25da5f/packages/next/build/webpack/config/blocks/css/loaders/client.ts#L24
*/
componentIndexHtml: componentIndexHtmlGenerator([
`<!-- Used by Next.js to inject CSS. -->\n`,
`<div id="__next_css__DO_NOT_USE__"></div>`,
].join(' '.repeat(8))),
},
{
type: 'nuxtjs',
configFramework: 'nuxt',
category: 'template',
name: 'Nuxt.js (v2)',
detectors: [dependencies.WIZARD_DEPENDENCY_NUXT],
supportedBundlers: [dependencies.WIZARD_DEPENDENCY_WEBPACK],
dependencies: (bundler: WizardBundler['type'], projectPath: string): Promise<DependencyToInstall[]> => {
return Promise.all([
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_NUXT, projectPath),
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_VUE_2, projectPath),
])
},
codeGenFramework: 'vue',
glob: '*.vue',
mountModule: mountModule('cypress/vue2'),
supportStatus: 'alpha',
componentIndexHtml: componentIndexHtmlGenerator(),
},
{
type: 'vue2',
configFramework: 'vue',
category: 'library',
name: 'Vue.js 2',
detectors: [dependencies.WIZARD_DEPENDENCY_VUE_2],
supportedBundlers: [dependencies.WIZARD_DEPENDENCY_WEBPACK, dependencies.WIZARD_DEPENDENCY_VITE],
dependencies: (bundler: WizardBundler['type'], projectPath: string): Promise<DependencyToInstall[]> => {
return Promise.all([
getBundlerDependency(bundler, projectPath),
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_VUE_2, projectPath),
])
},
codeGenFramework: 'vue',
glob: '*.vue',
mountModule: mountModule('cypress/vue2'),
supportStatus: 'full',
componentIndexHtml: componentIndexHtmlGenerator(),
},
{
type: 'vue3',
configFramework: 'vue',
category: 'library',
name: 'Vue.js 3',
detectors: [dependencies.WIZARD_DEPENDENCY_VUE_3],
supportedBundlers: [dependencies.WIZARD_DEPENDENCY_WEBPACK, dependencies.WIZARD_DEPENDENCY_VITE],
dependencies: (bundler: WizardBundler['type'], projectPath: string): Promise<DependencyToInstall[]> => {
return Promise.all([
getBundlerDependency(bundler, projectPath),
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_VUE_3, projectPath),
])
},
codeGenFramework: 'vue',
glob: '*.vue',
mountModule: mountModule('cypress/vue'),
supportStatus: 'full',
componentIndexHtml: componentIndexHtmlGenerator(),
},
{
type: 'react',
configFramework: 'react',
category: 'library',
name: 'React.js',
detectors: [dependencies.WIZARD_DEPENDENCY_REACT],
supportedBundlers: [dependencies.WIZARD_DEPENDENCY_WEBPACK, dependencies.WIZARD_DEPENDENCY_VITE],
dependencies: (bundler: WizardBundler['type'], projectPath: string): Promise<DependencyToInstall[]> => {
return Promise.all([
getBundlerDependency(bundler, projectPath),
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_REACT, projectPath),
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_REACT_DOM, projectPath),
])
},
codeGenFramework: 'react',
glob: '*.{js,jsx,tsx}',
mountModule: reactMountModule,
supportStatus: 'full',
componentIndexHtml: componentIndexHtmlGenerator(),
},
{
type: 'angular',
configFramework: 'angular',
category: 'template',
name: 'Angular',
detectors: [dependencies.WIZARD_DEPENDENCY_ANGULAR_CLI],
supportedBundlers: [dependencies.WIZARD_DEPENDENCY_WEBPACK],
dependencies: (bundler: WizardBundler['type'], projectPath: string): Promise<DependencyToInstall[]> => {
return Promise.all([
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_ANGULAR_CLI, projectPath),
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_ANGULAR_DEVKIT_BUILD_ANGULAR, projectPath),
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_ANGULAR_CORE, projectPath),
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_ANGULAR_COMMON, projectPath),
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_ANGULAR_PLATFORM_BROWSER_DYNAMIC, projectPath),
])
},
codeGenFramework: 'angular',
glob: '*.component.ts',
mountModule: mountModule('cypress/angular'),
supportStatus: 'full',
componentIndexHtml: componentIndexHtmlGenerator(),
specPattern: '**/*.cy.ts',
},
{
type: 'svelte',
configFramework: 'svelte',
category: 'library',
name: 'Svelte.js',
detectors: [dependencies.WIZARD_DEPENDENCY_SVELTE],
supportedBundlers: [dependencies.WIZARD_DEPENDENCY_WEBPACK, dependencies.WIZARD_DEPENDENCY_VITE],
dependencies: (bundler: WizardBundler['type'], projectPath: string): Promise<DependencyToInstall[]> => {
return Promise.all([
getBundlerDependency(bundler, projectPath),
isDependencyInstalled(dependencies.WIZARD_DEPENDENCY_SVELTE, projectPath),
])
},
codeGenFramework: 'svelte',
glob: '*.svelte',
mountModule: mountModule('cypress/svelte'),
supportStatus: 'alpha',
componentIndexHtml: componentIndexHtmlGenerator(),
},
] as const