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 as default
  • Loading branch information
itayod committed Feb 15, 2019
1 parent e07ae3d commit 6929ff4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
15 changes: 15 additions & 0 deletions modules/store/schematics-core/utility/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,18 @@ export function getWorkspace(host: Tree): WorkspaceSchema {

return JSON.parse(config);
}

export function setWorkspace(host: Tree, workSpace: WorkspaceSchema) {
const path = getWorkspacePath(host);
host.overwrite(path, JSON.stringify(workSpace, null, 2));
}

export function updateWorkspace(
host: Tree,
key: keyof WorkspaceSchema,
value: any
) {
const workspace = getWorkspace(host);
workspace[key] = value;
setWorkspace(host, workspace);
}
11 changes: 11 additions & 0 deletions modules/store/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,15 @@ describe('Store ng-add Schematic', () => {
);
expect(content).toMatch(/export interface AppState {/);
});

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

const tree = schematicRunner.runSchematic('ng-add', options, appTree);
const workspace = JSON.parse(tree.readContent('/angular.json'));
expect(workspace.cli.defaultCollection).toEqual('@ngrx/schematics');
});
});
14 changes: 14 additions & 0 deletions modules/store/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
} from '@ngrx/store/schematics-core';
import { Path, dirname } from '@angular-devkit/core';
import * as ts from 'typescript';
import { updateWorkspace } from '../../schematics-core/utility/config';
import { Schema as RootStoreOptions } from './schema';

function addImportToNgModule(options: RootStoreOptions): Rule {
Expand Down Expand Up @@ -94,6 +95,16 @@ function addNgRxStoreToPackageJson() {
};
}

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

export default function(options: RootStoreOptions): Rule {
return (host: Tree, context: SchematicContext) => {
options.path = getProjectPath(host, options);
Expand Down Expand Up @@ -134,6 +145,9 @@ export default function(options: RootStoreOptions): Rule {
chain([addImportToNgModule(options), mergeWith(templateSource)])
),
options && options.skipPackageJson ? noop() : addNgRxStoreToPackageJson(),
options && options.setSchematicsDefault
? setAsDefaultSchematics()
: noop(),
])(host, context);
};
}
9 changes: 7 additions & 2 deletions modules/store/schematics/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"skipPackageJson": {
"type": "boolean",
"default": false,
"description":
"Do not add @ngrx/store as dependency to package.json (e.g., --skipPackageJson)."
"description": "Do not add @ngrx/store as dependency to package.json (e.g., --skipPackageJson)."
},
"path": {
"type": "string",
Expand Down Expand Up @@ -37,6 +36,12 @@
"default": "State",
"description": "Specifies the interface for the state.",
"alias": "si"
},
"setSchematicsDefault": {
"type": "boolean",
"default": true,
"description": "Make @ngrx/schematics as the default schematics",
"x-prompt": "Would you like to make @ngrx/schematics as the project's default?"
}
},
"required": []
Expand Down
1 change: 1 addition & 0 deletions modules/store/schematics/ng-add/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface Schema {
module?: string;
statePath?: string;
stateInterface?: string;
setSchematicsDefault?: boolean;
}

0 comments on commit 6929ff4

Please sign in to comment.