Skip to content

Commit

Permalink
feat(schematics): Add ability to ng update from Nx 1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored and vsavkin committed May 16, 2018
1 parent f78889e commit 8de81d6
Show file tree
Hide file tree
Showing 39 changed files with 949 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateJsonFile } from '../src/utils/fileutils';
import { updateJsonFile } from '../../src/utils/fileutils';

export default {
description: 'Update the schema file to point to the nrwl schema.',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateJsonFile } from '../src/utils/fileutils';
import { updateJsonFile } from '../../src/utils/fileutils';

export default {
description:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateJsonFile } from '../src/utils/fileutils';
import { updateJsonFile } from '../../src/utils/fileutils';

export default {
description: 'Remove npmScope from tslint.json',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readCliConfigFile, updateJsonFile } from '../src/utils/fileutils';
import { readCliConfigFile, updateJsonFile } from '../../src/utils/fileutils';
import { writeFileSync, unlinkSync } from 'fs';
import { offsetFromRoot } from '../src/utils/common';
import { offsetFromRoot } from '../../src/utils/common';
import * as path from 'path';

export default {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateJsonFile } from '../src/utils/fileutils';
import { updateJsonFile } from '../../src/utils/fileutils';

export default {
description: 'Update tsconfig.spec.json to exclude e2e specs',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateJsonFile } from '../src/utils/fileutils';
import { updateJsonFile } from '../../src/utils/fileutils';

export default {
description:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateJsonFile } from '../src/utils/fileutils';
import { updateJsonFile } from '../../src/utils/fileutils';

export default {
description: 'Add format:write and format:check to npm scripts',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateJsonFile } from '../src/utils/fileutils';
import { updateJsonFile } from '../../src/utils/fileutils';

export default {
description: 'Update npm scripts to use the nx command',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateJsonFile } from '../src/utils/fileutils';
import { updateJsonFile } from '../../src/utils/fileutils';

export default {
description: 'Update the version of prettier',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { copyFile, updateJsonFile } from '../src/utils/fileutils';
import { copyFile, updateJsonFile } from '../../src/utils/fileutils';
import * as path from 'path';

export default {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateJsonFile } from '../src/utils/fileutils';
import { updateJsonFile } from '../../src/utils/fileutils';

export default {
description: 'Add @angular-devkit/core as a dev dependency',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateJsonFile } from '../src/utils/fileutils';
import { updateJsonFile } from '../../src/utils/fileutils';

export default {
description: 'Add nx-migrate:check and nx-migrate:skip to npm scripts',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateJsonFile } from '../src/utils/fileutils';
import { updateJsonFile } from '../../src/utils/fileutils';

export default {
description: 'Add postinstall script to run nx-migrate:check',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateJsonFile } from '../src/utils/fileutils';
import { updateJsonFile } from '../../src/utils/fileutils';

export default {
description: 'Add @angular-devkit/schematics as a dev dependency',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateJsonFile } from '../src/utils/fileutils';
import { updateJsonFile } from '../../src/utils/fileutils';
import { unlinkSync } from 'fs';

export default {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateJsonFile } from '../src/utils/fileutils';
import { updateJsonFile } from '../../src/utils/fileutils';

export default {
description: 'Add update, update:skip, update:check scripts',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { writeFileSync, unlinkSync } from 'fs';
import { join } from 'path';

import { updateJsonFile } from '../src/utils/fileutils';
import { updateJsonFile } from '../../src/utils/fileutils';
import {
ExistingPrettierConfig,
resolveUserExistingPrettierConfig
} from '../src/utils/common';
} from '../../src/utils/common';

export default {
description: 'Create or update prettier configuration',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateJsonFile } from '../src/utils/fileutils';
import { updateJsonFile } from '../../src/utils/fileutils';

export default {
description: 'Add tags to all app and libs',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateJsonFile } from '../src/utils/fileutils';
import { updateJsonFile } from '../../src/utils/fileutils';

export default {
description: 'Run lint checks ensuring the integrity of the workspace',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateJsonFile } from '../src/utils/fileutils';
import { updateJsonFile } from '../../src/utils/fileutils';

export default {
description: 'Update npm scripts to use the nx command',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateJsonFile } from '../src/utils/fileutils';
import { updateJsonFile } from '../../src/utils/fileutils';

export default {
description: 'Add a command to generate workspace-specific schematics',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateJsonFile } from '../src/utils/fileutils';
import { updateJsonFile } from '../../src/utils/fileutils';

export default {
description: 'Update script to use an updated way of invoking Nx commands',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { existsSync, writeFileSync } from 'fs';
import { dependencies } from '../../src/command-line/affected-apps';
import {
readJsonFile,
serializeJson,
updateJsonFile
} from '../../src/utils/fileutils';
import { stripIndents } from '@angular-devkit/core/src/utils/literals';

export default {
description: `Create nx.json before migrating to Angular CLI 6.`,
run: () => {
if (!existsSync('.angular-cli.json') && existsSync('angular.json')) {
console.warn(stripIndents`
You have already upgraded to Angular CLI 6.
We will not be able to recover information about your project's tags for you.
`);
return;
}

const angularJson = readJsonFile('.angular-cli.json');
const projects = angularJson.apps.reduce((projects, app) => {
if (app.name === '$workspaceRoot') {
return projects;
}
const normalizedName = app.name.replace(new RegExp('/', 'g'), '-');
projects[normalizedName] = {
tags: app.tags
};
if (app.root.startsWith('apps/')) {
projects[`${normalizedName}-e2e`] = {
tags: []
};
}

return projects;
}, {});
writeFileSync(
'nx.json',
serializeJson({
npmScope: angularJson.project.npmScope,
projects: projects
})
);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { existsSync, writeFileSync } from 'fs';
import { dependencies } from '../../src/command-line/affected-apps';
import {
readJsonFile,
serializeJson,
updateJsonFile
} from '../../src/utils/fileutils';
import { stripIndents } from '@angular-devkit/core/src/utils/literals';
import { execSync } from 'child_process';

export default {
description: `Switch to Nx 6.0`,
run: () => {
if (!existsSync('.angular-cli.json') && existsSync('angular.json')) {
console.warn(stripIndents`
You have already upgraded to Angular CLI 6.
We will not be able to recover information about your project's tags for you.
`);
return;
}

updateJsonFile('package.json', json => {
json.devDependencies['@angular/cli'] = '6.0.1';
});

updateJsonFile('package.json', json => {
delete json.scripts.postinstall;
});

try {
if (existsSync('yarn.lock')) {
execSync('yarn install', { stdio: [0, 1, 2] });
} else {
execSync('npm install', { stdio: [0, 1, 2] });
}

execSync('ng update @angular/cli@6.0.1', { stdio: [0, 1, 2] });
execSync('ng update @nrwl/schematics@6.0.0', { stdio: [0, 1, 2] });
} catch (e) {
console.warn(stripIndents`
The automatic upgrade to Nx 6 has failed with the following error: ${e}.
You can upgrade it manually by running:
* yarn install or npm install
* ng update @angular/cli@6.0.1
* ng update @nrwl/schematics@6.0.0
The upgrade process creates a test target for every library. If you have a library
that does not have specs, either set failOnEmptyTestSuite to false in karma.conf.js of the library,
or remove the test target in angular.json.
`);
throw e;
}

console.log(stripIndents`
The upgrade process creates a test target for every library. If you have a library
that does not have specs, either set failOnEmptyTestSuite to false in karma.conf.js of the library,
or remove the test target in angular.json.
`);
}
};
9 changes: 9 additions & 0 deletions packages/schematics/migrations/migrations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"schematics": {
"update-to-6.0.0": {
"version": "6",
"description": " ",
"factory": "./update-6-0-0/update-6-0-0"
}
}
}
94 changes: 94 additions & 0 deletions packages/schematics/migrations/update-6-0-0/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import * as ts from 'typescript';

export function findFunctionCallExpressionStatement(
nodes: ts.Node[],
functionName: string
): ts.ExpressionStatement {
return nodes.find(
node =>
ts.isExpressionStatement(node) &&
ts.isCallExpression(node.expression) &&
ts.isIdentifier(node.expression.expression) &&
node.expression.expression.escapedText === functionName
) as ts.ExpressionStatement;
}

export function findFunctionCalls(
sourceFile: ts.SourceFile,
functionName: string
) {
return sourceFile.statements.filter(statement => {
if (!ts.isVariableStatement(statement)) {
return false;
}
const declarations = statement.declarationList.declarations;

return declarations.some(declaration => {
if (
!ts.isCallExpression(declaration.initializer) ||
!ts.isIdentifier(declaration.initializer.expression)
) {
return false;
}

return declaration.initializer.expression.text === functionName;
});
}) as ts.VariableStatement[];
}

export function findRequireStatement(nodes: ts.Node[]): ts.VariableStatement {
return nodes.find(node => {
if (!ts.isVariableStatement(node)) {
return false;
}

const requireDeclaration = node.declarationList.declarations.find(
declaration => {
if (!ts.isCallExpression(declaration.initializer)) {
return false;
}

const callExpression = declaration.initializer;
if (
ts.isIdentifier(callExpression.expression) &&
callExpression.expression.escapedText === 'require' &&
ts.isStringLiteral(callExpression.arguments[0])
) {
const argument = callExpression.arguments[0] as ts.StringLiteral;
return (
argument.text === '@nrwl/schematics/src/utils/cli-config-utils'
);
}
return false;
}
) as ts.VariableDeclaration;

return !!requireDeclaration;
}) as ts.VariableStatement;
}

export function findSpecDeclaration(nodes: ts.Node[]) {
return nodes.find(
node =>
ts.isPropertyAssignment(node) &&
ts.isIdentifier(node.name) &&
node.name.text === 'specs' &&
ts.isArrayLiteralExpression(node.initializer)
) as ts.PropertyAssignment;
}

export function findTsNodeRegisterExpression(nodes: ts.Node[]) {
return nodes.find(
node =>
ts.isCallExpression(node) &&
ts.isPropertyAccessExpression(node.expression) &&
ts.isIdentifier(node.expression.name) &&
node.expression.name.text === 'register' &&
ts.isCallExpression(node.expression.expression) &&
ts.isIdentifier(node.expression.expression.expression) &&
node.expression.expression.expression.text === 'require' &&
ts.isStringLiteral(node.expression.expression.arguments[0]) &&
(node.expression.expression.arguments[0] as ts.StringLiteral).text ===
'ts-node'
) as ts.CallExpression;
}
Loading

0 comments on commit 8de81d6

Please sign in to comment.