-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(schematics): Add ng-add support with prompt for making our schem…
…atics default (#1552)
- Loading branch information
1 parent
9d3597a
commit 01ff157
Showing
5 changed files
with
118 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface Schema { | ||
defaultCollection?: boolean; | ||
} |