Skip to content

Commit

Permalink
feat(schematics): add migration schematics for schematics users
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Apr 11, 2018
1 parent 451aa11 commit 20a2f07
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 1 deletion.
51 changes: 51 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
},
"devDependencies": {
"@angular-devkit/build-optimizer": "0.4.6",
"@angular-devkit/schematics": "^0.5.4",
"@types/chai": "4.1.2",
"@types/lodash": "4.14.102",
"@types/mocha": "2.2.48",
Expand Down Expand Up @@ -258,5 +259,11 @@
"engines": {
"npm": ">=2.0.0"
},
"typings": "./dist/package/Rx.d.ts"
"typings": "./dist/package/Rx.d.ts",
"ng-update": {
"requirements": {
"rxjs": "^6"
},
"migrations": "./migrations/migration-collection.json"
}
}
8 changes: 8 additions & 0 deletions spec/migrations/migrations-collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"schematics": {
"migration-01": {
"version": "6",
"factory": "./update-6_0_0/index#rxjsV6MigrationSchematic"
}
}
}
87 changes: 87 additions & 0 deletions spec/migrations/update-6_0_0/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { Rule, SchematicContext, Tree, SchematicsException, chain, } from '@angular-devkit/schematics';
import { TsLintTask, NodePackageInstallTask } from '@angular-devkit/schematics/tasks';

export function rxjsV6MigrationSchematic(_options: any): Rule {
return chain([
addDependencies(),
runTsLint(),
]);
}

const rxjsCompatVersion = '^6.0.0-rc.0';
const rxjsTSLintVersion = '0.0.0';

function addDependencies() {
return (tree: Tree, context: SchematicContext) => {
const pkgPath = '/package.json';
const buffer = tree.read(pkgPath);
if (buffer == null) {
throw new SchematicsException('Could not read package.json');
}
const content = buffer.toString();
const pkg = JSON.parse(content);

if (pkg === null || typeof pkg !== 'object' || Array.isArray(pkg)) {
throw new SchematicsException('Error reading package.json');
}

if (!pkg.dependencies) {
pkg.dependencies = {};
}
if (!pkg.devDependencies) {
pkg.devDependencies = {};
}

pkg.dependencies['rxjs-compat'] = rxjsCompatVersion;
pkg.devDependencies['rxjs-tslint'] = rxjsTSLintVersion;

tree.overwrite(pkgPath, JSON.stringify(pkg, null, 2));
context.addTask(new NodePackageInstallTask());

return tree;
};
}

function runTsLint() {
return (tree: Tree, context: SchematicContext) => {
const workspace = getWorkspace(tree);
const tsLintRules = {
rulesDirectory: ['node_modules/rxjs-tslint'],
rules: {
'update-rxjs-imports': true,
'migrate-to-pipeable-operators': true,
'collapse-rxjs-imports': true
}
};
Object.keys(workspace.projects)
.filter(prj => workspace.projects[prj].architect.build)
.map(prj => workspace.projects[prj].architect.build.options.tsConfig)
.forEach(tsConfigPath => {
const options = {
tsConfigPath: tsConfigPath,
tslintConfig: tsLintRules
};

context.addTask(new TslintFixTask(options));
});

};
}

export function getWorkspacePath(tree: Tree): string {
const possibleFiles = [ '/angular.json', '/.angular.json' ];
const path = possibleFiles.filter(path => tree.exists(path))[0];

return path;
}

export function getWorkspace(tree: Tree): any {
const path = getWorkspacePath(tree);
const configBuffer = tree.read(path);
if (configBuffer === null) {
throw new SchematicsException(`Could not find (${path})`);
}
const config = configBuffer.toString();

return JSON.parse(config);
}
5 changes: 5 additions & 0 deletions spec/migrations/update-6_0_0/latestVersions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const latestVersions = {
'rxjs-compat': '^6.0.0-rc.0',
'rxjs': '^6.0.0-rc.0',
'rxjs-tslint': '*',
};

0 comments on commit 20a2f07

Please sign in to comment.