Skip to content

Commit

Permalink
feat(schematics): add update, update:skip, and update:check commands
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Feb 27, 2018
1 parent e8ee7a2 commit 2fb6259
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 34 deletions.
18 changes: 9 additions & 9 deletions e2e/schematics/command-line.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Command line', () => {
);

it(
'nx-migrate should run migrations',
'update should run migrations',
() => {
newProject();
updateFile(
Expand All @@ -48,11 +48,11 @@ describe('Command line', () => {
};
`
);
const checkOut = runCommand('npm run nx-migrate:check');
expect(checkOut).toContain('Run "npm run nx-migrate" to run the following migrations');
const checkOut = runCommand('npm run update:check');
expect(checkOut).toContain('Run "npm run update" to run the following migrations');
expect(checkOut).toContain('20200101-test-migration');

const migrateOut = runCommand('npm run nx-migrate');
const migrateOut = runCommand('npm run update');
expect(migrateOut).toContain('Test migration');
expect(migrateOut).toContain('Running test migration');
expect(migrateOut).toContain(
Expand All @@ -71,17 +71,17 @@ describe('Command line', () => {
`
);

const checkOut2 = runCommand('npm run nx-migrate:check');
expect(checkOut2).toContain('Run "npm run nx-migrate" to run the following migrations');
const checkOut2 = runCommand('npm run update:check');
expect(checkOut2).toContain('Run "npm run update" to run the following migrations');
expect(checkOut2).toContain('20200102-test-migration');

const skipOut = runCommand('npm run nx-migrate:skip');
const skipOut = runCommand('npm run update:skip');
expect(skipOut).toContain(
`The latestMigration property in .angular-cli.json has been set to "20200102-test-migration".`
);

expect(runCommand('npm run nx-migrate:check')).not.toContain('IMPORTANT');
expect(runCommand('npm run nx-migrate')).toContain('No migrations to run');
expect(runCommand('npm run update:check')).not.toContain('IMPORTANT');
expect(runCommand('npm run update')).toContain('No migrations to run');
},
1000000
);
Expand Down
14 changes: 10 additions & 4 deletions e2e/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@ import * as path from 'path';

const projectName: string = 'proj';

export function runNgNew(command?: string): string {
export function runNgNew(command?: string, silent?: boolean): string {
return execSync(`../node_modules/.bin/ng new proj ${command}`, {
cwd: `./tmp`
cwd: `./tmp`,
...(silent ? {stdio: ['ignore', 'ignore', 'ignore']} : {})
}).toString();
}

export function newProject(): void {
cleanup();
if (!directoryExists('./tmp/proj_backup')) {
runNgNew('--collection=@nrwl/schematics --npmScope=proj');
//TODO delete the try catch after 0.8.0 is released
try {
runNgNew('--collection=@nrwl/schematics --npmScope=proj', true);
} catch (e) {
}
copyMissingPackages();
execSync('npm run postinstall', {cwd: './tmp/proj'});
execSync('mv ./tmp/proj ./tmp/proj_backup');
}
execSync('cp -a ./tmp/proj_backup ./tmp/proj');
Expand All @@ -31,7 +37,7 @@ export function createNxWorkspace(command: string): string {
}

export function copyMissingPackages(): void {
const modulesToCopy = ['@ngrx', 'jasmine-marbles', '@nrwl', 'angular', '@angular/upgrade', '@angular/cli'];
const modulesToCopy = ['@ngrx', 'jasmine-marbles', '@nrwl', 'angular', '@angular/upgrade'];
modulesToCopy.forEach(m => copyNodeModule(projectName, m));
}

Expand Down
25 changes: 25 additions & 0 deletions packages/schematics/migrations/20180227-cleanup-scripts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {updateJsonFile} from '../src/collection/utility/fileutils';

export default {
description: 'Add update, update:skip, update:check scripts',
run: () => {
updateJsonFile('package.json', json => {
json.scripts = {
...json.scripts,
"update": "./node_modules/.bin/nx update",
"update:check": "./node_modules/.bin/nx update check",
"update:skip": "./node_modules/.bin/nx update skip",
"nx-migrate": undefined,
"nx-migrate:check": undefined,
"nx-migrate:skip": undefined,
"apps:affected": undefined,
"build:affected": undefined,
"e2e:affected": undefined
};

if (json.scripts.postinstall === './node_modules/.bin/nx migrate check') {
json.scripts.postinstall = "./node_modules/.bin/nx postinstall";
}
});
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
"lint": "ng lint",
"e2e": "ng e2e",

"apps:affected": "./node_modules/.bin/nx affected apps",
"build:affected": "./node_modules/.bin/nx affected build",
"e2e:affected": "./node_modules/.bin/nx affected e2e",

"affected:apps": "./node_modules/.bin/nx affected apps",
"affected:build": "./node_modules/.bin/nx affected build",
"affected:e2e": "./node_modules/.bin/nx affected e2e",
Expand All @@ -22,11 +18,11 @@
"format:write": "./node_modules/.bin/nx format write",
"format:check": "./node_modules/.bin/nx format check",

"nx-migrate": "./node_modules/.bin/nx migrate",
"nx-migrate:check": "./node_modules/.bin/nx migrate check",
"nx-migrate:skip": "./node_modules/.bin/nx migrate skip",
"update": "./node_modules/.bin/nx update",
"update:check": "./node_modules/.bin/nx update check",
"update:skip": "./node_modules/.bin/nx update skip",

"postinstall": "./node_modules/.bin/nx migrate check"
"postinstall": "./node_modules/.bin/nx postinstall"
},
"private": true,
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/schematics/src/collection/ngrx/ngrx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('ngrx', () => {
expect(packageJson.dependencies['@ngrx/effects']).toBeDefined();
});

fit('should error when no module is provided', () => {
it('should error when no module is provided', () => {
expect(() =>
schematicRunner.runSchematic(
'ngrx',
Expand Down
2 changes: 1 addition & 1 deletion packages/schematics/src/collection/utility/lib-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const ngrxVersion = '5.1.0';
export const routerStoreVersion = '5.0.1';
export const nxVersion = '*';
export const schematicsVersion = '*';
export const angularCliSchema = './node_modules/@nrwl/schematics/src/schema.json';
export const angularCliSchema = '20180227-cleanup-scripts';
export const latestMigration = '20180225-switch-to-cli17';
export const prettierVersion = '1.10.2';
export const typescriptVersion = '2.6.2';
Expand Down
10 changes: 5 additions & 5 deletions packages/schematics/src/collection/workspace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ function updatePackageJson() {
packageJson.devDependencies['prettier'] = prettierVersion;
}

packageJson.scripts['apps:affected'] = './node_modules/.bin/nx affected apps';
packageJson.scripts['build:affected'] = './node_modules/.bin/nx affected build';
packageJson.scripts['e2e:affected'] = './node_modules/.bin/nx affected e2e';

packageJson.scripts['affected:apps'] = './node_modules/.bin/nx affected apps';
packageJson.scripts['affected:build'] = './node_modules/.bin/nx affected build';
packageJson.scripts['affected:e2e'] = './node_modules/.bin/nx affected e2e';
Expand All @@ -66,7 +62,11 @@ function updatePackageJson() {
packageJson.scripts['format:write'] = './node_modules/.bin/nx format write';
packageJson.scripts['format:check'] = './node_modules/.bin/nx format check';

packageJson.scripts['nx-migrate'] = './node_modules/.bin/nx migrate';
packageJson.scripts['update'] = './node_modules/.bin/nx update';
packageJson.scripts['update:check'] = './node_modules/.bin/nx update check';
packageJson.scripts['update:skip'] = './node_modules/.bin/nx update skip';

packageJson.scripts['postinstall'] = './node_modules/.bin/nx postinstall';

host.overwrite('package.json', serializeJson(packageJson));
return host;
Expand Down
14 changes: 11 additions & 3 deletions packages/schematics/src/command-line/nx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env node
import { affected } from './affected';
import { format } from './format';
import { migrate } from './migrate';
import { update } from './update';
import { patchNg } from './patch-ng';

const command = process.argv[2];
const args = process.argv.slice(3);
Expand All @@ -13,8 +14,15 @@ switch (command) {
case 'format':
format(args);
break;
case 'migrate':
migrate(args);
case 'migrate': // TODO: delete this after 1.0
update(args);
break;
case 'update':
update(args);
break;
case 'postinstall':
patchNg();
update(['check']);
break;
default:
throw new Error(`Unrecognized command '${command}'`);
Expand Down
33 changes: 33 additions & 0 deletions packages/schematics/src/command-line/patch-ng.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {readFileSync, statSync, writeFileSync} from "fs";
import * as path from 'path';

const nxCheck = `
// nx-check
if (process.argv.indexOf('update') > -1) {
console.log("This is an Nx workspace, and it provides an enhanced 'update' command.");
console.log('Please run "npm run update" or "yarn update" instead.');
process.exit(1);
}
// nx-check-end
`;

export function patchNg() {
const ngBin = path.join('node_modules', '@angular', 'cli', 'bin', 'ng');
if (fileExists(ngBin)) {
const file = readFileSync(ngBin).toString();
writeFileSync(ngBin, addNxCheck(file));
}
}

function addNxCheck(file: string): string {
if (file.indexOf('nx-check') > -1) return file;
return file.replace(`'use strict';`, `'use strict';${nxCheck}`);
}

function fileExists(filePath: string): boolean {
try {
return statSync(filePath).isFile();
} catch (err) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as path from 'path';
type Migration = { description: string; run(): void };
type MigrationName = { name: string; migration: Migration };

export function migrate(args: string[]) {
export function update(args: string[]) {
const allMigrations = readAllMigrations();
const latestMigration = readLatestMigration();
const migrationsToRun = calculateMigrationsToRun(allMigrations, latestMigration);
Expand Down Expand Up @@ -66,15 +66,15 @@ function check(latestMigration: string, migrations: MigrationName[]): void {
console.log('-----------------------------------------------------------------------------');
console.log('-------------------------------IMPORTANT!!!----------------------------------');
console.log('-----------------------------------------------------------------------------');
console.log('Run "npm run nx-migrate" to run the following migrations:');
console.log('Run "npm run update" to run the following migrations:');
migrations.forEach(m => {
console.log(`- ${m.name}`);
console.log(m.migration.description);
console.log('-----------------------------------------------------------------------------');
});

const target = migrations[migrations.length - 1].name;
console.log(`Or run "npm run nx-migrate:skip" to set the latestMigration property`);
console.log(`Or run "npm run update:skip" to set the latestMigration property`);
console.log(`in .angular-cli.json to: "${target}".`);
}

Expand Down

0 comments on commit 2fb6259

Please sign in to comment.