@@ -3,6 +3,7 @@ import path from 'path'
33import { fork } from 'child_process'
44import type { ForkOptions } from 'child_process'
55import assert from 'assert'
6+ import _ from 'lodash'
67import type { DataContext } from '..'
78import {
89 cleanUpIntegrationFolder ,
@@ -23,10 +24,45 @@ import {
2324 getComponentFolder ,
2425 getIntegrationTestFilesGlobs ,
2526 getSpecPattern ,
27+ legacyOptions ,
2628} from '../sources/migration'
2729import { makeCoreData } from '../data'
2830import { LegacyPluginsIpc } from '../data/LegacyPluginsIpc'
2931
32+ export function getConfigWithDefaults ( legacyConfig : any ) {
33+ const newConfig = _ . cloneDeep ( legacyConfig )
34+
35+ legacyOptions . forEach ( ( { defaultValue, name } ) => {
36+ if ( defaultValue !== undefined && legacyConfig [ name ] === undefined ) {
37+ newConfig [ name ] = typeof defaultValue === 'function' ? defaultValue ( ) : defaultValue
38+ }
39+ } )
40+
41+ return newConfig
42+ }
43+
44+ export function getDiff ( oldConfig : any , newConfig : any ) {
45+ // get all the values updated
46+ const result : any = _ . reduce ( oldConfig , ( acc : any , value , key ) => {
47+ // ignore values that have been removed
48+ if ( newConfig [ key ] && ! _ . isEqual ( value , newConfig [ key ] ) ) {
49+ acc [ key ] = newConfig [ key ]
50+ }
51+
52+ return acc
53+ } , { } )
54+
55+ // get all the values added
56+ return _ . reduce ( newConfig , ( acc : any , value , key ) => {
57+ // their key is in the new config but not in the old config
58+ if ( ! oldConfig . hasOwnProperty ( key ) ) {
59+ acc [ key ] = value
60+ }
61+
62+ return acc
63+ } , result )
64+ }
65+
3066export async function processConfigViaLegacyPlugins ( projectRoot : string , legacyConfig : LegacyCypressConfigJson ) : Promise < LegacyCypressConfigJson > {
3167 const pluginFile = legacyConfig . pluginsFile ?? await tryGetDefaultLegacyPluginsFile ( projectRoot )
3268
@@ -49,12 +85,23 @@ export async function processConfigViaLegacyPlugins (projectRoot: string, legacy
4985 const CHILD_PROCESS_FILE_PATH = require . resolve ( '@packages/server/lib/plugins/child/require_async_child' )
5086 const ipc = new LegacyPluginsIpc ( fork ( CHILD_PROCESS_FILE_PATH , configProcessArgs , childOptions ) )
5187
88+ const legacyConfigWithDefaults = getConfigWithDefaults ( legacyConfig )
89+
5290 ipc . on ( 'ready' , ( ) => {
53- ipc . send ( 'loadLegacyPlugins' , legacyConfig )
91+ ipc . send ( 'loadLegacyPlugins' , legacyConfigWithDefaults )
5492 } )
5593
5694 ipc . on ( 'loadLegacyPlugins:reply' , ( modifiedLegacyConfig ) => {
57- resolve ( modifiedLegacyConfig )
95+ const diff = getDiff ( legacyConfigWithDefaults , modifiedLegacyConfig )
96+
97+ // if env is updated by plugins, avoid adding it to the config file
98+ if ( diff . env ) {
99+ delete diff . env
100+ }
101+
102+ const legacyConfigWithChanges = _ . merge ( legacyConfig , diff )
103+
104+ resolve ( legacyConfigWithChanges )
58105 ipc . childProcess . kill ( )
59106 } )
60107
0 commit comments