Skip to content

Commit

Permalink
fix(nx): run angular migration in separate process
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored and vsavkin committed May 31, 2019
1 parent 319c86d commit 1b3f584
Showing 1 changed file with 73 additions and 32 deletions.
105 changes: 73 additions & 32 deletions packages/schematics/migrations/update-8-0-0/update-8-0-0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import {
chain,
SchematicContext,
Tree,
externalSchematic,
noop
TaskConfigurationGenerator,
TaskConfiguration,
TaskExecutorFactory,
externalSchematic
} from '@angular-devkit/schematics';
import { stripIndents } from '@angular-devkit/core/src/utils/literals';
import {
Expand All @@ -25,9 +27,67 @@ import {
ReplaceChange
} from '@nrwl/workspace/src/utils/ast-utils';
import { relative } from 'path';
import { RunSchematicTaskOptions } from '@angular-devkit/schematics/tasks/run-schematic/options';
import * as path from 'path';
import { platform } from 'os';
import { Observable } from 'rxjs';
import { spawn } from 'child_process';

const ignore = require('ignore');

export class RunUpdateTask implements TaskConfigurationGenerator<any> {
protected _package: string;

constructor(pkg: string) {
this._package = pkg;
}

toConfiguration(): TaskConfiguration<any> {
return {
name: 'RunUpdate',
options: {
package: this._package
}
};
}
}

function createRunUpdateTask(): TaskExecutorFactory<any> {
return {
name: 'RunUpdate',
create: () => {
return Promise.resolve((options: any, context: SchematicContext) => {
context.logger.info(`Updating ${options.package}`);
const spawnOptions = {
stdio: [process.stdin, process.stdout, process.stderr],
shell: true
};
const ng =
platform() === 'win32'
? '.\\node_modules\\.bin\\ng'
: './node_modules/.bin/ng';
const args = [
'update',
options.package,
'--force',
'--allow-dirty'
].filter(e => !!e);
return new Observable(obs => {
spawn(ng, args, spawnOptions).on('close', (code: number) => {
if (code === 0) {
obs.next();
obs.complete();
} else {
const message = `${options.package} migration failed, see above.`;
obs.error(new Error(message));
}
});
});
});
}
};
}

function addDependencies() {
return (host: Tree, context: SchematicContext) => {
const dependencies = readJsonInTree(host, 'package.json').dependencies;
Expand Down Expand Up @@ -340,39 +400,20 @@ export const runAngularMigrations: Rule = (
host: Tree,
context: SchematicContext
) => {
// Should always be there during ng update but not during tests.
if (!context.engine.workflow) {
return;
}

const packageJson = readJsonInTree(host, 'package.json');

if (!!packageJson.dependencies['@angular/core']) {
if (
context.engine.workflow &&
context.engine.workflow.context.parentContext &&
!(context.engine.workflow.context.parentContext
.options as any).packages.includes('@angular/core')
) {
const message = `This migration should be run with the @angular/core migration`;
context.logger.error(message);
context.logger.info(
'\n!!! Please rerun this command as "npm run update -- @angular/core" or "yarn update @angular/core"!\n'
);
throw new Error(message);
}
}
const engineHost = (context.engine.workflow as any)._engineHost;
engineHost.registerTaskExecutor(createRunUpdateTask());
const cliUpgrade = context.addTask(new RunUpdateTask('@angular/cli'));

return chain([
externalSchematic(
'@schematics/update',
'update',
{
packages: ['@angular/cli'],
from: '7.2.2',
to: '8.0.0',
force: true
},
{
interactive: true
}
)
]);
if (packageJson.dependencies['@angular/core']) {
context.addTask(new RunUpdateTask('@angular/core'), [cliUpgrade]);
}
};

const updateNestDependencies = updateJsonInTree('package.json', json => {
Expand Down

0 comments on commit 1b3f584

Please sign in to comment.