-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: rework component onboarding in launchpad #25713
Changes from 29 commits
b920973
9af7df5
b0e6a54
8685358
7f46405
bd446d7
169fd2d
9a58381
fe5c399
04eff95
c9dc13b
57a8c33
9582e8d
aa03f22
55eeadf
2c722c3
6c73ff5
7d3535f
2ae85c0
b8ff1a0
e9257c7
681294a
08dba6b
1d959ae
3dbd6c1
54fee26
b441be4
57c2ab9
bf884c7
33feea4
c8087c1
270e4e7
5060900
6d6d718
29c8b45
08b705a
82fe744
da254fd
988142c
45bbd45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3244,6 +3244,162 @@ declare namespace Cypress { | |
|
||
type PickConfigOpt<T> = T extends keyof DefineDevServerConfig ? DefineDevServerConfig[T] : any | ||
|
||
interface DependencyToInstall { | ||
dependency: CypressComponentDependency | ||
satisfied: boolean | ||
loc: string | null | ||
detectedVersion: string | null | ||
} | ||
|
||
interface CypressComponentDependency { | ||
/** | ||
* Unique idenitifer. | ||
* @example 'reactscripts' | ||
*/ | ||
type: string | ||
|
||
/** | ||
* Name to display in the user interface. | ||
* @example "React Scripts" | ||
*/ | ||
name: string | ||
|
||
/** | ||
* Package name on npm. | ||
* @example react-scripts | ||
*/ | ||
package: string | ||
|
||
/** | ||
* Code to run when installing. Version is optional. | ||
* | ||
* Should be <package_name>@<version>. | ||
* | ||
* @example `react` | ||
* @example `react@18` | ||
* @example `react-scripts` | ||
*/ | ||
installer: string | ||
|
||
/** | ||
* Description shown in UI. It is recommended to use the same one the package uses on npm. | ||
* @example 'Create React apps with no build configuration' | ||
*/ | ||
description: string | ||
|
||
/** | ||
* Minimum version supported. Should conform to Semantic Versioning as used in `package.json`. | ||
* @see https://docs.npmjs.com/cli/v9/configuring-npm/package-json#dependencies | ||
* @example '^=4.0.0 || ^=5.0.0' | ||
* @example '^2.0.0' | ||
*/ | ||
minVersion: string | ||
} | ||
|
||
interface ResolvedComponentFrameworkDefinition { | ||
/** | ||
* A semantic, unique identifier. Must begin with `cypress-ct-` for third party implementations. | ||
* @example: 'reactscripts', 'nextjs', 'cypress-ct-solid-js' | ||
*/ | ||
type: string | ||
|
||
/** | ||
* Used as the flag for `getPreset` for meta framworks, such as finding the webpack config for CRA, Angular, etc. | ||
* | ||
* configFramwork: () => { | ||
* return getSolidJsMetaFrameworkBundlerConfig() | ||
* } | ||
* It is also the name of the string added to `cypress.config` | ||
* | ||
* @example | ||
* | ||
* export default { | ||
* component: { | ||
* devServer: { | ||
* framework: 'create-react-app' // can be 'next', 'create-react-app', etc etc. | ||
* } | ||
* } | ||
* } | ||
*/ | ||
configFramework: string | ||
|
||
/** | ||
* Library (React, Vue) or template (aka "meta framework") (CRA, Next.js, Angular) | ||
*/ | ||
category: 'library' | 'template' | ||
|
||
/** | ||
* Name displayed in Launchpad when doing initial setup. | ||
* @example 'Solid.js', 'Create React App' | ||
*/ | ||
name: string | ||
|
||
/** | ||
* Supported bundlers. | ||
*/ | ||
supportedBundlers: Array<'webpack' | 'vite'> | ||
|
||
/** | ||
* Used to attempt to automatically select the correct framework/bundler from the dropdown. | ||
* @example | ||
* | ||
* const SOLID_DETECTOR: Dependency = { | ||
* type: 'solid', | ||
* name: 'Solid.js', | ||
* package: 'solid-js', | ||
* installer: 'solid-js', | ||
* description: 'Solid is a declarative JavaScript library for creating user interfaces', | ||
* minVersion: '^1.0.0', | ||
* } | ||
*/ | ||
detectors: CypressComponentDependency[] | ||
|
||
/** | ||
* Array of required dependencies. This could be the bundler and JavaScript library. | ||
* It's the same type as `detectors`. | ||
*/ | ||
dependencies: (bundler: 'webpack' | 'vite', projectPath: string) => Promise<DependencyToInstall[]> | ||
|
||
/** | ||
* @internal | ||
* This is used interally by Cypress for the "Create From Component" feature. | ||
*/ | ||
codeGenFramework?: 'react' | 'vue' | 'svelte' | 'angular' | ||
|
||
/** | ||
* @internal | ||
* This is used interally by Cypress for the "Create From Component" feature. | ||
* @example '*.{js,jsx,tsx}' | ||
*/ | ||
glob?: string | ||
|
||
/** | ||
* This is the path to get mount, eg `import { mount } from <mount_module>, | ||
* @example: `cypress-ct-solidjs/src/mount` | ||
*/ | ||
mountModule: (projectPath: string) => Promise<string> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mentioned this in the tech-brief but I'm curious what you think of making the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, I was trying to make the APIs as similar as possible but it's probably not worth it - better to opt for some internal complexity and expose a nicer public API There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need Since we assume it's named export from the default entry point, we can assume |
||
|
||
/** | ||
* Support status. Internally alpha | beta | full. | ||
* Community integrations are "community". | ||
* @internal | ||
*/ | ||
supportStatus: 'alpha' | 'beta' | 'full' | 'community' | ||
|
||
/** | ||
* Function returning string for used for the component-index.html file. | ||
* Cypress provides a default if one isn't specified for third party integrations. | ||
*/ | ||
componentIndexHtml?: () => string | ||
|
||
/** | ||
* Used for the Create From Comopnent feature. | ||
* This is currently not supported for third party frameworks. | ||
* @internal | ||
*/ | ||
specPattern?: '**/*.cy.ts' | ||
} | ||
|
||
interface AngularDevServerProjectConfig { | ||
root: string | ||
sourceRoot: string | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ import type { DataContext } from '..' | |
import { SpecOptions, codeGenerator } from '../codegen' | ||
import templates from '../codegen/templates' | ||
import type { CodeGenType } from '../gen/graphcache-config.gen' | ||
import { WizardFrontendFramework, WIZARD_FRAMEWORKS } from '@packages/scaffold-config' | ||
import { parse as parseReactComponent, resolver as reactDocgenResolvers } from 'react-docgen' | ||
import { visit } from 'ast-types' | ||
|
||
|
@@ -152,7 +151,7 @@ export class CodegenActions { | |
}) | ||
} | ||
|
||
getWizardFrameworkFromConfig (): WizardFrontendFramework | undefined { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general I think we should move away from naming things "Wizard". 🧙 are pretty cool, but it's probably a confusing, considering these are used throughout the entire app, not just in the Launchpad wizard. We should just call them There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree Wizards are pretty cool |
||
getWizardFrameworkFromConfig (): Cypress.ResolvedComponentFrameworkDefinition | undefined { | ||
const config = this.ctx.lifecycleManager.loadedConfigFile | ||
|
||
// If devServer is a function, they are using a custom dev server. | ||
|
@@ -161,7 +160,7 @@ export class CodegenActions { | |
} | ||
|
||
// @ts-ignore - because of the conditional above, we know that devServer isn't a function | ||
return WIZARD_FRAMEWORKS.find((framework) => framework.configFramework === config?.component?.devServer.framework) | ||
return this.ctx.coreData.wizard.frameworks.find((framework) => framework.configFramework === config?.component?.devServer.framework) | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import type { NexusGenObjects } from '@packages/graphql/src/gen/nxs.gen' | ||
import { detectFramework, WIZARD_FRAMEWORKS, commandsFileBody, supportFileComponent, supportFileE2E, WizardBundler, WizardFrontendFramework } from '@packages/scaffold-config' | ||
import { detectFramework, commandsFileBody, supportFileComponent, supportFileE2E, getBundler } from '@packages/scaffold-config' | ||
import assert from 'assert' | ||
import path from 'path' | ||
import Debug from 'debug' | ||
|
@@ -9,6 +9,7 @@ const debug = Debug('cypress:data-context:wizard-actions') | |
|
||
import type { DataContext } from '..' | ||
import { addTestingTypeToCypressConfig, AddTestingTypeToCypressConfigOptions } from '@packages/config' | ||
import componentIndexHtmlGenerator from '@packages/scaffold-config/src/component-index-template' | ||
|
||
export class WizardActions { | ||
constructor (private ctx: DataContext) {} | ||
|
@@ -23,14 +24,14 @@ export class WizardActions { | |
return this.ctx.wizardData | ||
} | ||
|
||
setFramework (framework: WizardFrontendFramework | null): void { | ||
const next = WIZARD_FRAMEWORKS.find((x) => x.type === framework?.type) | ||
setFramework (framework: Cypress.ResolvedComponentFrameworkDefinition | null): void { | ||
const next = this.ctx.coreData.wizard.frameworks.find((x) => x.type === framework?.type) | ||
|
||
this.ctx.update((coreData) => { | ||
coreData.wizard.chosenFramework = framework | ||
}) | ||
|
||
if (next?.supportedBundlers?.length === 1) { | ||
if (next?.supportedBundlers?.[0] && next.supportedBundlers.length === 1) { | ||
this.setBundler(next?.supportedBundlers?.[0]) | ||
|
||
return | ||
|
@@ -41,7 +42,7 @@ export class WizardActions { | |
// if the previous bundler was incompatible with the | ||
// new framework that was selected, we need to reset it | ||
const doesNotSupportChosenBundler = (chosenBundler && !new Set( | ||
this.ctx.coreData.wizard.chosenFramework?.supportedBundlers.map((x) => x.type) || [], | ||
this.ctx.coreData.wizard.chosenFramework?.supportedBundlers ?? [], | ||
).has(chosenBundler.type)) ?? false | ||
|
||
const prevFramework = this.ctx.coreData.wizard.chosenFramework?.type ?? null | ||
|
@@ -51,9 +52,21 @@ export class WizardActions { | |
} | ||
} | ||
|
||
setBundler (bundler: WizardBundler | null) { | ||
getNullableBundler (bundler: 'vite' | 'webpack' | null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the reason for all the change around export const WIZARD_DEPENDENCY_WEBPACK = {
type: 'webpack',
name: 'Webpack',
package: 'webpack',
installer: 'webpack',
description: 'Webpack is a module bundler',
minVersion: '^=4.0.0 || ^=5.0.0',
}
supportedBundlers: [WIZARD_DEPENDENCY_WEBPACK] But for a third party to do this, we'd need to expose the bundler: 'webpack' I reworked the internal API (which is also now the public API) to use a string, Now, it's just supportedBundlers: ['webpack'] It's way more ergonomic, and mirrors how |
||
if (!bundler) { | ||
return null | ||
} | ||
|
||
try { | ||
return getBundler(bundler) | ||
} catch (e) { | ||
return null | ||
} | ||
} | ||
|
||
setBundler (bundler: 'vite' | 'webpack' | null) { | ||
this.ctx.update((coreData) => { | ||
coreData.wizard.chosenBundler = bundler | ||
coreData.wizard.chosenBundler = this.getNullableBundler(bundler) | ||
}) | ||
|
||
return this.ctx.coreData.wizard | ||
|
@@ -87,7 +100,7 @@ export class WizardActions { | |
|
||
this.resetWizard() | ||
|
||
const detected = await detectFramework(this.ctx.currentProject) | ||
const detected = await detectFramework(this.ctx.currentProject, this.ctx.coreData.wizard.frameworks) | ||
|
||
debug('detected %o', detected) | ||
|
||
|
@@ -100,8 +113,8 @@ export class WizardActions { | |
return | ||
} | ||
|
||
coreData.wizard.detectedBundler = detected.bundler || detected.framework.supportedBundlers[0] | ||
coreData.wizard.chosenBundler = detected.bundler || detected.framework.supportedBundlers[0] | ||
coreData.wizard.detectedBundler = this.getNullableBundler(detected.bundler || detected.framework.supportedBundlers[0]) | ||
coreData.wizard.chosenBundler = this.getNullableBundler(detected.bundler || detected.framework.supportedBundlers[0]) | ||
}) | ||
} | ||
} | ||
|
@@ -295,14 +308,16 @@ export class WizardActions { | |
} | ||
} | ||
|
||
private async scaffoldComponentIndexHtml (chosenFramework: WizardFrontendFramework): Promise<NexusGenObjects['ScaffoldedFile']> { | ||
private async scaffoldComponentIndexHtml (chosenFramework: Cypress.ResolvedComponentFrameworkDefinition): Promise<NexusGenObjects['ScaffoldedFile']> { | ||
const componentIndexHtmlPath = path.join(this.projectRoot, 'cypress', 'support', 'component-index.html') | ||
|
||
await this.ensureDir('support') | ||
|
||
const defaultComponentIndex = componentIndexHtmlGenerator('') | ||
mike-plummer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return this.scaffoldFile( | ||
componentIndexHtmlPath, | ||
chosenFramework.componentIndexHtml(), | ||
chosenFramework.componentIndexHtml?.() ?? defaultComponentIndex(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason this is now nullable is since third parties CAN provide a custom |
||
'The HTML wrapper that each component is served with. Used for global fonts, CSS, JS, HTML, etc.', | ||
) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ import { CypressEnv } from './CypressEnv' | |
import { autoBindDebug } from '../util/autoBindDebug' | ||
import type { EventRegistrar } from './EventRegistrar' | ||
import type { DataContext } from '../DataContext' | ||
import { DependencyToInstall, isDependencyInstalled, WIZARD_BUNDLERS, WIZARD_DEPENDENCIES, WIZARD_FRAMEWORKS } from '@packages/scaffold-config' | ||
import { isDependencyInstalled, WIZARD_BUNDLERS } from '@packages/scaffold-config' | ||
|
||
const debug = debugLib(`cypress:lifecycle:ProjectConfigManager`) | ||
|
||
|
@@ -191,14 +191,19 @@ export class ProjectConfigManager { | |
|
||
// Use a map since sometimes the same dependency can appear in `bundler` and `framework`, | ||
// for example webpack appears in both `bundler: 'webpack', framework: 'react-scripts'` | ||
const unsupportedDeps = new Map<DependencyToInstall['dependency']['type'], DependencyToInstall>() | ||
const unsupportedDeps = new Map<Cypress.DependencyToInstall['dependency']['type'], Cypress.DependencyToInstall>() | ||
|
||
if (!bundler) { | ||
return | ||
} | ||
|
||
const isFrameworkSatisfied = async (bundler: typeof WIZARD_BUNDLERS[number], framework: typeof WIZARD_FRAMEWORKS[number]) => { | ||
for (const dep of await (framework.dependencies(bundler.type, this.options.projectRoot))) { | ||
const isFrameworkSatisfied = async (bundler: typeof WIZARD_BUNDLERS[number], framework: Cypress.ResolvedComponentFrameworkDefinition) => { | ||
const deps = await (framework.dependencies(bundler.type, this.options.projectRoot)) | ||
mike-plummer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
debug('deps are %o', deps) | ||
|
||
for (const dep of deps) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could do this in a bit more compact fashion with
Automatically takes care of ejecting from the loop once when it hits the first There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless I'm doing something weird I don't think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ugh, totally glossed over the async nature here. Oops. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still likely possible if we really do not like const installed = await Promise.all(deps.map(async (dep) => {
debug('detecting %s in %s', dep.dependency.name, this.options.projectRoot)
const res = await isDependencyInstalled(dep.dependency, this.options.projectRoot)
return res.satisfied
}))
const result = installed.every(Boolean)
|
||
debug('detecting %s in %s', dep.dependency.name, this.options.projectRoot) | ||
const res = await isDependencyInstalled(dep.dependency, this.options.projectRoot) | ||
|
||
if (!res.satisfied) { | ||
|
@@ -209,9 +214,9 @@ export class ProjectConfigManager { | |
return true | ||
} | ||
|
||
const frameworks = WIZARD_FRAMEWORKS.filter((x) => x.configFramework === devServerOptions.framework) | ||
const frameworks = this.options.ctx.coreData.wizard.frameworks.filter((x) => x.configFramework === devServerOptions.framework) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are a couple places we're filtering through this |
||
|
||
const mismatchedFrameworkDeps = new Map<typeof WIZARD_DEPENDENCIES[number]['type'], DependencyToInstall>() | ||
const mismatchedFrameworkDeps = new Map<string, Cypress.DependencyToInstall>() | ||
|
||
let isSatisfied = false | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we say the
type
must start withcypress-ct-
, but theconfigFramework
doesn't have this restriction even though inside ofwebpack-dev-server/src/devServer.ts
we expect it to also have this same restriction. Looks like they should be the same or at least both should share thecypress-ct-
prefixThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm good point, maybe we just expose
name
in the public API and for third parties, inprocessFrameworkDefinition
, doThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So
configFramework
will be@private
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done