Skip to content

Commit

Permalink
feat(@angular-devkit/schematics): allow using a root path with NodeWo…
Browse files Browse the repository at this point in the history
…rkflow

This change allows the creation of a NodeWorkflow class with a path string instead of requiring a fully configured filesystem host object.  This reduces the amount of code necessary to setup a schematics runtime for common cases.
  • Loading branch information
clydin committed Oct 2, 2020
1 parent 2cf1b9a commit 017581c
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions packages/angular_devkit/schematics/tools/workflow/node-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* 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, schema, virtualFs } from '@angular-devkit/core';
import { Path, getSystemPath, normalize, schema, virtualFs } from '@angular-devkit/core';
import { NodeJsSyncHost } from '@angular-devkit/core/node';
import {
workflow,
} from '@angular-devkit/schematics'; // tslint:disable-line:no-implicit-dependencies
Expand All @@ -17,6 +18,15 @@ import { NodeModulesEngineHost } from '../node-module-engine-host';
* A workflow specifically for Node tools.
*/
export class NodeWorkflow extends workflow.BaseWorkflow {
constructor(root: string, options: {
force?: boolean;
dryRun?: boolean;
packageManager?: string;
packageRegistry?: string;
registry?: schema.CoreSchemaRegistry;
resolvePaths?: string[],
});

constructor(
host: virtualFs.Host,
options: {
Expand All @@ -28,7 +38,30 @@ export class NodeWorkflow extends workflow.BaseWorkflow {
registry?: schema.CoreSchemaRegistry;
resolvePaths?: string[],
},
);

constructor(
hostOrRoot: virtualFs.Host | string,
options: {
force?: boolean;
dryRun?: boolean;
root?: Path;
packageManager?: string;
packageRegistry?: string;
registry?: schema.CoreSchemaRegistry;
resolvePaths?: string[],
},
) {
let host;
let root;
if (typeof hostOrRoot === 'string') {
root = normalize(hostOrRoot);
host = new virtualFs.ScopedHost(new NodeJsSyncHost(), root);
} else {
host = hostOrRoot;
root = options.root;
}

const engineHost = new NodeModulesEngineHost(options.resolvePaths);
super({
host,
Expand All @@ -44,14 +77,14 @@ export class NodeWorkflow extends workflow.BaseWorkflow {
{
allowPackageManagerOverride: true,
packageManager: options.packageManager,
rootDirectory: options.root && getSystemPath(options.root),
rootDirectory: root && getSystemPath(root),
registry: options.packageRegistry,
},
);
engineHost.registerTaskExecutor(
BuiltinTaskExecutor.RepositoryInitializer,
{
rootDirectory: options.root && getSystemPath(options.root),
rootDirectory: root && getSystemPath(root),
},
);
engineHost.registerTaskExecutor(BuiltinTaskExecutor.RunSchematic);
Expand All @@ -61,7 +94,7 @@ export class NodeWorkflow extends workflow.BaseWorkflow {
}

get engine(): FileSystemEngine {
return this._engine as {} as FileSystemEngine;
return this._engine as FileSystemEngine;
}
get engineHost(): NodeModulesEngineHost {
return this._engineHost as NodeModulesEngineHost;
Expand Down

0 comments on commit 017581c

Please sign in to comment.