Skip to content

Commit

Permalink
feat(nx): set default collection on ngadd
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored and vsavkin committed May 21, 2019
1 parent 2d4d735 commit a5351c9
Show file tree
Hide file tree
Showing 46 changed files with 563 additions and 127 deletions.
8 changes: 8 additions & 0 deletions docs/api-angular/schematics/ng-add.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion docs/api-cypress/schematics/cypress-project.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# cypress-project
# cypress-project [hidden]

Add a Cypress E2E Project

Expand Down
10 changes: 10 additions & 0 deletions docs/api-express/schematics/ng-add.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@ Add @nrwl/express to a project
ng generate ng-add ...

```

## Options

### skipFormat

Default: `false`

Type: `boolean`

Skip formatting files
2 changes: 1 addition & 1 deletion docs/api-jest/schematics/jest-project.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# jest-project
# jest-project [hidden]

Add Jest configuration to a project

Expand Down
10 changes: 10 additions & 0 deletions docs/api-nest/schematics/ng-add.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@ Add @nrwl/nest to a project
ng generate ng-add ...

```

## Options

### skipFormat

Default: `false`

Type: `boolean`

Skip formatting files
10 changes: 10 additions & 0 deletions docs/api-node/schematics/ng-add.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@ Add @nrwl/node to a project
ng generate ng-add ...

```

## Options

### skipFormat

Default: `false`

Type: `boolean`

Skip formatting files
10 changes: 10 additions & 0 deletions docs/api-react/schematics/ng-add.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@ Add @nrwl/react to a project
ng generate ng-add ...

```

## Options

### skipFormat

Default: `false`

Type: `boolean`

Skip formatting files
10 changes: 10 additions & 0 deletions docs/api-web/schematics/ng-add.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@ Add @nrwl/web to a project
ng generate ng-add ...

```

## Options

### skipFormat

Default: `false`

Type: `boolean`

Skip formatting files
8 changes: 0 additions & 8 deletions docs/api-workspace/schematics/library.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion docs/api-workspace/schematics/ng-new.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ng-new
# ng-new [hidden]

Create a workspace

Expand Down
11 changes: 5 additions & 6 deletions packages/angular/src/schematics/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down
44 changes: 42 additions & 2 deletions packages/angular/src/schematics/ng-add/ng-add.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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');
});
});
});
40 changes: 26 additions & 14 deletions packages/angular/src/schematics/ng-add/ng-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
import {
readJsonInTree,
addDepsToPackageJson,
updateJsonInTree
updateWorkspace,
formatFiles
} from '@nrwl/workspace';
import {
angularVersion,
Expand All @@ -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 = {
Expand Down Expand Up @@ -117,31 +119,41 @@ export function addE2eTestRunner(options: Pick<Schema, 'e2eTestRunner'>): 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()
]);
}
1 change: 1 addition & 0 deletions packages/angular/src/schematics/ng-add/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import { UnitTestRunner } from '../../utils/test-runners';
export interface Schema {
unitTestRunner: UnitTestRunner;
e2eTestRunner: E2eTestRunner;
skipFormat: boolean;
skipInstall?: boolean;
}
5 changes: 5 additions & 0 deletions packages/angular/src/schematics/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
"type": "boolean",
"description": "Skip installing after adding @nrwl/workspace",
"default": false
},
"skipFormat": {
"description": "Skip formatting files",
"type": "boolean",
"default": false
}
}
}
2 changes: 1 addition & 1 deletion packages/express/src/schematics/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 42 additions & 1 deletion packages/express/src/schematics/ng-add/ng-add.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
});
});
});
Loading

0 comments on commit a5351c9

Please sign in to comment.