diff --git a/e2e/schematics/command-line.test.ts b/e2e/schematics/command-line.test.ts index 4815a41d5d..cc2626ac8e 100644 --- a/e2e/schematics/command-line.test.ts +++ b/e2e/schematics/command-line.test.ts @@ -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 ); diff --git a/e2e/utils.ts b/e2e/utils.ts index 86edc495c8..ac22317ce9 100644 --- a/e2e/utils.ts +++ b/e2e/utils.ts @@ -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)); } diff --git a/packages/schematics/migrations/20180227-cleanup-scripts.ts b/packages/schematics/migrations/20180227-cleanup-scripts.ts new file mode 100644 index 0000000000..312782eca6 --- /dev/null +++ b/packages/schematics/migrations/20180227-cleanup-scripts.ts @@ -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"; + } + }); + } +}; diff --git a/packages/schematics/src/collection/application/files/__directory__/package.json b/packages/schematics/src/collection/application/files/__directory__/package.json index 5368b1c061..83df523c15 100644 --- a/packages/schematics/src/collection/application/files/__directory__/package.json +++ b/packages/schematics/src/collection/application/files/__directory__/package.json @@ -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": { diff --git a/packages/schematics/src/collection/ngrx/ngrx.spec.ts b/packages/schematics/src/collection/ngrx/ngrx.spec.ts index 23fb02162d..c2bf1105ff 100644 --- a/packages/schematics/src/collection/ngrx/ngrx.spec.ts +++ b/packages/schematics/src/collection/ngrx/ngrx.spec.ts @@ -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', diff --git a/packages/schematics/src/collection/utility/lib-versions.ts b/packages/schematics/src/collection/utility/lib-versions.ts index 2ad480cdf4..398ba43c22 100644 --- a/packages/schematics/src/collection/utility/lib-versions.ts +++ b/packages/schematics/src/collection/utility/lib-versions.ts @@ -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'; diff --git a/packages/schematics/src/collection/workspace/index.ts b/packages/schematics/src/collection/workspace/index.ts index 4a0540e6cf..8a4c0bb849 100644 --- a/packages/schematics/src/collection/workspace/index.ts +++ b/packages/schematics/src/collection/workspace/index.ts @@ -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; diff --git a/packages/schematics/src/command-line/nx.ts b/packages/schematics/src/command-line/nx.ts index f05e4d01e4..b65e5f347d 100644 --- a/packages/schematics/src/command-line/nx.ts +++ b/packages/schematics/src/command-line/nx.ts @@ -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}'`); diff --git a/packages/schematics/src/command-line/patch-ng.ts b/packages/schematics/src/command-line/patch-ng.ts new file mode 100644 index 0000000000..2a5d951e8b --- /dev/null +++ b/packages/schematics/src/command-line/patch-ng.ts @@ -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; + } +} \ No newline at end of file diff --git a/packages/schematics/src/command-line/migrate.ts b/packages/schematics/src/command-line/update.ts similarity index 94% rename from packages/schematics/src/command-line/migrate.ts rename to packages/schematics/src/command-line/update.ts index a429f5b4ab..d15c7532ba 100644 --- a/packages/schematics/src/command-line/migrate.ts +++ b/packages/schematics/src/command-line/update.ts @@ -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}".`); }