Fix scaffolding

This commit is contained in:
Ryan Manuel
2022-03-26 20:46:28 -05:00
parent 5e9991d6d8
commit a03e700ac7
4 changed files with 48 additions and 38 deletions

View File

@@ -435,6 +435,7 @@ export class DataContext {
await this.initializeOpenMode()
if (this.coreData.currentTestingType && await this.lifecycleManager.waitForInitializeSuccess()) {
this.lifecycleManager.setCurrentTestingType(this.coreData.currentTestingType)
this.lifecycleManager.scaffoldFilesIfNecessary()
}
} else {
throw new Error(`Missing DataContext config "mode" setting, expected run | open`)

View File

@@ -12,6 +12,7 @@ import chokidar from 'chokidar'
import { validate as validateConfig, validateNoBreakingConfigLaunchpad, validateNoBreakingConfigRoot, validateNoBreakingTestingTypeConfig } from '@packages/config'
import type { SetupFullConfigOptions } from './ProjectLifecycleManager'
import { CypressEnv } from './CypressEnv'
import { autoBindDebug } from '../util/autoBindDebug'
const debug = debugLib(`cypress:lifecycle:ProjectConfigManager`)
@@ -63,12 +64,11 @@ export class ProjectConfigManager {
private _state: ConfigManagerState = 'pending'
private _loadConfigPromise: Promise<LoadConfigReply> | undefined
private _cachedLoadConfig: LoadConfigReply | undefined
#cypressEnv: CypressEnv
private _cypressEnv: CypressEnv
constructor (private options: ProjectConfigManagerOptions) {
this.#cypressEnv = new CypressEnv({
envFilePath: this.#envFilePath,
this._cypressEnv = new CypressEnv({
envFilePath: this.envFilePath,
validateConfigFile: (filePath, config) => {
this.validateConfigFile(filePath, config)
},
@@ -76,6 +76,8 @@ export class ProjectConfigManager {
this.options.toLaunchpad(...args)
},
})
return autoBindDebug(this)
}
get isLoadingNodeEvents () {
@@ -104,11 +106,11 @@ export class ProjectConfigManager {
this._testingType = testingType
}
get #envFilePath () {
private get envFilePath () {
return path.join(this.options.projectRoot, 'cypress.env.json')
}
get #loadedConfigFile (): Partial<Cypress.ConfigOptions> | null {
private get loadedConfigFile (): Partial<Cypress.ConfigOptions> | null {
return this._cachedLoadConfig?.initialConfig ?? null
}
@@ -670,12 +672,12 @@ export class ProjectConfigManager {
}
async loadCypressEnvFile () {
return this.#cypressEnv.loadCypressEnvFile()
return this._cypressEnv.loadCypressEnvFile()
}
async reloadCypressEnvFile () {
this.#cypressEnv = new CypressEnv({
envFilePath: this.#envFilePath,
this._cypressEnv = new CypressEnv({
envFilePath: this.envFilePath,
validateConfigFile: (filePath, config) => {
this.validateConfigFile(filePath, config)
},
@@ -684,11 +686,11 @@ export class ProjectConfigManager {
},
})
return this.#cypressEnv.loadCypressEnvFile()
return this._cypressEnv.loadCypressEnvFile()
}
isTestingTypeConfigured (testingType: TestingType): boolean {
const config = this.#loadedConfigFile
const config = this.loadedConfigFile
if (!config) {
return false
@@ -729,7 +731,7 @@ export class ProjectConfigManager {
this.options.onWarning(getError('UNEXPECTED_INTERNAL_ERROR', err))
})
const cypressEnvFileWatcher = this.addWatcher(this.#envFilePath)
const cypressEnvFileWatcher = this.addWatcher(this.envFilePath)
cypressEnvFileWatcher.on('all', () => {
this.reloadConfig().catch(this.onLoadError)

View File

@@ -79,9 +79,8 @@ export class ProjectLifecycleManager {
private _configManager: ProjectConfigManager | undefined
private _projectMetaState: ProjectMetaState = { ...PROJECT_META_STATE }
private _pendingInitialize?: pDefer.DeferredPromise<FullConfig>
#cachedInitialConfig: Cypress.ConfigOptions | undefined
#cachedFullConfig: FullConfig | undefined
private _cachedInitialConfig: Cypress.ConfigOptions | undefined
private _cachedFullConfig: FullConfig | undefined
constructor (private ctx: DataContext) {
if (ctx.coreData.currentProject) {
@@ -166,11 +165,11 @@ export class ProjectLifecycleManager {
}
get loadedConfigFile (): Partial<Cypress.ConfigOptions> | null {
return this.#cachedInitialConfig ?? null
return this._cachedInitialConfig ?? null
}
get loadedFullConfig (): FullConfig | null {
return this.#cachedFullConfig ?? null
return this._cachedFullConfig ?? null
}
get projectRoot () {
@@ -214,7 +213,7 @@ export class ProjectLifecycleManager {
return 'npm'
}
#createConfigManager () {
private createConfigManager () {
return new ProjectConfigManager({
configFile: this.configFile,
projectRoot: this.projectRoot,
@@ -238,26 +237,19 @@ export class ProjectLifecycleManager {
this.ctx.emitter.toLaunchpad(...args)
},
onInitialConfigLoaded: (initialConfig: Cypress.ConfigOptions) => {
this.#cachedInitialConfig = initialConfig
if (this._currentTestingType) {
if (!this.isTestingTypeConfigured(this._currentTestingType) && !this.ctx.isRunMode) {
this.ctx.actions.wizard.scaffoldTestingType().catch(this.onLoadError)
this._cachedInitialConfig = initialConfig
return
}
if (this.ctx.coreData.scaffoldedFiles) {
this.ctx.coreData.scaffoldedFiles.filter((f) => {
if (f.file.absolute === this.configFilePath && f.status !== 'valid') {
f.status = 'valid'
this.ctx.emitter.toLaunchpad()
}
})
}
if (this.ctx.coreData.scaffoldedFiles) {
this.ctx.coreData.scaffoldedFiles.filter((f) => {
if (f.file.absolute === this.configFilePath && f.status !== 'valid') {
f.status = 'valid'
this.ctx.emitter.toLaunchpad()
}
})
}
},
onFinalConfigLoaded: async (finalConfig: FullConfig) => {
this.#cachedFullConfig = finalConfig
this._cachedFullConfig = finalConfig
if (this.ctx.coreData.cliBrowser) {
await this.setActiveBrowser(this.ctx.coreData.cliBrowser)
@@ -343,14 +335,14 @@ export class ProjectLifecycleManager {
this.resetInternalState()
this._configManager = this.#createConfigManager()
this._configManager = this.createConfigManager()
const { needsCypressJsonMigration } = this.refreshMetaState()
const legacyConfigPath = path.join(projectRoot, this.legacyConfigFile)
if (needsCypressJsonMigration && !this.ctx.isRunMode && this.ctx.fs.existsSync(legacyConfigPath)) {
this.#legacyMigration(legacyConfigPath).catch(this.onLoadError)
this.legacyMigration(legacyConfigPath).catch(this.onLoadError)
return
}
@@ -374,7 +366,7 @@ export class ProjectLifecycleManager {
this.loadCypressEnvFile().catch(this.onLoadError)
}
async #legacyMigration (legacyConfigPath: string) {
async legacyMigration (legacyConfigPath: string) {
try {
// we run the legacy plugins/index.js in a child process
// and mutate the config based on the return value for migration
@@ -426,6 +418,12 @@ export class ProjectLifecycleManager {
}
}
scaffoldFilesIfNecessary () {
if (this._currentTestingType && !this.isTestingTypeConfigured(this._currentTestingType) && !this.ctx.isRunMode) {
this.ctx.actions.wizard.scaffoldTestingType().catch(this.onLoadError)
}
}
private resetInternalState () {
if (this._configManager) {
this._configManager.destroy()
@@ -434,7 +432,8 @@ export class ProjectLifecycleManager {
this.ctx.project.destroy()
this._currentTestingType = null
this.#cachedInitialConfig = undefined
this._cachedInitialConfig = undefined
this._cachedFullConfig = undefined
}
/**

View File

@@ -173,6 +173,10 @@ export const mutation = mutationType({
if (ctx.coreData.currentTestingType
&& !ctx.lifecycleManager.isTestingTypeConfigured(ctx.coreData.currentTestingType)) {
await ctx.actions.wizard.initialize()
if (ctx.wizardData.chosenLanguage === 'ts') {
ctx.lifecycleManager.scaffoldFilesIfNecessary()
}
}
return {}
@@ -615,6 +619,8 @@ export const mutation = mutationType({
ctx.actions.project.setCurrentTestingType(args.testingType)
await ctx.actions.project.reconfigureProject()
// TODO: do i need to scaffold here too?
return true
},
})
@@ -634,6 +640,8 @@ export const mutation = mutationType({
await ctx.actions.project.reconfigureProject()
}
// TODO: do i need to scaffold here too?
return true
},
})