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 4, 2019
1 parent f76589c commit c3b0b98
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 141 deletions.
17 changes: 4 additions & 13 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 { Rule, chain } from '@angular-devkit/schematics';
import { updateJsonInTree } from '@nrwl/workspace';
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 '@nrwl/workspace';
import { getWorkspacePath } from '@nrwl/workspace';
import { offsetFromRoot } from '@nrwl/workspace';
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
8 changes: 2 additions & 6 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 { updateJsonInTree } from '@nrwl/workspace';
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
38 changes: 15 additions & 23 deletions packages/schematics/migrations/update-7-6-0/update-7-6-0.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import {
Rule,
chain,
externalSchematic,
SchematicContext,
Tree
} from '@angular-devkit/schematics';
import { chain, Rule, Tree } from '@angular-devkit/schematics';

import * as ts from 'typescript';

import { updateJsonInTree, readJsonInTree, insert } from '@nrwl/workspace';
import { formatFiles } from '@nrwl/workspace';
import {
addDepsToPackageJson,
formatFiles,
insert,
readJsonInTree,
updateJsonInTree
} from '@nrwl/workspace';
import {
getSourceNodes,
ReplaceChange
} from '@nrwl/workspace/src/utils/ast-utils';
import { addUpdateTask } from '../../src/utils/update-task';

const addExtensionRecommendations = updateJsonInTree(
'.vscode/extensions.json',
Expand Down Expand Up @@ -401,21 +401,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
17 changes: 6 additions & 11 deletions packages/schematics/migrations/update-7-8-1/update-7-8-1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '@angular-devkit/schematics';
import { updateJsonInTree } from '@nrwl/workspace';
import { stripIndents } from '@angular-devkit/core/src/utils/literals';
import { addDepsToPackageJson } from '@nrwl/workspace/src/utils/ast-utils';

function displayInformation(host: Tree, context: SchematicContext) {
context.logger.info(
Expand All @@ -21,18 +22,12 @@ function displayInformation(host: Tree, context: SchematicContext) {

export default function(): Rule {
return chain([
updateJsonInTree('package.json', json => {
json.scripts = json.scripts || {};
json.devDependencies = json.devDependencies || {};
json.scripts = json.scripts || {};

json.devDependencies = {
...json.devDependencies,
addDepsToPackageJson(
{},
{
prettier: '1.16.4'
};

return json;
}),
}
),
displayInformation
]);
}
97 changes: 15 additions & 82 deletions packages/schematics/migrations/update-8-0-0/update-8-0-0.ts
Original file line number Diff line number Diff line change
@@ -1,93 +1,32 @@
import {
Rule,
chain,
Rule,
SchematicContext,
Tree,
TaskConfigurationGenerator,
TaskConfiguration,
TaskExecutorFactory,
externalSchematic
Tree
} from '@angular-devkit/schematics';
import { stripIndents } from '@angular-devkit/core/src/utils/literals';
import {
readJsonInTree,
addDepsToPackageJson,
updateJsonInTree,
formatFiles,
insert,
formatFiles
readJsonInTree,
updateJsonInTree
} from '@nrwl/workspace';
import {
createSourceFile,
ScriptTarget,
isImportDeclaration,
isStringLiteral
isStringLiteral,
ScriptTarget
} from 'typescript';
import {
getSourceNodes,
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';
import { addUpdateTask } from '../../src/utils/update-task';

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 @@ -400,20 +339,14 @@ 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 { dependencies } = readJsonInTree(host, 'package.json');

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

const engineHost = (context.engine.workflow as any)._engineHost;
engineHost.registerTaskExecutor(createRunUpdateTask());
const cliUpgrade = context.addTask(new RunUpdateTask('@angular/cli'));

if (packageJson.dependencies['@angular/core']) {
context.addTask(new RunUpdateTask('@angular/core'), [cliUpgrade]);
}
return chain([
addUpdateTask('@angular/cli', '8.0.1'),
...(dependencies['@angular/core']
? [addUpdateTask('@angular/core', '8.0.0')]
: [])
]);
};

const updateNestDependencies = updateJsonInTree('package.json', json => {
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 c3b0b98

Please sign in to comment.