-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(schematics): implement router store ng-add #1487
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,35 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
load("//tools:defaults.bzl", "npm_package", "ts_library") | ||
|
||
ts_library( | ||
name = "schematics", | ||
srcs = glob( | ||
[ | ||
"**/*.ts", | ||
], | ||
exclude = [ | ||
"**/*.spec.ts", | ||
"**/files/**/*", | ||
], | ||
), | ||
module_name = "@ngrx/router-store/schematics", | ||
deps = [ | ||
"//modules/router-store/schematics-core", | ||
"@npm//@angular-devkit/schematics", | ||
"@npm//typescript", | ||
], | ||
) | ||
|
||
npm_package( | ||
name = "npm_package", | ||
srcs = [ | ||
":collection.json", | ||
] + glob([ | ||
"**/files/**/*", | ||
"**/schema.json", | ||
]), | ||
deps = [ | ||
":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,10 @@ | ||
{ | ||
"schematics": { | ||
"ng-add": { | ||
"aliases": ["init"], | ||
"factory": "./ng-add", | ||
"schema": "./ng-add/schema.json", | ||
"description": "Register @ngrx/router-store within your application" | ||
} | ||
} | ||
} |
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,79 @@ | ||
import { | ||
SchematicTestRunner, | ||
UnitTestTree, | ||
} from '@angular-devkit/schematics/testing'; | ||
import * as path from 'path'; | ||
import { Schema as RouterStoreOptions } from './schema'; | ||
import { | ||
getTestProjectPath, | ||
createWorkspace, | ||
} from '../../../schematics-core/testing'; | ||
|
||
describe('Router Store ng-add Schematic', () => { | ||
const schematicRunner = new SchematicTestRunner( | ||
'@ngrx/router-store', | ||
path.join(__dirname, '../collection.json') | ||
); | ||
const defaultOptions: RouterStoreOptions = { | ||
skipPackageJson: false, | ||
module: 'app', | ||
}; | ||
|
||
const projectPath = getTestProjectPath(); | ||
|
||
let appTree: UnitTestTree; | ||
|
||
beforeEach(() => { | ||
appTree = createWorkspace(schematicRunner, appTree); | ||
}); | ||
|
||
it('should update package.json', () => { | ||
const options = { ...defaultOptions }; | ||
|
||
const tree = schematicRunner.runSchematic('ng-add', options, appTree); | ||
const packageJson = JSON.parse(tree.readContent('/package.json')); | ||
|
||
expect(packageJson.dependencies['@ngrx/router-store']).toBeDefined(); | ||
}); | ||
|
||
it('should skip package.json update', () => { | ||
const options = { ...defaultOptions, skipPackageJson: true }; | ||
|
||
const tree = schematicRunner.runSchematic('ng-add', options, appTree); | ||
const packageJson = JSON.parse(tree.readContent('/package.json')); | ||
|
||
expect(packageJson.dependencies['@ngrx/router-store']).toBeUndefined(); | ||
}); | ||
|
||
it('should be provided by default', () => { | ||
const options = { ...defaultOptions }; | ||
|
||
const tree = schematicRunner.runSchematic('ng-add', options, appTree); | ||
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`); | ||
expect(content).toMatch( | ||
/import { StoreRouterConnectingModule } from '@ngrx\/router-store';/ | ||
); | ||
expect(content).toMatch(/StoreRouterConnectingModule.forRoot\(\)/); | ||
}); | ||
|
||
it('should import into a specified module', () => { | ||
const options = { ...defaultOptions }; | ||
|
||
const tree = schematicRunner.runSchematic('ng-add', options, appTree); | ||
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`); | ||
expect(content).toMatch( | ||
/import { StoreRouterConnectingModule } from '@ngrx\/router-store';/ | ||
); | ||
}); | ||
|
||
it('should fail if specified module does not exist', () => { | ||
const options = { ...defaultOptions, module: '/src/app/app.moduleXXX.ts' }; | ||
let thrownError: Error | null = null; | ||
try { | ||
schematicRunner.runSchematic('ng-add', options, appTree); | ||
} catch (err) { | ||
thrownError = err; | ||
} | ||
expect(thrownError).toBeDefined(); | ||
}); | ||
}); |
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,115 @@ | ||
import { | ||
Rule, | ||
SchematicContext, | ||
SchematicsException, | ||
Tree, | ||
branchAndMerge, | ||
chain, | ||
noop, | ||
} from '@angular-devkit/schematics'; | ||
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks'; | ||
import * as ts from 'typescript'; | ||
import { | ||
InsertChange, | ||
addImportToModule, | ||
addPackageToPackageJson, | ||
buildRelativePath, | ||
findModuleFromOptions, | ||
getProjectPath, | ||
insertImport, | ||
parseName, | ||
platformVersion, | ||
stringUtils, | ||
} from '@ngrx/router-store/schematics-core'; | ||
import { Schema as RouterStoreOptions } from './schema'; | ||
|
||
function addImportToNgModule(options: RouterStoreOptions): Rule { | ||
return (host: Tree) => { | ||
const modulePath = options.module; | ||
|
||
if (!modulePath) { | ||
return host; | ||
} | ||
|
||
if (!host.exists(modulePath)) { | ||
throw new Error('Specified module does not exist'); | ||
} | ||
|
||
const text = host.read(modulePath); | ||
if (text === null) { | ||
throw new SchematicsException(`File ${modulePath} does not exist.`); | ||
} | ||
const sourceText = text.toString('utf-8'); | ||
|
||
const source = ts.createSourceFile( | ||
modulePath, | ||
sourceText, | ||
ts.ScriptTarget.Latest, | ||
true | ||
); | ||
|
||
const [routerStoreNgModuleImport] = addImportToModule( | ||
source, | ||
modulePath, | ||
`StoreRouterConnectingModule.forRoot()`, | ||
`@ngrx/router-store` | ||
); | ||
|
||
const changes = [ | ||
insertImport( | ||
source, | ||
modulePath, | ||
'StoreRouterConnectingModule', | ||
'@ngrx/router-store' | ||
), | ||
routerStoreNgModuleImport, | ||
]; | ||
const recorder = host.beginUpdate(modulePath); | ||
|
||
for (const change of changes) { | ||
if (change instanceof InsertChange) { | ||
recorder.insertLeft(change.pos, change.toAdd); | ||
} | ||
} | ||
host.commitUpdate(recorder); | ||
|
||
return host; | ||
}; | ||
} | ||
|
||
function addNgRxRouterStoreToPackageJson() { | ||
return (host: Tree, context: SchematicContext) => { | ||
addPackageToPackageJson( | ||
host, | ||
'dependencies', | ||
'@ngrx/router-store', | ||
platformVersion | ||
); | ||
context.addTask(new NodePackageInstallTask()); | ||
return host; | ||
}; | ||
} | ||
|
||
export default function(options: RouterStoreOptions): Rule { | ||
return (host: Tree, context: SchematicContext) => { | ||
options.path = getProjectPath(host, options); | ||
|
||
if (options.module) { | ||
options.module = findModuleFromOptions(host, { | ||
name: '', | ||
module: options.module, | ||
path: options.path, | ||
}); | ||
} | ||
|
||
const parsedPath = parseName(options.path, ''); | ||
options.path = parsedPath.path; | ||
|
||
return chain([ | ||
branchAndMerge(chain([addImportToNgModule(options)])), | ||
options && options.skipPackageJson | ||
? noop() | ||
: addNgRxRouterStoreToPackageJson(), | ||
])(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,33 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"id": "SchematicsNgRxRouterStore", | ||
"title": "NgRx Router Store Schema", | ||
"type": "object", | ||
"properties": { | ||
"skipPackageJson": { | ||
"type": "boolean", | ||
"default": false, | ||
"description": "Do not add @ngrx/router-store as dependency to package.json (e.g., --skipPackageJson)." | ||
}, | ||
"path": { | ||
"type": "string", | ||
"format": "path", | ||
"description": "The path to create the router store.", | ||
"visible": false | ||
}, | ||
"project": { | ||
"type": "string", | ||
"description": "The name of the project.", | ||
"visible": false, | ||
"aliases": ["p"] | ||
}, | ||
"module": { | ||
"type": "string", | ||
"default": "app", | ||
"description": "Allows specification of the declaring module.", | ||
"alias": "m", | ||
"subtype": "filepath" | ||
} | ||
}, | ||
"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,6 @@ | ||
export interface Schema { | ||
skipPackageJson?: boolean; | ||
path?: string; | ||
project?: string; | ||
module?: string; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
project
key is missing here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
project
keys are also missing in /modules/MODULE/schematics/ng-add/schema.json as they are merged with #1067, at least for effects, store or store-devtools. But for example in /modules/schematics/src/MODULE/schema.json they are included.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. That can be done in a separate PR.