From 87b0f1b7d91db2d1efe8606cc2c840b23b666a95 Mon Sep 17 00:00:00 2001 From: Victor Savkin Date: Wed, 15 May 2019 17:52:10 -0400 Subject: [PATCH] fix(nx): use custom task runner for presets --- .../workspace/src/schematics/ng-new/ng-new.ts | 64 +++++++++++++++---- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/packages/workspace/src/schematics/ng-new/ng-new.ts b/packages/workspace/src/schematics/ng-new/ng-new.ts index 34a48cf5180e7..9c37890653a3d 100644 --- a/packages/workspace/src/schematics/ng-new/ng-new.ts +++ b/packages/workspace/src/schematics/ng-new/ng-new.ts @@ -10,8 +10,7 @@ import { import { Schema } from './schema'; import { NodePackageInstallTask, - RepositoryInitializerTask, - RunSchematicTask + RepositoryInitializerTask } from '@angular-devkit/schematics/tasks'; import { addDepsToPackageJson } from '../../utils/ast-utils'; @@ -21,6 +20,51 @@ import { toFileName } from '../../utils/name-utils'; import { formatFiles } from '../../utils/rules/format-files'; import { nxVersion } from '../../utils/versions'; +import * as path from 'path'; +import { Observable } from 'rxjs'; +import { spawn } from 'child_process'; + +class RunPresetTask { + toConfiguration() { + return { + name: 'RunPreset' + }; + } +} + +function createPresetTaskExecutor(opts: Schema) { + return { + name: 'RunPreset', + create: () => { + return Promise.resolve(() => { + const spawnOptions = { + stdio: [process.stdin, process.stdout, process.stderr], + shell: true, + cwd: path.join(process.cwd(), opts.directory) + }; + const args = [ + `g`, + `@nrwl/workspace:preset`, + `--name='${opts.name}'`, + `--style='${opts.style}'`, + `--npmScope='${opts.npmScope}'`, + `--preset='${opts.preset}'` + ]; + return new Observable(obs => { + spawn('ng', args, spawnOptions).on('close', (code: number) => { + if (code === 0) { + obs.next(); + obs.complete(); + } else { + const message = 'Workspace creation failed, see above.'; + obs.error(new Error(message)); + } + }); + }); + }); + } + }; +} export default function(options: Schema): Rule { if (options.skipInstall && options.preset !== 'empty') { @@ -28,9 +72,11 @@ export default function(options: Schema): Rule { } options = normalizeOptions(options); - const workspaceOpts = { ...options, preset: undefined }; return (host: Tree, context: SchematicContext) => { + const engineHost = (context.engine.workflow as any).engineHost; + engineHost.registerTaskExecutor(createPresetTaskExecutor(options)); + return chain([ schematic('workspace', workspaceOpts), addDependencies(options), @@ -87,15 +133,9 @@ function addTasks(options: Schema) { ); } if (options.preset !== 'empty') { - const createPresetTask = context.addTask( - new RunSchematicTask('preset', { - name: options.name, - style: options.style, - npmScope: options.npmScope, - preset: options.preset - }), - [packageTask] - ); + const createPresetTask = context.addTask(new RunPresetTask(), [ + packageTask + ]); presetInstallTask = context.addTask( new NodePackageInstallTask(options.directory),