fix(schematic): make the migration to 6.0.0 more flexible

This commit is contained in:
Victor Savkin 2018-05-18 14:15:05 -04:00
parent 9fc7c9f4c3
commit 7650950abd

View File

@ -1,9 +1,4 @@
import { import { chain, Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
chain,
SchematicContext,
Tree,
Rule
} from '@angular-devkit/schematics';
import { stripIndents } from '@angular-devkit/core/src/utils/literals'; import { stripIndents } from '@angular-devkit/core/src/utils/literals';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks'; import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
@ -12,13 +7,13 @@ import { FormatFiles } from '../../src/utils/tasks';
import { serializeJson } from '../../src/utils/fileutils'; import { serializeJson } from '../../src/utils/fileutils';
import { parseTarget, serializeTarget } from '../../src/utils/cli-config-utils'; import { parseTarget, serializeTarget } from '../../src/utils/cli-config-utils';
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path';
import { offsetFromRoot } from '@nrwl/schematics/src/utils/common'; import { offsetFromRoot } from '@nrwl/schematics/src/utils/common';
function createKarma(host: Tree, project: any) { function createKarma(host: Tree, project: any) {
const offset = offsetFromRoot(project.root); const offset = offsetFromRoot(project.root);
host.create( createOrUpdate(
host,
`${project.root}/karma.conf.js`, `${project.root}/karma.conf.js`,
` `
// Karma configuration file, see link for more information // Karma configuration file, see link for more information
@ -57,7 +52,8 @@ module.exports = function (config) {
} }
function createProtractor(host: Tree, project: any) { function createProtractor(host: Tree, project: any) {
host.create( createOrUpdate(
host,
`${project.root}/protractor.conf.js`, `${project.root}/protractor.conf.js`,
` `
// Protractor configuration file, see link for more information // Protractor configuration file, see link for more information
@ -94,7 +90,8 @@ exports.config = {
function createTestTs(host: Tree, project: any) { function createTestTs(host: Tree, project: any) {
if (project.projectType === 'library') { if (project.projectType === 'library') {
host.create( createOrUpdate(
host,
`${project.sourceRoot}/test.ts`, `${project.sourceRoot}/test.ts`,
` `
// This file is required by karma.conf.js and loads recursively all the .spec and framework files // This file is required by karma.conf.js and loads recursively all the .spec and framework files
@ -122,7 +119,8 @@ context.keys().map(context);
` `
); );
} else { } else {
host.create( createOrUpdate(
host,
`${project.sourceRoot}/test.ts`, `${project.sourceRoot}/test.ts`,
` `
// This file is required by karma.conf.js and loads recursively all the .spec and framework files // This file is required by karma.conf.js and loads recursively all the .spec and framework files
@ -152,7 +150,8 @@ context.keys().map(context);
} }
function createBrowserlist(host: Tree, project: any) { function createBrowserlist(host: Tree, project: any) {
host.create( createOrUpdate(
host,
`${project.root}/browserslist`, `${project.root}/browserslist`,
stripIndents` stripIndents`
# This file is currently used by autoprefixer to adjust CSS to support the below specified browsers # This file is currently used by autoprefixer to adjust CSS to support the below specified browsers
@ -183,7 +182,8 @@ function createTsconfigSpecJson(host: Tree, project: any) {
compilerOptions['module'] = 'commonjs'; compilerOptions['module'] = 'commonjs';
} }
host.create( createOrUpdate(
host,
`${project.root}/tsconfig.spec.json`, `${project.root}/tsconfig.spec.json`,
serializeJson({ serializeJson({
extends: `${offset}tsconfig.json`, extends: `${offset}tsconfig.json`,
@ -197,7 +197,8 @@ function createTsconfigSpecJson(host: Tree, project: any) {
function createTslintJson(host: Tree, project: any) { function createTslintJson(host: Tree, project: any) {
const offset = offsetFromRoot(project.root); const offset = offsetFromRoot(project.root);
host.create( createOrUpdate(
host,
`${project.root}/tslint.json`, `${project.root}/tslint.json`,
serializeJson({ serializeJson({
extends: `${offset}tslint.json`, extends: `${offset}tslint.json`,
@ -212,7 +213,8 @@ function createTslintJson(host: Tree, project: any) {
function createTsconfigLibJson(host: Tree, project: any) { function createTsconfigLibJson(host: Tree, project: any) {
const offset = offsetFromRoot(project.root); const offset = offsetFromRoot(project.root);
host.create( createOrUpdate(
host,
`${project.root}/tsconfig.lib.json`, `${project.root}/tsconfig.lib.json`,
serializeJson({ serializeJson({
extends: `${offset}tsconfig.json`, extends: `${offset}tsconfig.json`,
@ -296,6 +298,7 @@ function patchLibIndexFiles(host: Tree, context: SchematicContext) {
Object.values(angularJson.projects).forEach((p: any) => { Object.values(angularJson.projects).forEach((p: any) => {
if (p.projectType === 'library') { if (p.projectType === 'library') {
try {
fs.renameSync(p.sourceRoot, `${p.root}/lib`); fs.renameSync(p.sourceRoot, `${p.root}/lib`);
fs.mkdirSync(p.sourceRoot); fs.mkdirSync(p.sourceRoot);
fs.renameSync(`${p.root}/lib`, `${p.sourceRoot}/lib`); fs.renameSync(`${p.root}/lib`, `${p.sourceRoot}/lib`);
@ -313,6 +316,11 @@ function patchLibIndexFiles(host: Tree, context: SchematicContext) {
content.replace(new RegExp('/src/', 'g'), '/lib/') content.replace(new RegExp('/src/', 'g'), '/lib/')
); );
host.delete(`${p.root}/index.ts`); host.delete(`${p.root}/index.ts`);
} catch (e) {
console.warn(`Nx failed to successfully update '${p.root}'.`);
console.warn(`Error message: ${e.message}`);
console.warn(`PLease update the library manually.`);
}
} }
}); });
@ -323,6 +331,11 @@ const updatePackageJson = updateJsonInTree('package.json', json => {
json.dependencies = json.dependencies || {}; json.dependencies = json.dependencies || {};
json.devDependencies = json.devDependencies || {}; json.devDependencies = json.devDependencies || {};
json.scripts = {
...json.scripts,
'affected:test': './node_modules/.bin/nx affected:test'
};
json.dependencies = { json.dependencies = {
...json.dependencies, ...json.dependencies,
// Migrating Angular Dependencies because ng update @angular/core doesn't work well right now // Migrating Angular Dependencies because ng update @angular/core doesn't work well right now
@ -388,7 +401,8 @@ function createDefaultAppTsConfig(host: Tree, project: any) {
include: ['**/*.ts'], include: ['**/*.ts'],
exclude: ['src/test.ts', '**/*.spec.ts'] exclude: ['src/test.ts', '**/*.spec.ts']
}; };
host.create( createOrUpdate(
host,
`${project.root}/tsconfig.app.json`, `${project.root}/tsconfig.app.json`,
serializeJson(defaultAppTsConfig) serializeJson(defaultAppTsConfig)
); );
@ -406,7 +420,9 @@ function createDefaultE2eTsConfig(host: Tree, project: any) {
types: ['jasmine', 'jasminewd2', 'node'] types: ['jasmine', 'jasminewd2', 'node']
} }
}; };
host.create( createOrUpdate(
host,
`${project.root}/tsconfig.e2e.json`, `${project.root}/tsconfig.e2e.json`,
serializeJson(defaultE2eTsConfig) serializeJson(defaultE2eTsConfig)
); );
@ -427,7 +443,9 @@ function updateTsConfigs(host: Tree) {
tsConfig.exclude.push('src/test.ts'); tsConfig.exclude.push('src/test.ts');
} }
host.create( createOrUpdate(
host,
`${project.root}/tsconfig.app.json`, `${project.root}/tsconfig.app.json`,
serializeJson({ serializeJson({
...tsConfig, ...tsConfig,
@ -469,7 +487,9 @@ function updateTsConfigs(host: Tree) {
}; };
delete tsConfig.include; delete tsConfig.include;
delete tsConfig.exclude; delete tsConfig.exclude;
host.create( createOrUpdate(
host,
`${project.root}/tsconfig.e2e.json`, `${project.root}/tsconfig.e2e.json`,
serializeJson(tsConfig) serializeJson(tsConfig)
); );
@ -622,6 +642,14 @@ function pathToName(s: string) {
return s.replace(new RegExp('/', 'g'), '-'); return s.replace(new RegExp('/', 'g'), '-');
} }
function createOrUpdate(host: Tree, path: string, content: string) {
if (host.exists(path)) {
host.overwrite(path, content);
} else {
host.create(path, content);
}
}
export default function(): Rule { export default function(): Rule {
return chain([ return chain([
checkCli6Upgraded, checkCli6Upgraded,