Skip to content

Commit

Permalink
feat(schematics): Add ng-add support with prompt for making our schem…
Browse files Browse the repository at this point in the history
…atics default (#1552)
  • Loading branch information
itayod authored and brandonroberts committed Feb 20, 2019
1 parent 9d3597a commit 01ff157
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 8 deletions.
23 changes: 15 additions & 8 deletions modules/schematics/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,59 @@
"extends": ["@schematics/angular"],
"schematics": {
"action": {
"aliases": [ "a" ],
"aliases": ["a"],
"factory": "./src/action",
"schema": "./src/action/schema.json",
"description": "Add store actions"
},

"container": {
"aliases": [ "co" ],
"aliases": ["co"],
"factory": "./src/container",
"schema": "./src/container/schema.json",
"description": "Add store container component"
},

"effect": {
"aliases": [ "ef" ],
"aliases": ["ef"],
"factory": "./src/effect",
"schema": "./src/effect/schema.json",
"description": "Add side effect class"
},

"entity": {
"aliases": [ "en" ],
"aliases": ["en"],
"factory": "./src/entity",
"schema": "./src/entity/schema.json",
"description": "Add entity state"
},

"feature": {
"aliases": [ "f" ],
"aliases": ["f"],
"factory": "./src/feature",
"schema": "./src/feature/schema.json",
"description": "Add feature state"
},

"reducer": {
"aliases": [ "r" ],
"aliases": ["r"],
"factory": "./src/reducer",
"schema": "./src/reducer/schema.json",
"description": "Add state reducer"
},

"store": {
"aliases": [ "st" ],
"aliases": ["st"],
"factory": "./src/store",
"schema": "./src/store/schema.json",
"description": "Adds initial setup for state managment"
"description": "Adds initial setup for state management"
},

"ng-add": {
"aliases": ["init"],
"factory": "./src/ng-add",
"schema": "./src/ng-add/schema.json",
"description": "Installs the NgRx schematics package"
}
}
}
45 changes: 45 additions & 0 deletions modules/schematics/src/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
SchematicTestRunner,
UnitTestTree,
} from '@angular-devkit/schematics/testing';
import * as path from 'path';
import { createWorkspace } from '../../../schematics-core/testing';
import { Schema as SchematicOptions } from './schema';

describe('ng-add Schematic', () => {
const schematicRunner = new SchematicTestRunner(
'@ngrx/schematics',
path.join(__dirname, '../../collection.json')
);
const defaultOptions: SchematicOptions = {
defaultCollection: true,
};

let appTree: UnitTestTree;

beforeEach(() => {
appTree = createWorkspace(schematicRunner, appTree);
});

it(`should leave the workspace's cli as default`, () => {
const options: SchematicOptions = {
...defaultOptions,
defaultCollection: false,
};

const tree = schematicRunner.runSchematic('ng-add', options, appTree);
const workspace = JSON.parse(tree.readContent('/angular.json'));
expect(workspace.cli).not.toBeDefined();
});

it('should set workspace default cli to @ngrx/schematics', () => {
const options: SchematicOptions = {
...defaultOptions,
defaultCollection: true,
};

const tree = schematicRunner.runSchematic('ng-add', options, appTree);
const workspace = JSON.parse(tree.readContent('/angular.json'));
expect(workspace.cli.defaultCollection).toEqual('@ngrx/schematics');
});
});
38 changes: 38 additions & 0 deletions modules/schematics/src/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
Rule,
SchematicContext,
Tree,
chain,
noop,
} from '@angular-devkit/schematics';
import {
WorkspaceSchema,
getWorkspacePath,
getWorkspace,
} from '../../schematics-core/utility/config';
import { Schema as SchematicOptions } from './schema';

function updateWorkspace(host: Tree, key: keyof WorkspaceSchema, value: any) {
const workspace = getWorkspace(host);
const path = getWorkspacePath(host);
workspace[key] = value;
host.overwrite(path, JSON.stringify(workspace, null, 2));
}

function setAsDefaultSchematics() {
const cli = {
defaultCollection: '@ngrx/schematics',
};
return (host: Tree) => {
updateWorkspace(host, 'cli', cli);
return host;
};
}

export default function(options: SchematicOptions): Rule {
return (host: Tree, context: SchematicContext) => {
return chain([
options && options.defaultCollection ? setAsDefaultSchematics() : noop(),
])(host, context);
};
}
17 changes: 17 additions & 0 deletions modules/schematics/src/ng-add/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "http://json-schema.org/schema",
"id": "SchematicsNgRxSchematics",
"title": "Scaffolding library for Angular applications using NgRx libraries",
"type": "object",
"properties": {
"defaultCollection": {
"type": "boolean",
"default": true,
"description": "Use @ngrx/schematics as the default collection",
"x-prompt":
"Do you want to use @ngrx/schematics as the default collection?",
"alias": "d"
}
},
"required": []
}
3 changes: 3 additions & 0 deletions modules/schematics/src/ng-add/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface Schema {
defaultCollection?: boolean;
}

0 comments on commit 01ff157

Please sign in to comment.