Skip to content

Commit

Permalink
fix(nx): fix updating from 6 to 8
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored and vsavkin committed Jun 15, 2019
1 parent c2a9271 commit 33d3423
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 49 deletions.
19 changes: 5 additions & 14 deletions packages/schematics/migrations/update-7-0-0/update-7-0-0.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Rule, externalSchematic, chain } from '@angular-devkit/schematics';
import { updateJsonInTree } from '@nrwl/schematics/src/utils/ast-utils';
import { Rule, chain } from '@angular-devkit/schematics';
import { updateJsonInTree } from '../../src/utils/ast-utils';
import { addUpdateTask } from '../../src/utils/update-task';

export default function(): Rule {
return chain([
Expand All @@ -17,17 +18,7 @@ export default function(): Rule {

return json;
}),
externalSchematic('@schematics/update', 'update', {
packages: ['@angular/core'],
from: '6.1.0',
to: '7.0.0',
force: true
}),
externalSchematic('@schematics/update', 'update', {
packages: ['@angular/cli'],
from: '6.2.0',
to: '7.0.1',
force: true
})
addUpdateTask('@angular/core', '7.0.0'),
addUpdateTask('@angular/cli', '7.0.1')
]);
}
8 changes: 2 additions & 6 deletions packages/schematics/migrations/update-7-2-0/update-7-2-0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { updateJsonInTree, readJsonInTree } from '../../src/utils/ast-utils';
import { getWorkspacePath } from '../../src/utils/cli-config-utils';
import { offsetFromRoot } from '../../src/utils/common';
import { stripIndents } from '@angular-devkit/core/src/utils/literals';
import { addUpdateTask } from '../../src/utils/update-task';

function getBuilders(project: any): string[] {
return Array.from(
Expand Down Expand Up @@ -213,12 +214,7 @@ function switchToEs2015(host: Tree, context: SchematicContext) {
});
}

const updateAngularCLI = externalSchematic('@schematics/update', 'update', {
packages: ['@angular/cli'],
from: '7.0.1',
to: '7.1.0',
force: true
});
const updateAngularCLI = addUpdateTask('@angular/cli', '7.1.0');

export default function(): Rule {
return chain([
Expand Down
10 changes: 3 additions & 7 deletions packages/schematics/migrations/update-7-5-0/update-7-5-0.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { Rule, chain, externalSchematic } from '@angular-devkit/schematics';
import { Rule, chain } from '@angular-devkit/schematics';

import { updateJsonInTree } from '../../src/utils/ast-utils';
import { addUpdateTask } from '../../src/utils/update-task';

const updateAngularCLI = externalSchematic('@schematics/update', 'update', {
packages: ['@angular/cli'],
from: '7.1.0',
to: '7.2.2',
force: true
});
const updateAngularCLI = addUpdateTask('@angular/cli', '7.2.2');

const updateTypescript = updateJsonInTree('package.json', json => {
json.devDependencies = json.devDependencies || {};
Expand Down
32 changes: 10 additions & 22 deletions packages/schematics/migrations/update-7-6-0/update-7-6-0.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
Rule,
chain,
externalSchematic,
SchematicContext,
Tree
} from '@angular-devkit/schematics';
import { chain, Rule, Tree } from '@angular-devkit/schematics';

import { ReplaceChange } from '@schematics/angular/utility/change';
import { getSourceNodes } from '@schematics/angular/utility/ast-utils';
Expand All @@ -14,8 +8,10 @@ import * as ts from 'typescript';
import {
updateJsonInTree,
readJsonInTree,
insert
insert,
addDepsToPackageJson
} from '../../src/utils/ast-utils';
import { addUpdateTask } from '../../src/utils/update-task';
import { formatFiles } from '../../src/utils/rules/format-files';

const addExtensionRecommendations = updateJsonInTree(
Expand Down Expand Up @@ -404,21 +400,13 @@ const setDefaults = updateJsonInTree('angular.json', json => {
});

const updateAngularCLI = chain([
externalSchematic('@schematics/update', 'update', {
packages: ['@angular/cli'],
from: '7.2.2',
to: '7.3.1',
force: true
}),
updateJsonInTree('package.json', json => {
json.devDependencies = json.devDependencies || {};
json.devDependencies = {
...json.devDependencies,
'@angular/cli': '7.3.1',
addUpdateTask('@angular/cli', '7.3.1'),
addDepsToPackageJson(
{},
{
'@angular-devkit/build-angular': '~0.13.1'
};
return json;
})
}
)
]);

export default function(): Rule {
Expand Down
99 changes: 99 additions & 0 deletions packages/schematics/src/utils/update-task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import {
chain,
Rule,
SchematicContext,
TaskConfiguration,
TaskConfigurationGenerator,
TaskExecutorFactory,
Tree
} from '@angular-devkit/schematics';
import { platform } from 'os';
import { Observable } from 'rxjs';
import { spawn } from 'child_process';

let taskRegistered = false;

export function addUpdateTask(pkg: string, to: string): Rule {
return (host: Tree, context: SchematicContext) => {
// Workflow should always be there during ng update but not during tests.
if (!context.engine.workflow) {
return;
}
if (!taskRegistered) {
const engineHost = (context.engine.workflow as any)._engineHost;
engineHost.registerTaskExecutor(createRunUpdateTask());

taskRegistered = true;
}
(context.engine as any)._taskSchedulers.forEach(scheduler => {
if (
scheduler._queue.peek() &&
scheduler._queue.peek().configuration.name === 'RunUpdate' &&
scheduler._queue.peek().configuration.options.package === pkg
) {
scheduler._queue.pop();
}
});

context.addTask(new RunUpdateTask(pkg, to));
};
}

interface UpdateTaskOptions {
package: string;
to: string;
}

class RunUpdateTask implements TaskConfigurationGenerator<UpdateTaskOptions> {
constructor(private _pkg: string, private _to: string) {}

toConfiguration(): TaskConfiguration<UpdateTaskOptions> {
return {
name: 'RunUpdate',
options: {
package: this._pkg,
to: this._to
}
};
}
}

function createRunUpdateTask(): TaskExecutorFactory<UpdateTaskOptions> {
return {
name: 'RunUpdate',
create: () => {
return Promise.resolve(
(options: UpdateTaskOptions, context: SchematicContext) => {
context.logger.info(`Updating ${options.package} to ${options.to}`);
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}@${options.to}`,
'--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));
}
});
});
}
);
}
};
}

0 comments on commit 33d3423

Please sign in to comment.