forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@angular-devkit/architect): merge object options from CLI
We recently introduced the ability to pass object values from the command line (angular#28362). @clydin noticed that the initial behavior didn't work well for `--define`: It completely replaced all values even if just one of multiple defines is specified. This updates the architect to support merging of object options. If both the base option (e.g. from `angular.json`) and the override (e.g. from a CLI `--flag`) are objects, the objects are merged. See: angular#28362
- Loading branch information
Showing
3 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
import { json } from '@angular-devkit/core'; | ||
|
||
import { BuilderInput } from './api'; | ||
|
||
type OverrideOptions = BuilderInput['options']; | ||
|
||
export function mergeOptions( | ||
baseOptions: json.JsonObject, | ||
overrideOptions: OverrideOptions, | ||
): json.JsonObject { | ||
if (!overrideOptions) { | ||
return { ...baseOptions }; | ||
} | ||
|
||
const options = { | ||
...baseOptions, | ||
...overrideOptions, | ||
}; | ||
|
||
// For object-object overrides, we merge one layer deep. | ||
for (const key of Object.keys(overrideOptions)) { | ||
const override = overrideOptions[key]; | ||
const base = baseOptions[key]; | ||
|
||
if (json.isJsonObject(base) && json.isJsonObject(override)) { | ||
options[key] = { ...base, ...override }; | ||
} | ||
} | ||
|
||
return options; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
import { mergeOptions } from './options'; | ||
|
||
describe('mergeOptions', () => { | ||
it('overwrites literal values', () => { | ||
expect( | ||
mergeOptions( | ||
{ | ||
onlyBase: 'base', | ||
a: 'foo', | ||
b: 42, | ||
c: true, | ||
}, | ||
{ | ||
onlyOverride: 'override', | ||
a: 'bar', | ||
b: 43, | ||
c: false, | ||
}, | ||
), | ||
).toEqual({ | ||
onlyBase: 'base', | ||
a: 'bar', | ||
b: 43, | ||
c: false, | ||
onlyOverride: 'override', | ||
}); | ||
}); | ||
|
||
it('merges object values one layer deep', () => { | ||
expect( | ||
mergeOptions( | ||
{ | ||
obj: { | ||
nested: { | ||
fromBase: true, | ||
}, | ||
fromBase: true, | ||
overridden: false, | ||
}, | ||
}, | ||
{ | ||
obj: { | ||
nested: { | ||
fromOverride: true, | ||
}, | ||
overridden: true, | ||
fromOverride: true, | ||
}, | ||
}, | ||
), | ||
).toEqual({ | ||
obj: { | ||
nested: { | ||
fromOverride: true, | ||
}, | ||
fromBase: true, | ||
overridden: true, | ||
fromOverride: true, | ||
}, | ||
}); | ||
}); | ||
}); |