Skip to content

Commit 5cb5b58

Browse files
committed
fix(@angular/cli): handle format path in invoked schematics
With this change we change the how we handle `"format": "path"` schematic property option. Previously, in version 14 this format was only being applied on parent schematics that were registered as commands. With this change we apply the formatter on all options, even those who are called programmatically using APIs like `externalSchematic`.
1 parent 84ba308 commit 5cb5b58

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

packages/angular/cli/src/command-builder/schematics-command-module.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { normalize as devkitNormalize, schema, tags } from '@angular-devkit/core';
9+
import { normalize as devkitNormalize, isJsonObject, schema, tags } from '@angular-devkit/core';
1010
import { Collection, UnsuccessfulWorkflowExecution, formats } from '@angular-devkit/schematics';
1111
import {
1212
FileSystemCollectionDescription,
@@ -135,12 +135,14 @@ export abstract class SchematicsCommandModule
135135
workflow.registry.addPostTransform(schema.transforms.addUndefinedDefaults);
136136
workflow.registry.useXDeprecatedProvider((msg) => logger.warn(msg));
137137
workflow.registry.addSmartDefaultProvider('projectName', () => this.getProjectName());
138-
workflow.registry.addSmartDefaultProvider(
139-
'workingDirectory',
140-
() => devkitNormalize(relative(this.context.root, process.cwd())) || undefined,
138+
139+
const workingDir = devkitNormalize(relative(this.context.root, process.cwd()));
140+
workflow.registry.addSmartDefaultProvider('workingDirectory', () =>
141+
workingDir === '' ? undefined : workingDir,
141142
);
142143

143144
let shouldReportAnalytics = true;
145+
144146
workflow.engineHost.registerOptionsTransform(async (schematic, options) => {
145147
if (shouldReportAnalytics) {
146148
shouldReportAnalytics = false;
@@ -154,6 +156,35 @@ export abstract class SchematicsCommandModule
154156
]);
155157
}
156158

159+
// TODO: The below should be removed in version 15 when we change 1P schematics to use the `workingDirectory smart default`.
160+
// Handle `"format": "path"` options.
161+
const schema = schematic?.schemaJson;
162+
if (!options || !schema || !isJsonObject(schema)) {
163+
return options;
164+
}
165+
166+
if (!('path' in options && (options as Record<string, unknown>)['path'] === undefined)) {
167+
return options;
168+
}
169+
170+
const properties = schema?.['properties'];
171+
if (!properties || !isJsonObject(properties)) {
172+
return options;
173+
}
174+
175+
const property = properties['path'];
176+
if (!property || !isJsonObject(property)) {
177+
return options;
178+
}
179+
180+
if (property['format'] === 'path' && !property['$default']) {
181+
(options as Record<string, unknown>)['path'] = workingDir || undefined;
182+
this.context.logger.warn(
183+
`The 'path' option in '${schematic?.schema}' is using deprecated behaviour.` +
184+
`'workingDirectory' smart default provider should be used instead.`,
185+
);
186+
}
187+
157188
return options;
158189
});
159190

0 commit comments

Comments
 (0)