feat(schematics): add update, update:skip, and update:check commands

This commit is contained in:
vsavkin 2018-02-27 11:19:32 -05:00 committed by Victor Savkin
parent e8ee7a2d3a
commit 2fb6259751
10 changed files with 102 additions and 34 deletions

View File

@ -34,7 +34,7 @@ describe('Command line', () => {
);
it(
'nx-migrate should run migrations',
'update should run migrations',
() => {
newProject();
updateFile(
@ -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(
@ -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
);

View File

@ -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');
@ -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));
}

View File

@ -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";
}
});
}
};

View File

@ -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",
@ -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": {

View File

@ -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',

View File

@ -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';

View File

@ -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';
@ -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;

View File

@ -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);
@ -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}'`);

View File

@ -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;
}
}

View File

@ -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);
@ -66,7 +66,7 @@ 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);
@ -74,7 +74,7 @@ function check(latestMigration: string, migrations: MigrationName[]): void {
});
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}".`);
}