diff --git a/docs/api-angular/schematics/ng-add.md b/docs/api-angular/schematics/ng-add.md index 1dcc5d12035a6..37b63afaec916 100644 --- a/docs/api-angular/schematics/ng-add.md +++ b/docs/api-angular/schematics/ng-add.md @@ -19,6 +19,14 @@ Type: `string` Test runner to use for end to end (e2e) tests +### skipFormat + +Default: `false` + +Type: `boolean` + +Skip formatting files + ### skipInstall Default: `false` diff --git a/docs/api-cypress/schematics/cypress-project.md b/docs/api-cypress/schematics/cypress-project.md index 3ecd66dc7bdd7..940dbcfcbe9d5 100644 --- a/docs/api-cypress/schematics/cypress-project.md +++ b/docs/api-cypress/schematics/cypress-project.md @@ -1,4 +1,4 @@ -# cypress-project +# cypress-project [hidden] Add a Cypress E2E Project diff --git a/docs/api-express/schematics/ng-add.md b/docs/api-express/schematics/ng-add.md index c1b9e7f1da305..277aa74e47104 100644 --- a/docs/api-express/schematics/ng-add.md +++ b/docs/api-express/schematics/ng-add.md @@ -8,3 +8,13 @@ Add @nrwl/express to a project ng generate ng-add ... ``` + +## Options + +### skipFormat + +Default: `false` + +Type: `boolean` + +Skip formatting files diff --git a/docs/api-jest/schematics/jest-project.md b/docs/api-jest/schematics/jest-project.md index 6ba9fe3daf9d6..47c72aba84c9d 100644 --- a/docs/api-jest/schematics/jest-project.md +++ b/docs/api-jest/schematics/jest-project.md @@ -1,4 +1,4 @@ -# jest-project +# jest-project [hidden] Add Jest configuration to a project diff --git a/docs/api-nest/schematics/ng-add.md b/docs/api-nest/schematics/ng-add.md index d8353e20c5503..467ee1d4ab477 100644 --- a/docs/api-nest/schematics/ng-add.md +++ b/docs/api-nest/schematics/ng-add.md @@ -8,3 +8,13 @@ Add @nrwl/nest to a project ng generate ng-add ... ``` + +## Options + +### skipFormat + +Default: `false` + +Type: `boolean` + +Skip formatting files diff --git a/docs/api-node/schematics/ng-add.md b/docs/api-node/schematics/ng-add.md index bc1ca026ad0e3..9ac3f5c73ef1a 100644 --- a/docs/api-node/schematics/ng-add.md +++ b/docs/api-node/schematics/ng-add.md @@ -8,3 +8,13 @@ Add @nrwl/node to a project ng generate ng-add ... ``` + +## Options + +### skipFormat + +Default: `false` + +Type: `boolean` + +Skip formatting files diff --git a/docs/api-react/schematics/ng-add.md b/docs/api-react/schematics/ng-add.md index 9ba705c3afe37..9cdf4d16ebd8e 100644 --- a/docs/api-react/schematics/ng-add.md +++ b/docs/api-react/schematics/ng-add.md @@ -8,3 +8,13 @@ Add @nrwl/react to a project ng generate ng-add ... ``` + +## Options + +### skipFormat + +Default: `false` + +Type: `boolean` + +Skip formatting files diff --git a/docs/api-web/schematics/ng-add.md b/docs/api-web/schematics/ng-add.md index 4c0d0b6ffc4c6..b7d72163a0354 100644 --- a/docs/api-web/schematics/ng-add.md +++ b/docs/api-web/schematics/ng-add.md @@ -8,3 +8,13 @@ Add @nrwl/web to a project ng generate ng-add ... ``` + +## Options + +### skipFormat + +Default: `false` + +Type: `boolean` + +Skip formatting files diff --git a/docs/api-workspace/schematics/library.md b/docs/api-workspace/schematics/library.md index 97a38804846a3..ae4d4cac50dc8 100644 --- a/docs/api-workspace/schematics/library.md +++ b/docs/api-workspace/schematics/library.md @@ -39,14 +39,6 @@ Type: `boolean` Do not update tsconfig.json for development experience. -### style - -Default: `css` - -Type: `string` - -The file extension to be used for style files. - ### tags Type: `string` diff --git a/docs/api-workspace/schematics/ng-new.md b/docs/api-workspace/schematics/ng-new.md index 001f8db2f8706..d3dca0663bd86 100644 --- a/docs/api-workspace/schematics/ng-new.md +++ b/docs/api-workspace/schematics/ng-new.md @@ -1,4 +1,4 @@ -# ng-new +# ng-new [hidden] Create a workspace diff --git a/packages/angular/src/schematics/application/application.ts b/packages/angular/src/schematics/application/application.ts index 602600ccbdf1a..e379302904256 100644 --- a/packages/angular/src/schematics/application/application.ts +++ b/packages/angular/src/schematics/application/application.ts @@ -26,7 +26,7 @@ import { } from '@nrwl/workspace'; import { formatFiles } from '@nrwl/workspace'; import { join, normalize } from '@angular-devkit/core'; -import { addE2eTestRunner, addUnitTestRunner } from '../ng-add/ng-add'; +import ngAdd from '../ng-add/ng-add'; import { addImportToModule, addImportToTestBed, @@ -317,10 +317,6 @@ function updateE2eProject(options: NormalizedSchema): Rule { }; } -function setupTestRunners(options: NormalizedSchema): Rule { - return chain([addUnitTestRunner(options), addE2eTestRunner(options)]); -} - export default function(schema: Schema): Rule { return (host: Tree, context: SchematicContext) => { const options = normalizeOptions(host, schema); @@ -337,7 +333,10 @@ export default function(schema: Schema): Rule { : `${options.name}/e2e`; return chain([ - setupTestRunners(options), + ngAdd({ + ...options, + skipFormat: true + }), externalSchematic('@schematics/angular', 'application', { name: options.name, inlineStyle: options.inlineStyle, diff --git a/packages/angular/src/schematics/ng-add/ng-add.spec.ts b/packages/angular/src/schematics/ng-add/ng-add.spec.ts index 034ed7d00ea4e..df4f9c758b9c2 100644 --- a/packages/angular/src/schematics/ng-add/ng-add.spec.ts +++ b/packages/angular/src/schematics/ng-add/ng-add.spec.ts @@ -1,7 +1,7 @@ import { Tree } from '@angular-devkit/schematics'; -import { runSchematic } from '../../utils/testing'; +import { runSchematic, callRule } from '../../utils/testing'; import { createEmptyWorkspace } from '@nrwl/workspace/testing'; -import { readJsonInTree } from '@nrwl/workspace'; +import { readJsonInTree, updateJsonInTree } from '@nrwl/workspace'; describe('ng-add', () => { let appTree: Tree; @@ -193,4 +193,44 @@ describe('ng-add', () => { }); }); }); + + describe('defaultCollection', () => { + it('should be set if none was set before', async () => { + const result = await runSchematic('ng-add', {}, appTree); + const angularJson = readJsonInTree(result, 'angular.json'); + expect(angularJson.cli.defaultCollection).toEqual('@nrwl/react'); + }); + + it('should be set if @nrwl/workspace was set before', async () => { + appTree = await callRule( + updateJsonInTree('angular.json', json => { + json.cli = { + defaultCollection: '@nrwl/workspace' + }; + + return json; + }), + appTree + ); + const result = await runSchematic('ng-add', {}, appTree); + const angularJson = readJsonInTree(result, 'angular.json'); + expect(angularJson.cli.defaultCollection).toEqual('@nrwl/react'); + }); + + it('should not be set if something else was set before', async () => { + appTree = await callRule( + updateJsonInTree('angular.json', json => { + json.cli = { + defaultCollection: '@nrwl/angular' + }; + + return json; + }), + appTree + ); + const result = await runSchematic('ng-add', {}, appTree); + const angularJson = readJsonInTree(result, 'angular.json'); + expect(angularJson.cli.defaultCollection).toEqual('@nrwl/angular'); + }); + }); }); diff --git a/packages/angular/src/schematics/ng-add/ng-add.ts b/packages/angular/src/schematics/ng-add/ng-add.ts index 169a2ce0b00b8..e495022b1d10e 100755 --- a/packages/angular/src/schematics/ng-add/ng-add.ts +++ b/packages/angular/src/schematics/ng-add/ng-add.ts @@ -9,7 +9,8 @@ import { import { readJsonInTree, addDepsToPackageJson, - updateJsonInTree + updateWorkspace, + formatFiles } from '@nrwl/workspace'; import { angularVersion, @@ -19,6 +20,7 @@ import { import { Schema } from './schema'; import { UnitTestRunner, E2eTestRunner } from '../../utils/test-runners'; import { jestPresetAngularVersion } from '../../utils/versions'; +import { JsonObject } from '@angular-devkit/core'; function updateDependencies(): Rule { const deps = { @@ -117,31 +119,41 @@ export function addE2eTestRunner(options: Pick): Rule { } } -function setDefaults(options: Schema): Rule { - return updateJsonInTree('angular.json', json => { - json.schematics = json.schematics || {}; - json.schematics['@nrwl/angular:application'] = - json.schematics['@nrwl/angular:application'] || {}; - json.schematics['@nrwl/angular:application'] = { - ...json.schematics['@nrwl/angular:application'], +export function setDefaults(options: Schema): Rule { + return updateWorkspace(workspace => { + workspace.extensions.schematics = workspace.extensions.schematics || {}; + workspace.extensions.schematics['@nrwl/angular:application'] = + workspace.extensions.schematics['@nrwl/angular:application'] || {}; + workspace.extensions.schematics['@nrwl/angular:application'] = { + ...workspace.extensions.schematics['@nrwl/angular:application'], unitTestRunner: options.unitTestRunner, e2eTestRunner: options.e2eTestRunner }; - json.schematics['@nrwl/angular:library'] = - json.schematics['@nrwl/angular:library'] || {}; - json.schematics['@nrwl/angular:library'] = { - ...json.schematics['@nrwl/angular:library'], + workspace.extensions.schematics['@nrwl/angular:library'] = + workspace.extensions.schematics['@nrwl/angular:library'] || {}; + workspace.extensions.schematics['@nrwl/angular:library'] = { + ...workspace.extensions.schematics['@nrwl/angular:library'], unitTestRunner: options.unitTestRunner }; - return json; + + workspace.extensions.cli = workspace.extensions.cli || {}; + const defaultCollection: string = + workspace.extensions.cli && + ((workspace.extensions.cli as JsonObject).defaultCollection as string); + + if (!defaultCollection || defaultCollection === '@nrwl/workspace') { + (workspace.extensions.cli as JsonObject).defaultCollection = + '@nrwl/react'; + } }); } export default function(options: Schema): Rule { return chain([ + setDefaults(options), updateDependencies(), addUnitTestRunner(options), addE2eTestRunner(options), - setDefaults(options) + formatFiles() ]); } diff --git a/packages/angular/src/schematics/ng-add/schema.d.ts b/packages/angular/src/schematics/ng-add/schema.d.ts index 2b6c02edb9385..d47c00b0a8e72 100644 --- a/packages/angular/src/schematics/ng-add/schema.d.ts +++ b/packages/angular/src/schematics/ng-add/schema.d.ts @@ -2,5 +2,6 @@ import { UnitTestRunner } from '../../utils/test-runners'; export interface Schema { unitTestRunner: UnitTestRunner; e2eTestRunner: E2eTestRunner; + skipFormat: boolean; skipInstall?: boolean; } diff --git a/packages/angular/src/schematics/ng-add/schema.json b/packages/angular/src/schematics/ng-add/schema.json index abb22552fa011..af99e9c8e7ba6 100644 --- a/packages/angular/src/schematics/ng-add/schema.json +++ b/packages/angular/src/schematics/ng-add/schema.json @@ -49,6 +49,11 @@ "type": "boolean", "description": "Skip installing after adding @nrwl/workspace", "default": false + }, + "skipFormat": { + "description": "Skip formatting files", + "type": "boolean", + "default": false } } } diff --git a/packages/express/src/schematics/application/application.ts b/packages/express/src/schematics/application/application.ts index 9178f01c2fa7a..a8bbecba9994d 100644 --- a/packages/express/src/schematics/application/application.ts +++ b/packages/express/src/schematics/application/application.ts @@ -56,7 +56,7 @@ export default function(schema: Schema): Rule { return (host: Tree, context: SchematicContext) => { const options = normalizeOptions(schema); return chain([ - ngAdd(), + ngAdd({ skipFormat: true }), externalSchematic('@nrwl/node', 'application', schema), addMainFile(options), addTypes(options) diff --git a/packages/express/src/schematics/ng-add/ng-add.spec.ts b/packages/express/src/schematics/ng-add/ng-add.spec.ts index 239b5f7e95aae..e311de495732f 100644 --- a/packages/express/src/schematics/ng-add/ng-add.spec.ts +++ b/packages/express/src/schematics/ng-add/ng-add.spec.ts @@ -2,7 +2,8 @@ import { Tree } from '@angular-devkit/schematics'; import { createEmptyWorkspace } from '@nrwl/workspace/testing'; import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; import { join } from 'path'; -import { readJsonInTree } from '@nrwl/workspace'; +import { readJsonInTree, updateJsonInTree } from '@nrwl/workspace'; +import { callRule, runSchematic } from '../../utils/testing'; describe('ng-add', () => { let tree: Tree; @@ -27,4 +28,44 @@ describe('ng-add', () => { expect(packageJson.dependencies['express']).toBeDefined(); expect(packageJson.devDependencies['@types/express']).toBeDefined(); }); + + describe('defaultCollection', () => { + it('should be set if none was set before', async () => { + const result = await runSchematic('ng-add', {}, tree); + const angularJson = readJsonInTree(result, 'angular.json'); + expect(angularJson.cli.defaultCollection).toEqual('@nrwl/express'); + }); + + it('should be set if @nrwl/workspace was set before', async () => { + tree = await callRule( + updateJsonInTree('angular.json', json => { + json.cli = { + defaultCollection: '@nrwl/workspace' + }; + + return json; + }), + tree + ); + const result = await runSchematic('ng-add', {}, tree); + const angularJson = readJsonInTree(result, 'angular.json'); + expect(angularJson.cli.defaultCollection).toEqual('@nrwl/express'); + }); + + it('should not be set if something else was set before', async () => { + tree = await callRule( + updateJsonInTree('angular.json', json => { + json.cli = { + defaultCollection: '@nrwl/angular' + }; + + return json; + }), + tree + ); + const result = await runSchematic('ng-add', {}, tree); + const angularJson = readJsonInTree(result, 'angular.json'); + expect(angularJson.cli.defaultCollection).toEqual('@nrwl/angular'); + }); + }); }); diff --git a/packages/express/src/schematics/ng-add/ng-add.ts b/packages/express/src/schematics/ng-add/ng-add.ts index 98972462f7768..7cb2ca0c92d80 100644 --- a/packages/express/src/schematics/ng-add/ng-add.ts +++ b/packages/express/src/schematics/ng-add/ng-add.ts @@ -1,21 +1,18 @@ -import { - Rule, - chain, - externalSchematic, - Tree, - noop -} from '@angular-devkit/schematics'; +import { Rule, chain } from '@angular-devkit/schematics'; import { addDepsToPackageJson, updateJsonInTree, - readJsonInTree, - addPackageWithNgAdd + addPackageWithNgAdd, + updateWorkspace, + formatFiles } from '@nrwl/workspace'; +import { Schema } from './schema'; import { expressTypingsVersion, expressVersion, nxVersion } from '../../utils/versions'; +import { JsonObject } from '@angular-devkit/core'; function addDependencies(): Rule { return addDepsToPackageJson( @@ -38,11 +35,28 @@ function moveDependency(): Rule { }); } -export default function() { +function setDefault(): Rule { + return updateWorkspace(workspace => { + workspace.extensions.cli = workspace.extensions.cli || {}; + + const defaultCollection: string = + workspace.extensions.cli && + ((workspace.extensions.cli as JsonObject).defaultCollection as string); + + if (!defaultCollection || defaultCollection === '@nrwl/workspace') { + (workspace.extensions.cli as JsonObject).defaultCollection = + '@nrwl/express'; + } + }); +} + +export default function(schema: Schema) { return chain([ + setDefault(), addPackageWithNgAdd('@nrwl/node'), addPackageWithNgAdd('@nrwl/jest'), addDependencies(), - moveDependency() + moveDependency(), + formatFiles(schema) ]); } diff --git a/packages/express/src/schematics/ng-add/schema.d.ts b/packages/express/src/schematics/ng-add/schema.d.ts index e53f1202a2dbc..b514cc1795a13 100644 --- a/packages/express/src/schematics/ng-add/schema.d.ts +++ b/packages/express/src/schematics/ng-add/schema.d.ts @@ -1 +1,3 @@ -export interface Schema {} +export interface Schema { + skipFormat: boolean; +} diff --git a/packages/express/src/schematics/ng-add/schema.json b/packages/express/src/schematics/ng-add/schema.json index 6a9d40bedc089..9d0332a5fc8ea 100644 --- a/packages/express/src/schematics/ng-add/schema.json +++ b/packages/express/src/schematics/ng-add/schema.json @@ -3,6 +3,12 @@ "id": "NxExpressNgAdd", "title": "Add Nx Express Schematics", "type": "object", - "properties": {}, + "properties": { + "skipFormat": { + "description": "Skip formatting files", + "type": "boolean", + "default": false + } + }, "required": [] } diff --git a/packages/express/src/utils/testing.ts b/packages/express/src/utils/testing.ts index 830f90cb95178..ef7dc45241367 100644 --- a/packages/express/src/utils/testing.ts +++ b/packages/express/src/utils/testing.ts @@ -1,6 +1,6 @@ import { join } from 'path'; import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; -import { Tree } from '@angular-devkit/schematics'; +import { Rule, Tree } from '@angular-devkit/schematics'; const testRunner = new SchematicTestRunner( '@nrwl/express', @@ -10,3 +10,7 @@ const testRunner = new SchematicTestRunner( export function runSchematic(schematicName: string, options: any, tree: Tree) { return testRunner.runSchematicAsync(schematicName, options, tree).toPromise(); } + +export function callRule(rule: Rule, tree: Tree) { + return testRunner.callRule(rule, tree).toPromise(); +} diff --git a/packages/nest/src/schematics/application/application.ts b/packages/nest/src/schematics/application/application.ts index 4821a2cd8601d..2b96594783845 100644 --- a/packages/nest/src/schematics/application/application.ts +++ b/packages/nest/src/schematics/application/application.ts @@ -65,7 +65,9 @@ export default function(schema: Schema): Rule { return (host: Tree, context: SchematicContext) => { const options = normalizeOptions(schema); return chain([ - ngAdd(), + ngAdd({ + skipFormat: true + }), externalSchematic('@nrwl/node', 'application', schema), addMainFile(options), addAppFiles(options) diff --git a/packages/nest/src/schematics/ng-add/ng-add.spec.ts b/packages/nest/src/schematics/ng-add/ng-add.spec.ts index 3e09a48232193..bb48dd8de1feb 100644 --- a/packages/nest/src/schematics/ng-add/ng-add.spec.ts +++ b/packages/nest/src/schematics/ng-add/ng-add.spec.ts @@ -1,30 +1,62 @@ import { Tree } from '@angular-devkit/schematics'; import { createEmptyWorkspace } from '@nrwl/workspace/testing'; -import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; -import { join } from 'path'; -import { readJsonInTree } from '@nrwl/workspace'; +import { readJsonInTree, updateJsonInTree } from '@nrwl/workspace'; +import { runSchematic, callRule } from '../../utils/testing'; describe('ng-add', () => { let tree: Tree; - let testRunner: SchematicTestRunner; beforeEach(() => { tree = Tree.empty(); tree = createEmptyWorkspace(tree); - testRunner = new SchematicTestRunner( - '@nrwl/nest', - join(__dirname, '../../../collection.json') - ); }); it('should add dependencies', async () => { - const result = await testRunner - .runSchematicAsync('ng-add', {}, tree) - .toPromise(); + const result = await runSchematic('ng-add', {}, tree); const packageJson = readJsonInTree(result, 'package.json'); expect(packageJson.dependencies['@nrwl/nest']).toBeUndefined(); expect(packageJson.devDependencies['@nrwl/nest']).toBeDefined(); expect(packageJson.dependencies['@nestjs/core']).toBeDefined(); }); + + describe('defaultCollection', () => { + it('should be set if none was set before', async () => { + const result = await runSchematic('ng-add', {}, tree); + const angularJson = readJsonInTree(result, 'angular.json'); + expect(angularJson.cli.defaultCollection).toEqual('@nrwl/nest'); + }); + + it('should be set if @nrwl/workspace was set before', async () => { + tree = await callRule( + updateJsonInTree('angular.json', json => { + json.cli = { + defaultCollection: '@nrwl/workspace' + }; + + return json; + }), + tree + ); + const result = await runSchematic('ng-add', {}, tree); + const angularJson = readJsonInTree(result, 'angular.json'); + expect(angularJson.cli.defaultCollection).toEqual('@nrwl/nest'); + }); + + it('should not be set if something else was set before', async () => { + tree = await callRule( + updateJsonInTree('angular.json', json => { + json.cli = { + defaultCollection: '@nrwl/angular' + }; + + return json; + }), + tree + ); + const result = await runSchematic('ng-add', {}, tree); + const angularJson = readJsonInTree(result, 'angular.json'); + expect(angularJson.cli.defaultCollection).toEqual('@nrwl/angular'); + }); + }); }); diff --git a/packages/nest/src/schematics/ng-add/ng-add.ts b/packages/nest/src/schematics/ng-add/ng-add.ts index c6a0e90c5fe45..3e78b0b779ed4 100644 --- a/packages/nest/src/schematics/ng-add/ng-add.ts +++ b/packages/nest/src/schematics/ng-add/ng-add.ts @@ -2,8 +2,11 @@ import { chain, Rule } from '@angular-devkit/schematics'; import { addDepsToPackageJson, addPackageWithNgAdd, - updateJsonInTree + formatFiles, + updateJsonInTree, + updateWorkspace } from '@nrwl/workspace'; +import { Schema } from './schema'; import { expressTypingsVersion, nestJsSchematicsVersion, @@ -11,6 +14,7 @@ import { nxVersion, reflectMetadataVersion } from '../../utils/versions'; +import { JsonObject } from '@angular-devkit/core'; export function addDependencies(): Rule { return addDepsToPackageJson( @@ -38,11 +42,27 @@ function moveDependency(): Rule { }); } -export default function() { +function setDefault(): Rule { + return updateWorkspace(workspace => { + workspace.extensions.cli = workspace.extensions.cli || {}; + + const defaultCollection: string = + workspace.extensions.cli && + ((workspace.extensions.cli as JsonObject).defaultCollection as string); + + if (!defaultCollection || defaultCollection === '@nrwl/workspace') { + (workspace.extensions.cli as JsonObject).defaultCollection = '@nrwl/nest'; + } + }); +} + +export default function(schema: Schema) { return chain([ + setDefault(), addPackageWithNgAdd('@nrwl/node'), addPackageWithNgAdd('@nrwl/jest'), addDependencies(), - moveDependency() + moveDependency(), + formatFiles(schema) ]); } diff --git a/packages/nest/src/schematics/ng-add/schema.d.ts b/packages/nest/src/schematics/ng-add/schema.d.ts index e53f1202a2dbc..b514cc1795a13 100644 --- a/packages/nest/src/schematics/ng-add/schema.d.ts +++ b/packages/nest/src/schematics/ng-add/schema.d.ts @@ -1 +1,3 @@ -export interface Schema {} +export interface Schema { + skipFormat: boolean; +} diff --git a/packages/nest/src/schematics/ng-add/schema.json b/packages/nest/src/schematics/ng-add/schema.json index 29d0c93d6f8ba..4779f24ca99c7 100644 --- a/packages/nest/src/schematics/ng-add/schema.json +++ b/packages/nest/src/schematics/ng-add/schema.json @@ -3,6 +3,12 @@ "id": "NxNestNgAdd", "title": "Add Nx Nest Schematics", "type": "object", - "properties": {}, + "properties": { + "skipFormat": { + "description": "Skip formatting files", + "type": "boolean", + "default": false + } + }, "required": [] } diff --git a/packages/nest/src/utils/testing.ts b/packages/nest/src/utils/testing.ts index a85df7d7f0539..db9da1ab8a207 100644 --- a/packages/nest/src/utils/testing.ts +++ b/packages/nest/src/utils/testing.ts @@ -1,6 +1,6 @@ import { join } from 'path'; import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; -import { Tree } from '@angular-devkit/schematics'; +import { Rule, Tree } from '@angular-devkit/schematics'; const testRunner = new SchematicTestRunner( '@nrwl/nest', @@ -10,3 +10,7 @@ const testRunner = new SchematicTestRunner( export function runSchematic(schematicName: string, options: any, tree: Tree) { return testRunner.runSchematicAsync(schematicName, options, tree).toPromise(); } + +export function callRule(rule: Rule, tree: Tree) { + return testRunner.callRule(rule, tree).toPromise(); +} diff --git a/packages/node/src/schematics/application/application.ts b/packages/node/src/schematics/application/application.ts index 1826dcee4ae08..540b02223519e 100644 --- a/packages/node/src/schematics/application/application.ts +++ b/packages/node/src/schematics/application/application.ts @@ -146,7 +146,9 @@ export default function(schema: Schema): Rule { return (host: Tree, context: SchematicContext) => { const options = normalizeOptions(schema); return chain([ - ngAdd(), + ngAdd({ + skipFormat: true + }), addAppFiles(options), updateAngularJson(options), updateNxJson(options), diff --git a/packages/node/src/schematics/ng-add/ng-add.spec.ts b/packages/node/src/schematics/ng-add/ng-add.spec.ts index fe27023c1a722..4f61c940c52f7 100644 --- a/packages/node/src/schematics/ng-add/ng-add.spec.ts +++ b/packages/node/src/schematics/ng-add/ng-add.spec.ts @@ -1,28 +1,60 @@ import { Tree } from '@angular-devkit/schematics'; import { createEmptyWorkspace } from '@nrwl/workspace/testing'; -import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; -import { join } from 'path'; -import { readJsonInTree } from '@nrwl/workspace'; +import { readJsonInTree, updateJsonInTree } from '@nrwl/workspace'; +import { callRule, runSchematic } from '../../utils/testing'; describe('ng-add', () => { let tree: Tree; - let testRunner: SchematicTestRunner; beforeEach(() => { tree = Tree.empty(); tree = createEmptyWorkspace(tree); - testRunner = new SchematicTestRunner( - '@nrwl/node', - join(__dirname, '../../../collection.json') - ); }); it('should add dependencies', async () => { - const result = await testRunner - .runSchematicAsync('ng-add', {}, tree) - .toPromise(); + const result = await runSchematic('ng-add', {}, tree); const packageJson = readJsonInTree(result, 'package.json'); expect(packageJson.dependencies['@nrwl/node']).toBeUndefined(); expect(packageJson.devDependencies['@nrwl/node']).toBeDefined(); }); + + describe('defaultCollection', () => { + it('should be set if none was set before', async () => { + const result = await runSchematic('ng-add', {}, tree); + const angularJson = readJsonInTree(result, 'angular.json'); + expect(angularJson.cli.defaultCollection).toEqual('@nrwl/node'); + }); + + it('should be set if @nrwl/workspace was set before', async () => { + tree = await callRule( + updateJsonInTree('angular.json', json => { + json.cli = { + defaultCollection: '@nrwl/workspace' + }; + + return json; + }), + tree + ); + const result = await runSchematic('ng-add', {}, tree); + const angularJson = readJsonInTree(result, 'angular.json'); + expect(angularJson.cli.defaultCollection).toEqual('@nrwl/node'); + }); + + it('should not be set if something else was set before', async () => { + tree = await callRule( + updateJsonInTree('angular.json', json => { + json.cli = { + defaultCollection: '@nrwl/angular' + }; + + return json; + }), + tree + ); + const result = await runSchematic('ng-add', {}, tree); + const angularJson = readJsonInTree(result, 'angular.json'); + expect(angularJson.cli.defaultCollection).toEqual('@nrwl/angular'); + }); + }); }); diff --git a/packages/node/src/schematics/ng-add/ng-add.ts b/packages/node/src/schematics/ng-add/ng-add.ts index 955e4ee75a7dd..da2f978a0f8d0 100644 --- a/packages/node/src/schematics/ng-add/ng-add.ts +++ b/packages/node/src/schematics/ng-add/ng-add.ts @@ -2,9 +2,13 @@ import { Rule, chain } from '@angular-devkit/schematics'; import { addDepsToPackageJson, updateJsonInTree, - addPackageWithNgAdd + addPackageWithNgAdd, + updateWorkspace, + formatFiles } from '@nrwl/workspace'; +import { Schema } from './schema'; import { nxVersion } from '../../utils/versions'; +import { JsonObject } from '@angular-devkit/core'; function addDependencies(): Rule { return addDepsToPackageJson( @@ -24,10 +28,26 @@ function moveDependency(): Rule { }); } -export default function() { +function setDefault(): Rule { + return updateWorkspace(workspace => { + workspace.extensions.cli = workspace.extensions.cli || {}; + + const defaultCollection: string = + workspace.extensions.cli && + ((workspace.extensions.cli as JsonObject).defaultCollection as string); + + if (!defaultCollection || defaultCollection === '@nrwl/workspace') { + (workspace.extensions.cli as JsonObject).defaultCollection = '@nrwl/node'; + } + }); +} + +export default function(schema: Schema) { return chain([ + setDefault(), addPackageWithNgAdd('@nrwl/jest'), addDependencies(), - moveDependency() + moveDependency(), + formatFiles(schema) ]); } diff --git a/packages/node/src/schematics/ng-add/schema.d.ts b/packages/node/src/schematics/ng-add/schema.d.ts index e53f1202a2dbc..b514cc1795a13 100644 --- a/packages/node/src/schematics/ng-add/schema.d.ts +++ b/packages/node/src/schematics/ng-add/schema.d.ts @@ -1 +1,3 @@ -export interface Schema {} +export interface Schema { + skipFormat: boolean; +} diff --git a/packages/node/src/schematics/ng-add/schema.json b/packages/node/src/schematics/ng-add/schema.json index 2053f43e3a363..af9ff34ed7f1b 100644 --- a/packages/node/src/schematics/ng-add/schema.json +++ b/packages/node/src/schematics/ng-add/schema.json @@ -3,6 +3,12 @@ "id": "NxNodeNgAdd", "title": "Add Nx Node Schematics", "type": "object", - "properties": {}, + "properties": { + "skipFormat": { + "description": "Skip formatting files", + "type": "boolean", + "default": false + } + }, "required": [] } diff --git a/packages/node/src/utils/testing.ts b/packages/node/src/utils/testing.ts index b3f61b77ea78a..1463551a58eae 100644 --- a/packages/node/src/utils/testing.ts +++ b/packages/node/src/utils/testing.ts @@ -1,6 +1,6 @@ import { join } from 'path'; import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; -import { Tree } from '@angular-devkit/schematics'; +import { Rule, Tree } from '@angular-devkit/schematics'; import { TestingArchitectHost, TestLogger @@ -18,6 +18,10 @@ export function runSchematic(schematicName: string, options: any, tree: Tree) { return testRunner.runSchematicAsync(schematicName, options, tree).toPromise(); } +export function callRule(rule: Rule, tree: Tree) { + return testRunner.callRule(rule, tree).toPromise(); +} + export async function getTestArchitect() { const architectHost = new TestingArchitectHost('/root', '/root'); const registry = new schema.CoreSchemaRegistry(); diff --git a/packages/react/src/schematics/application/application.ts b/packages/react/src/schematics/application/application.ts index 70a118c2c0b56..b31fb5fb3a785 100644 --- a/packages/react/src/schematics/application/application.ts +++ b/packages/react/src/schematics/application/application.ts @@ -3,7 +3,6 @@ import { chain, Rule, Tree, - SchematicContext, mergeWith, apply, template, @@ -148,7 +147,9 @@ export default function(schema: Schema): Rule { const options = normalizeOptions(host, schema); return chain([ - ngAdd(), + ngAdd({ + skipFormat: true + }), createApplicationFiles(options), updateNxJson(options), addProject(options), diff --git a/packages/react/src/schematics/ng-add/ng-add.spec.ts b/packages/react/src/schematics/ng-add/ng-add.spec.ts index 28d154ca707a5..6d9155d5ee01b 100644 --- a/packages/react/src/schematics/ng-add/ng-add.spec.ts +++ b/packages/react/src/schematics/ng-add/ng-add.spec.ts @@ -1,26 +1,19 @@ import { Tree } from '@angular-devkit/schematics'; import { createEmptyWorkspace } from '@nrwl/workspace/testing'; -import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; -import { join } from 'path'; import { readJsonInTree } from '@nrwl/workspace'; +import { updateJsonInTree } from '@nrwl/workspace'; +import { runSchematic, callRule } from '../../utils/testing'; describe('ng-add', () => { let tree: Tree; - let testRunner: SchematicTestRunner; beforeEach(() => { tree = Tree.empty(); tree = createEmptyWorkspace(tree); - testRunner = new SchematicTestRunner( - '@nrwl/react', - join(__dirname, '../../../collection.json') - ); }); it('should add react dependencies', async () => { - const result = await testRunner - .runSchematicAsync('ng-add', {}, tree) - .toPromise(); + const result = await runSchematic('ng-add', {}, tree); const packageJson = readJsonInTree(result, 'package.json'); expect(packageJson.dependencies['@nrwl/react']).toBeUndefined(); expect(packageJson.dependencies['react']).toBeDefined(); @@ -30,4 +23,44 @@ describe('ng-add', () => { expect(packageJson.devDependencies['@types/react-dom']).toBeDefined(); expect(packageJson.devDependencies['react-testing-library']).toBeDefined(); }); + + describe('defaultCollection', () => { + it('should be set if none was set before', async () => { + const result = await runSchematic('ng-add', {}, tree); + const angularJson = readJsonInTree(result, 'angular.json'); + expect(angularJson.cli.defaultCollection).toEqual('@nrwl/react'); + }); + + it('should be set if @nrwl/workspace was set before', async () => { + tree = await callRule( + updateJsonInTree('angular.json', json => { + json.cli = { + defaultCollection: '@nrwl/workspace' + }; + + return json; + }), + tree + ); + const result = await runSchematic('ng-add', {}, tree); + const angularJson = readJsonInTree(result, 'angular.json'); + expect(angularJson.cli.defaultCollection).toEqual('@nrwl/react'); + }); + + it('should not be set if something else was set before', async () => { + tree = await callRule( + updateJsonInTree('angular.json', json => { + json.cli = { + defaultCollection: '@nrwl/angular' + }; + + return json; + }), + tree + ); + const result = await runSchematic('ng-add', {}, tree); + const angularJson = readJsonInTree(result, 'angular.json'); + expect(angularJson.cli.defaultCollection).toEqual('@nrwl/angular'); + }); + }); }); diff --git a/packages/react/src/schematics/ng-add/ng-add.ts b/packages/react/src/schematics/ng-add/ng-add.ts index 346febdfeaeae..41c3cdcb92f1e 100644 --- a/packages/react/src/schematics/ng-add/ng-add.ts +++ b/packages/react/src/schematics/ng-add/ng-add.ts @@ -1,16 +1,11 @@ -import { - Rule, - chain, - externalSchematic, - noop, - Tree -} from '@angular-devkit/schematics'; +import { chain, Rule } from '@angular-devkit/schematics'; import { addDepsToPackageJson, updateJsonInTree, - readJsonInTree, - addPackageWithNgAdd + addPackageWithNgAdd, + updateWorkspace } from '@nrwl/workspace'; +import { Schema } from './schema'; import { frameworkVersion, typesVersion, @@ -18,6 +13,7 @@ import { testingLibraryVersion, nxVersion } from '../../utils/versions'; +import { JsonObject } from '@angular-devkit/core'; export function addDependencies(): Rule { return addDepsToPackageJson( @@ -43,8 +39,24 @@ function moveDependency(): Rule { }); } -export default function() { +function setDefault(): Rule { + return updateWorkspace(workspace => { + workspace.extensions.cli = workspace.extensions.cli || {}; + + const defaultCollection: string = + workspace.extensions.cli && + ((workspace.extensions.cli as JsonObject).defaultCollection as string); + + if (!defaultCollection || defaultCollection === '@nrwl/workspace') { + (workspace.extensions.cli as JsonObject).defaultCollection = + '@nrwl/react'; + } + }); +} + +export default function(schema: Schema) { return chain([ + setDefault(), addPackageWithNgAdd('@nrwl/jest'), addPackageWithNgAdd('@nrwl/cypress'), addPackageWithNgAdd('@nrwl/web'), diff --git a/packages/react/src/schematics/ng-add/schema.d.ts b/packages/react/src/schematics/ng-add/schema.d.ts index e53f1202a2dbc..b514cc1795a13 100644 --- a/packages/react/src/schematics/ng-add/schema.d.ts +++ b/packages/react/src/schematics/ng-add/schema.d.ts @@ -1 +1,3 @@ -export interface Schema {} +export interface Schema { + skipFormat: boolean; +} diff --git a/packages/react/src/schematics/ng-add/schema.json b/packages/react/src/schematics/ng-add/schema.json index 87d8ee8c89a27..a262d318691a4 100644 --- a/packages/react/src/schematics/ng-add/schema.json +++ b/packages/react/src/schematics/ng-add/schema.json @@ -3,6 +3,12 @@ "id": "NxReactNgAdd", "title": "Add Nx React Schematics", "type": "object", - "properties": {}, + "properties": { + "skipFormat": { + "description": "Skip formatting files", + "type": "boolean", + "default": false + } + }, "required": [] } diff --git a/packages/react/src/utils/testing.ts b/packages/react/src/utils/testing.ts index 09fe7461d7be9..5f4d715322c13 100644 --- a/packages/react/src/utils/testing.ts +++ b/packages/react/src/utils/testing.ts @@ -1,6 +1,6 @@ import { join } from 'path'; import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; -import { Tree } from '@angular-devkit/schematics'; +import { Rule, Tree } from '@angular-devkit/schematics'; const testRunner = new SchematicTestRunner( '@nrwl/react', @@ -10,3 +10,7 @@ const testRunner = new SchematicTestRunner( export function runSchematic(schematicName: string, options: any, tree: Tree) { return testRunner.runSchematicAsync(schematicName, options, tree).toPromise(); } + +export function callRule(rule: Rule, tree: Tree) { + return testRunner.callRule(rule, tree).toPromise(); +} diff --git a/packages/web/src/schematics/application/application.ts b/packages/web/src/schematics/application/application.ts index 13d574e40de04..8089290ce4348 100644 --- a/packages/web/src/schematics/application/application.ts +++ b/packages/web/src/schematics/application/application.ts @@ -148,7 +148,9 @@ export default function(schema: Schema): Rule { const options = normalizeOptions(host, schema); return chain([ - ngAdd(), + ngAdd({ + skipFormat: true + }), createApplicationFiles(options), updateNxJson(options), addProject(options), diff --git a/packages/web/src/schematics/ng-add/ng-add.spec.ts b/packages/web/src/schematics/ng-add/ng-add.spec.ts index 0c076806a1ec3..e3c062b51b92c 100644 --- a/packages/web/src/schematics/ng-add/ng-add.spec.ts +++ b/packages/web/src/schematics/ng-add/ng-add.spec.ts @@ -1,29 +1,62 @@ import { Tree } from '@angular-devkit/schematics'; import { createEmptyWorkspace } from '@nrwl/workspace/testing'; -import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; -import { join } from 'path'; import { readJsonInTree } from '@nrwl/workspace'; +import { callRule, runSchematic } from '../../utils/testing'; +import { updateJsonInTree } from '@nrwl/workspace/src/utils/ast-utils'; describe('ng-add', () => { let tree: Tree; - let testRunner: SchematicTestRunner; beforeEach(() => { tree = Tree.empty(); tree = createEmptyWorkspace(tree); - testRunner = new SchematicTestRunner( - '@nrwl/web', - join(__dirname, '../../../collection.json') - ); }); it('should add web dependencies', async () => { - const result = await testRunner - .runSchematicAsync('ng-add', {}, tree) - .toPromise(); + const result = await runSchematic('ng-add', {}, tree); const packageJson = readJsonInTree(result, 'package.json'); expect(packageJson.dependencies['@nrwl/web']).toBeUndefined(); expect(packageJson.dependencies['document-register-element']).toBeDefined(); expect(packageJson.devDependencies['@nrwl/web']).toBeDefined(); }); + + describe('defaultCollection', () => { + it('should be set if none was set before', async () => { + const result = await runSchematic('ng-add', {}, tree); + const angularJson = readJsonInTree(result, 'angular.json'); + expect(angularJson.cli.defaultCollection).toEqual('@nrwl/web'); + }); + + it('should be set if @nrwl/workspace was set before', async () => { + tree = await callRule( + updateJsonInTree('angular.json', json => { + json.cli = { + defaultCollection: '@nrwl/workspace' + }; + + return json; + }), + tree + ); + const result = await runSchematic('ng-add', {}, tree); + const angularJson = readJsonInTree(result, 'angular.json'); + expect(angularJson.cli.defaultCollection).toEqual('@nrwl/web'); + }); + + it('should not be set if something else was set before', async () => { + tree = await callRule( + updateJsonInTree('angular.json', json => { + json.cli = { + defaultCollection: '@nrwl/angular' + }; + + return json; + }), + tree + ); + const result = await runSchematic('ng-add', {}, tree); + const angularJson = readJsonInTree(result, 'angular.json'); + expect(angularJson.cli.defaultCollection).toEqual('@nrwl/angular'); + }); + }); }); diff --git a/packages/web/src/schematics/ng-add/ng-add.ts b/packages/web/src/schematics/ng-add/ng-add.ts index 5c49dc922b52d..af8597828f3de 100644 --- a/packages/web/src/schematics/ng-add/ng-add.ts +++ b/packages/web/src/schematics/ng-add/ng-add.ts @@ -1,10 +1,17 @@ import { Rule, chain } from '@angular-devkit/schematics'; -import { updateJsonInTree, addPackageWithNgAdd } from '@nrwl/workspace'; +import { + updateJsonInTree, + addPackageWithNgAdd, + formatFiles +} from '@nrwl/workspace'; import { addDepsToPackageJson } from '@nrwl/workspace'; +import { Schema } from './schema'; import { nxVersion, documentRegisterElementVersion } from '../../utils/versions'; +import { updateWorkspace } from '@nrwl/workspace'; +import { JsonObject } from '@angular-devkit/core'; function addDependencies(): Rule { return addDepsToPackageJson( @@ -26,11 +33,27 @@ function moveDependency(): Rule { }); } -export default function() { +function setDefault(): Rule { + return updateWorkspace(workspace => { + workspace.extensions.cli = workspace.extensions.cli || {}; + + const defaultCollection: string = + workspace.extensions.cli && + ((workspace.extensions.cli as JsonObject).defaultCollection as string); + + if (!defaultCollection || defaultCollection === '@nrwl/workspace') { + (workspace.extensions.cli as JsonObject).defaultCollection = '@nrwl/web'; + } + }); +} + +export default function(schema: Schema) { return chain([ + setDefault(), addPackageWithNgAdd('@nrwl/jest'), addPackageWithNgAdd('@nrwl/cypress'), addDependencies(), - moveDependency() + moveDependency(), + formatFiles(schema) ]); } diff --git a/packages/web/src/schematics/ng-add/schema.d.ts b/packages/web/src/schematics/ng-add/schema.d.ts index e53f1202a2dbc..b514cc1795a13 100644 --- a/packages/web/src/schematics/ng-add/schema.d.ts +++ b/packages/web/src/schematics/ng-add/schema.d.ts @@ -1 +1,3 @@ -export interface Schema {} +export interface Schema { + skipFormat: boolean; +} diff --git a/packages/web/src/schematics/ng-add/schema.json b/packages/web/src/schematics/ng-add/schema.json index 8bd78f9244e36..6cfb59cb91819 100644 --- a/packages/web/src/schematics/ng-add/schema.json +++ b/packages/web/src/schematics/ng-add/schema.json @@ -3,6 +3,12 @@ "id": "NxWebNgAdd", "title": "Add Nx Web Schematics", "type": "object", - "properties": {}, + "properties": { + "skipFormat": { + "description": "Skip formatting files", + "type": "boolean", + "default": false + } + }, "required": [] } diff --git a/packages/web/src/utils/testing.ts b/packages/web/src/utils/testing.ts index 19973a1939936..bed99dd421fa0 100644 --- a/packages/web/src/utils/testing.ts +++ b/packages/web/src/utils/testing.ts @@ -1,6 +1,6 @@ import { join } from 'path'; import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; -import { Tree } from '@angular-devkit/schematics'; +import { Rule, Tree } from '@angular-devkit/schematics'; import { BuilderContext, Architect, @@ -22,6 +22,10 @@ export function runSchematic(schematicName: string, options: any, tree: Tree) { return testRunner.runSchematicAsync(schematicName, options, tree).toPromise(); } +export function callRule(rule: Rule, tree: Tree) { + return testRunner.callRule(rule, tree).toPromise(); +} + export async function getTestArchitect() { const architectHost = new TestingArchitectHost('/root', '/root'); const registry = new schema.CoreSchemaRegistry(); diff --git a/packages/workspace/index.ts b/packages/workspace/index.ts index 0dbff3340eebe..c51aec382c1a6 100644 --- a/packages/workspace/index.ts +++ b/packages/workspace/index.ts @@ -46,6 +46,8 @@ export { serializeTarget } from './src/utils/cli-config-utils'; +export { getWorkspace, updateWorkspace } from './src/utils/workspace'; + export { formatFiles } from './src/utils/rules/format-files'; export { deleteFile } from './src/utils/rules/deleteFile'; export * from './src/utils/rules/ng-add';