Skip to content

Commit

Permalink
feat(angular): add "define" and "clearScreen" options to application …
Browse files Browse the repository at this point in the history
…executor (#21751)
  • Loading branch information
leosvelperez committed Feb 9, 2024
1 parent 96abc26 commit 1ba7662
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 55 deletions.
10 changes: 10 additions & 0 deletions docs/generated/packages/angular/executors/application.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@
"items": { "type": "string" },
"default": []
},
"clearScreen": {
"type": "boolean",
"default": false,
"description": "Automatically clear the terminal screen during rebuilds. _Note: this is only supported in Angular versions >= 17.2.0_."
},
"optimization": {
"description": "Enables optimization of the build output. Including minification of scripts and styles, tree-shaking, dead-code elimination, inlining of critical CSS and fonts inlining. For more information, see https://angular.io/guide/workspace-config#optimization-configuration.",
"default": true,
Expand Down Expand Up @@ -234,6 +239,11 @@
"^\\.\\S+$": { "enum": ["text", "binary", "file", "empty"] }
}
},
"define": {
"description": "Defines global identifiers that will be replaced with a specified constant value when found in any JavaScript or TypeScript code including libraries. The value will be used directly. String values must be put in quotes. Identifiers within Angular metadata such as Component Decorators will not be replaced. _Note: this is only supported in Angular versions >= 17.2.0_.",
"type": "object",
"additionalProperties": { "type": "string" }
},
"fileReplacements": {
"description": "Replace compilation source files with other compilation source files in the build.",
"type": "array",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default async function* applicationExecutor(
const {
buildLibsFromSource = true,
plugins: pluginPaths,
indexHtmlTransformer: indexHtmlTransformerPath,
...delegateExecutorOptions
} = options;

Expand All @@ -36,11 +37,8 @@ export default async function* applicationExecutor(
}

const plugins = await loadPlugins(pluginPaths, options.tsConfig);
const indexHtmlTransformer = options.indexHtmlTransformer
? await loadIndexHtmlTransformer(
options.indexHtmlTransformer,
options.tsConfig
)
const indexHtmlTransformer = indexHtmlTransformerPath
? await loadIndexHtmlTransformer(indexHtmlTransformerPath, options.tsConfig)
: undefined;

const { buildApplication } = await import('@angular-devkit/build-angular');
Expand Down
12 changes: 12 additions & 0 deletions packages/angular/src/executors/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@
},
"default": []
},
"clearScreen": {
"type": "boolean",
"default": false,
"description": "Automatically clear the terminal screen during rebuilds. _Note: this is only supported in Angular versions >= 17.2.0_."
},
"optimization": {
"description": "Enables optimization of the build output. Including minification of scripts and styles, tree-shaking, dead-code elimination, inlining of critical CSS and fonts inlining. For more information, see https://angular.io/guide/workspace-config#optimization-configuration.",
"default": true,
Expand Down Expand Up @@ -213,6 +218,13 @@
"^\\.\\S+$": { "enum": ["text", "binary", "file", "empty"] }
}
},
"define": {
"description": "Defines global identifiers that will be replaced with a specified constant value when found in any JavaScript or TypeScript code including libraries. The value will be used directly. String values must be put in quotes. Identifiers within Angular metadata such as Component Decorators will not be replaced. _Note: this is only supported in Angular versions >= 17.2.0_.",
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"fileReplacements": {
"description": "Replace compilation source files with other compilation source files in the build.",
"type": "array",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,62 @@ export function validateOptions(options: ApplicationExecutorOptions): void {
);
}

if (options.loader && lt(angularVersion, '17.1.0')) {
throw new Error(
`The "loader" option requires Angular version 17.1.0 or greater. You are currently using version ${angularVersion}.`
);
}
if (lt(angularVersion, '17.1.0')) {
if (options.loader) {
throw new Error(
`The "loader" option requires Angular version 17.1.0 or greater. You are currently using version ${angularVersion}.`
);
}

if (options.indexHtmlTransformer && lt(angularVersion, '17.1.0')) {
throw new Error(
`The "indexHtmlTransformer" option requires Angular version 17.1.0 or greater. You are currently using version ${angularVersion}.`
);
}
if (options.indexHtmlTransformer) {
throw new Error(
`The "indexHtmlTransformer" option requires Angular version 17.1.0 or greater. You are currently using version ${angularVersion}.`
);
}

if (
typeof options.index === 'object' &&
options.index.preloadInitial !== undefined &&
lt(angularVersion, '17.1.0')
) {
throw new Error(
`The "index.preloadInitial" option requires Angular version 17.1.0 or greater. You are currently using version ${angularVersion}.`
);
}
if (
typeof options.index === 'object' &&
options.index.preloadInitial !== undefined
) {
throw new Error(
`The "index.preloadInitial" option requires Angular version 17.1.0 or greater. You are currently using version ${angularVersion}.`
);
}

if (
options.optimization &&
typeof options.optimization !== 'boolean' &&
options.optimization.styles &&
typeof options.optimization.styles !== 'boolean' &&
lt(angularVersion, '17.1.0')
) {
if (options.optimization.styles.removeSpecialComments === false) {
if (
options.optimization &&
typeof options.optimization !== 'boolean' &&
options.optimization.styles &&
typeof options.optimization.styles !== 'boolean'
) {
if (options.optimization.styles.removeSpecialComments === false) {
throw new Error(
`The "optimization.styles.removeSpecialComments" option requires Angular version 17.1.0 or greater. You are currently using version ${angularVersion}.`
);
} else if (options.optimization.styles.removeSpecialComments === true) {
// silently remove the option, as it was the default before 17.1.0
delete options.optimization.styles.removeSpecialComments;
}
}

if (typeof options.outputPath === 'object') {
throw new Error(
`The "optimization.styles.removeSpecialComments" option requires Angular version 17.1.0 or greater. You are currently using version ${angularVersion}.`
`The "outputPath" option as an object requires Angular version 17.1.0 or greater. You are currently using version ${angularVersion}.`
);
} else if (options.optimization.styles.removeSpecialComments === true) {
// silently remove the option, as it was the default before 17.1.0
delete options.optimization.styles.removeSpecialComments;
}
}

if (typeof options.outputPath === 'object' && lt(angularVersion, '17.1.0')) {
throw new Error(
`The "outputPath" option as an object requires Angular version 17.1.0 or greater. You are currently using version ${angularVersion}.`
);
if (lt(angularVersion, '17.2.0-rc.0')) {
if (options.define) {
throw new Error(
`The "define" option requires Angular version 17.2.0 or greater. You are currently using version ${angularVersion}.`
);
}

if (options.clearScreen !== undefined) {
throw new Error(
`The "clearScreen" option requires Angular version 17.2.0 or greater. You are currently using version ${angularVersion}.`
);
}
}
}
70 changes: 60 additions & 10 deletions packages/nx/src/utils/params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -953,20 +953,13 @@ describe('params', () => {
it('should throw if property name matching pattern is not valid', () => {
expect(() =>
validateOptsAgainstSchema(
{
a: true,
b: false,
},
{ a: true, b: false },
{
properties: {
a: {
type: 'boolean',
},
a: { type: 'boolean' },
},
patternProperties: {
'^b$': {
type: 'number',
},
'^b$': { type: 'number' },
},
additionalProperties: false,
}
Expand All @@ -976,6 +969,63 @@ describe('params', () => {
);
});

it('should handle properties matching patternProperties schema', () => {
expect(() =>
validateOptsAgainstSchema(
{ a: true, b: false },
{
properties: {
a: { type: 'boolean' },
},
patternProperties: {
'^b$': { type: 'boolean' },
},
additionalProperties: false,
}
)
).not.toThrow();
});

it('should throw if additional property does not match schema', () => {
expect(() =>
validateOptsAgainstSchema(
{ a: true, b: 'b', c: 'c' },
{
properties: {
a: { type: 'boolean' },
},
patternProperties: {
'^b$': { type: 'string' },
},
additionalProperties: {
type: 'number',
},
}
)
).toThrow(
"Property 'c' does not match the schema. 'c' should be a 'number'."
);
});

it('should handle additional properties when they match the additionalProperties schema', () => {
expect(() =>
validateOptsAgainstSchema(
{ a: true, b: 'b', c: 1, d: 2 },
{
properties: {
a: { type: 'boolean' },
},
patternProperties: {
'^b$': { type: 'string' },
},
additionalProperties: {
type: 'number',
},
}
)
).not.toThrow();
});

it('should throw if found unsupported positional property', () => {
expect(() =>
validateOptsAgainstSchema(
Expand Down
20 changes: 15 additions & 5 deletions packages/nx/src/utils/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type PropertyDescription = {
| { $source: 'projectName' }
| { $source: 'unparsed' }
| { $source: 'workingDirectory' };
additionalProperties?: boolean;
additionalProperties?: boolean | PropertyDescription;
const?: any;
'x-prompt'?:
| string
Expand Down Expand Up @@ -72,7 +72,7 @@ export type Schema = {
oneOf?: Partial<Schema>[];
description?: string;
definitions?: Properties;
additionalProperties?: boolean;
additionalProperties?: boolean | PropertyDescription;
examples?: { command: string; description?: string }[];
patternProperties?: {
[pattern: string]: PropertyDescription;
Expand Down Expand Up @@ -284,7 +284,10 @@ export function validateObject(
}
});

if (schema.additionalProperties === false) {
if (
schema.additionalProperties !== undefined &&
schema.additionalProperties !== true
) {
Object.keys(opts).find((p) => {
if (
Object.keys(schema.properties).indexOf(p) === -1 &&
Expand All @@ -297,8 +300,15 @@ export function validateObject(
throw new SchemaError(
`Schema does not support positional arguments. Argument '${opts[p]}' found`
);
} else {
} else if (schema.additionalProperties === false) {
throw new SchemaError(`'${p}' is not found in schema`);
} else if (typeof schema.additionalProperties === 'object') {
validateProperty(
p,
opts[p],
schema.additionalProperties,
definitions
);
}
}
});
Expand Down Expand Up @@ -595,7 +605,7 @@ export function applyVerbosity(
isVerbose: boolean
) {
if (
(schema.additionalProperties || 'verbose' in schema.properties) &&
(schema.additionalProperties === true || 'verbose' in schema.properties) &&
isVerbose
) {
options['verbose'] = true;
Expand Down

0 comments on commit 1ba7662

Please sign in to comment.