Skip to content

fix(@angular/cli): use correct schematic defaults considering workspace #15041

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
Jul 16, 2019
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
4 changes: 4 additions & 0 deletions etc/api/angular_devkit/schematics/tools/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export declare type FileSystemCollection = Collection<FileSystemCollectionDescri
export declare type FileSystemCollectionDesc = CollectionDescription<FileSystemCollectionDescription>;

export interface FileSystemCollectionDescription {
readonly name: string;
readonly path: string;
readonly schematics: {
[name: string]: FileSystemSchematicDesc;
Expand Down Expand Up @@ -92,9 +93,11 @@ export interface FileSystemSchematicDescription extends FileSystemSchematicJsonD

export interface FileSystemSchematicJsonDescription {
readonly aliases?: string[];
readonly collection: FileSystemCollectionDescription;
readonly description: string;
readonly extends?: string;
readonly factory: string;
readonly name: string;
readonly schema?: string;
}

Expand Down Expand Up @@ -135,6 +138,7 @@ export declare class NodeWorkflow extends workflow.BaseWorkflow {
dryRun?: boolean;
root?: Path;
packageManager?: string;
registry?: schema.CoreSchemaRegistry;
});
}

Expand Down
38 changes: 27 additions & 11 deletions packages/angular/cli/models/schematic-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ import {
virtualFs,
} from '@angular-devkit/core';
import { NodeJsSyncHost } from '@angular-devkit/core/node';
import { DryRunEvent, UnsuccessfulWorkflowExecution, workflow } from '@angular-devkit/schematics';
import {
DryRunEvent,
UnsuccessfulWorkflowExecution,
formats,
workflow,
} from '@angular-devkit/schematics';
import {
FileSystemCollection,
FileSystemEngine,
FileSystemSchematic,
FileSystemSchematicDescription,
NodeWorkflow,
validateOptionsWithSchema,
} from '@angular-devkit/schematics/tools';
Expand Down Expand Up @@ -247,6 +253,7 @@ export abstract class SchematicCommand<
dryRun,
packageManager: getPackageManager(this.workspace.root),
root: normalize(this.workspace.root),
registry: new schema.CoreSchemaRegistry(formats.standardFormats),
});
workflow.engineHost.registerContextTransform(context => {
// This is run by ALL schematics, so if someone uses `externalSchematics(...)` which
Expand All @@ -262,15 +269,7 @@ export abstract class SchematicCommand<
}
});

workflow.engineHost.registerOptionsTransform(validateOptionsWithSchema(workflow.registry));

if (options.defaults) {
workflow.registry.addPreTransform(schema.transforms.addUndefinedDefaults);
} else {
workflow.registry.addPostTransform(schema.transforms.addUndefinedDefaults);
}

workflow.registry.addSmartDefaultProvider('projectName', () => {
const getProjectName = () => {
if (this._workspace) {
try {
return (
Expand All @@ -292,7 +291,24 @@ export abstract class SchematicCommand<
}

return undefined;
});
};

workflow.engineHost.registerOptionsTransform(
<T extends {}>(schematic: FileSystemSchematicDescription, current: T) => ({
...getSchematicDefaults(schematic.collection.name, schematic.name, getProjectName()),
...current,
}),
);

if (options.defaults) {
workflow.registry.addPreTransform(schema.transforms.addUndefinedDefaults);
} else {
workflow.registry.addPostTransform(schema.transforms.addUndefinedDefaults);
}

workflow.engineHost.registerOptionsTransform(validateOptionsWithSchema(workflow.registry));

workflow.registry.addSmartDefaultProvider('projectName', getProjectName);

if (options.interactive !== false && isTTY()) {
workflow.registry.usePromptProvider((definitions: Array<schema.PromptDefinition>) => {
Expand Down
3 changes: 3 additions & 0 deletions packages/angular_devkit/schematics/tools/description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {


export interface FileSystemCollectionDescription {
readonly name: string;
readonly path: string;
readonly version?: string;
readonly schematics: { [name: string]: FileSystemSchematicDesc };
Expand All @@ -28,6 +29,8 @@ export interface FileSystemCollectionDescription {
export interface FileSystemSchematicJsonDescription {
readonly aliases?: string[];
readonly factory: string;
readonly name: string;
readonly collection: FileSystemCollectionDescription;
readonly description: string;
readonly schema?: string;
readonly extends?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Path, getSystemPath, virtualFs } from '@angular-devkit/core';
import { Path, getSystemPath, schema, virtualFs } from '@angular-devkit/core';
import {
workflow,
} from '@angular-devkit/schematics'; // tslint:disable-line:no-implicit-dependencies
import { BuiltinTaskExecutor } from '../../tasks/node';
import { FileSystemEngine } from '../description';
import { NodeModulesEngineHost } from '../node-module-engine-host';
import { validateOptionsWithSchema } from '../schema-option-transform';

/**
* A workflow specifically for Node tools.
Expand All @@ -23,8 +22,9 @@ export class NodeWorkflow extends workflow.BaseWorkflow {
options: {
force?: boolean;
dryRun?: boolean;
root?: Path,
root?: Path;
packageManager?: string;
registry?: schema.CoreSchemaRegistry;
},
) {
const engineHost = new NodeModulesEngineHost();
Expand All @@ -34,9 +34,9 @@ export class NodeWorkflow extends workflow.BaseWorkflow {

force: options.force,
dryRun: options.dryRun,
registry: options.registry
});

engineHost.registerOptionsTransform(validateOptionsWithSchema(this._registry));
engineHost.registerTaskExecutor(
BuiltinTaskExecutor.NodePackage,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
// tslint:disable:no-implicit-dependencies
import { schema } from '@angular-devkit/core';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: unused import?

import { NodeJsSyncHost } from '@angular-devkit/core/node';
import { NodeWorkflow } from '@angular-devkit/schematics/tools';
import * as path from 'path';
Expand Down
10 changes: 7 additions & 3 deletions packages/angular_devkit/schematics_cli/bin/schematics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* found in the LICENSE file at https://angular.io/license
*/

import 'symbol-observable';
// symbol polyfill must go first
import 'symbol-observable';
// tslint:disable-next-line:ordered-imports import-groups
import {
JsonObject,
Expand All @@ -24,8 +24,9 @@ import {
DryRunEvent,
SchematicEngine,
UnsuccessfulWorkflowExecution,
formats
} from '@angular-devkit/schematics';
import { NodeModulesEngineHost, NodeWorkflow } from '@angular-devkit/schematics/tools';
import { NodeModulesEngineHost, NodeWorkflow, validateOptionsWithSchema } from '@angular-devkit/schematics/tools';
import * as inquirer from 'inquirer';
import * as minimist from 'minimist';

Expand Down Expand Up @@ -160,9 +161,12 @@ export async function main({

/** Create a Virtual FS Host scoped to where the process is being run. **/
const fsHost = new virtualFs.ScopedHost(new NodeJsSyncHost(), normalize(process.cwd()));
const registry = new schema.CoreSchemaRegistry(formats.standardFormats);

/** Create the workflow that will be executed with this run. */
const workflow = new NodeWorkflow(fsHost, { force, dryRun });
const workflow = new NodeWorkflow(fsHost, { force, dryRun, registry });
registry.addPostTransform(schema.transforms.addUndefinedDefaults);
workflow.engineHost.registerOptionsTransform(validateOptionsWithSchema(registry));

// Indicate to the user when nothing has been done. This is automatically set to off when there's
// a new DryRunEvent.
Expand Down