Skip to content

Commit

Permalink
fix(nx): use custom task runner for presets
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed May 16, 2019
1 parent 63f4cb5 commit 87b0f1b
Showing 1 changed file with 52 additions and 12 deletions.
64 changes: 52 additions & 12 deletions packages/workspace/src/schematics/ng-new/ng-new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -21,16 +20,63 @@ 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') {
throw new Error(`Cannot select a preset when skipInstall is set to true.`);
}

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),
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 87b0f1b

Please sign in to comment.