Skip to content

feat(@angular-devkit/architect): merge object options from CLI #28398

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

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/angular_devkit/architect/src/architect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
SimpleScheduler,
createJobHandler,
} from './jobs';
import { mergeOptions } from './options';
import { scheduleByName, scheduleByTarget } from './schedule-by-name';

const inputSchema = require('./input-schema.json');
Expand All @@ -71,10 +72,7 @@ function _createJobHandlerFromBuilderInfo(
concatMap(async (message) => {
if (message.kind === JobInboundMessageKind.Input) {
const v = message.value as BuilderInput;
const options = {
...baseOptions,
...v.options,
};
const options = mergeOptions(baseOptions, v.options);

// Validate v against the options schema.
const validation = await registry.compile(info.optionSchema);
Expand Down
39 changes: 39 additions & 0 deletions packages/angular_devkit/architect/src/options.ts
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;
}
70 changes: 70 additions & 0 deletions packages/angular_devkit/architect/src/options_spec.ts
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,
},
});
});
});