-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ng-add schematics implementation (#16)
Closes #9
- Loading branch information
1 parent
b0e67ba
commit 54e728b
Showing
15 changed files
with
1,405 additions
and
19 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
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,10 @@ | ||
{ | ||
"schematics": { | ||
"ng-add": { | ||
"aliases": ["init"], | ||
"factory": "./ng-add", | ||
"schema": "./ng-add/schema.json", | ||
"description": "Add angular-routing to 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,62 @@ | ||
import { | ||
SchematicTestRunner, | ||
UnitTestTree, | ||
} from '@angular-devkit/schematics/testing'; | ||
import * as path from 'path'; | ||
import { RouterOptions } from './schema'; | ||
|
||
export const workspaceOptions = { | ||
name: 'workspace', | ||
newProjectRoot: 'projects', | ||
version: '6.0.0', | ||
defaultProject: 'bar', | ||
}; | ||
|
||
export const defaultAppOptions = { | ||
name: 'bar', | ||
inlineStyle: false, | ||
inlineTemplate: false, | ||
viewEncapsulation: 'Emulated', | ||
routing: false, | ||
style: 'css', | ||
skipTests: false, | ||
}; | ||
|
||
|
||
const collectionPath = path.join(__dirname, '../collection.json'); | ||
|
||
describe('ng add function', () => { | ||
let appTree: UnitTestTree; | ||
let schematicRunner: SchematicTestRunner; | ||
|
||
const defaultOptions: RouterOptions = { | ||
project: 'bar', | ||
module: 'app' | ||
}; | ||
|
||
beforeEach(async () => { | ||
|
||
schematicRunner = new SchematicTestRunner('angular-routing', collectionPath); | ||
|
||
appTree = await schematicRunner.runExternalSchematicAsync( | ||
'@schematics/angular', | ||
'workspace', | ||
workspaceOptions | ||
).toPromise(); | ||
|
||
appTree = await schematicRunner.runExternalSchematicAsync( | ||
'@schematics/angular', | ||
'application', | ||
defaultAppOptions, | ||
appTree | ||
).toPromise(); | ||
}); | ||
|
||
it('should import RouterModule a specified module', () => { | ||
const options = { ...defaultOptions }; | ||
|
||
const tree = schematicRunner.runSchematic('ng-add', options, appTree); | ||
const content = tree.readContent(`/projects/bar/src/app/app.module.ts`); | ||
expect(content).toContain('RoutingModule.forRoot()'); | ||
}); | ||
}); |
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,67 @@ | ||
import { | ||
chain, Rule, | ||
Tree, SchematicsException | ||
} from '@angular-devkit/schematics'; | ||
import * as ts from 'typescript'; | ||
import { addImportToModule, insertImport } from '../utils/ast-utils'; | ||
import { commitChanges } from '../utils/change'; | ||
import { RouterOptions } from './schema'; | ||
import { findModuleFromOptions } from '../utils/find-module'; | ||
import { getProjectPath } from '../utils/project'; | ||
|
||
function addImportToNgModule(options: RouterOptions): Rule { | ||
return (host: Tree) => { | ||
options.path = getProjectPath(host, options); | ||
const modulePath = findModuleFromOptions(host, options); | ||
|
||
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 importChanges = addImportToModule( | ||
source, | ||
modulePath, | ||
'RoutingModule.forRoot()', | ||
'angular-routing', | ||
).shift(); | ||
|
||
|
||
let changes = [ | ||
insertImport(source, modulePath, 'RoutingModule', 'angular-routing'), | ||
importChanges | ||
]; | ||
|
||
commitChanges(host, source.fileName, changes); | ||
|
||
return host; | ||
}; | ||
} | ||
|
||
// Just return the tree | ||
export default function (options: RouterOptions): Rule { | ||
|
||
return chain([ | ||
addImportToNgModule(options) | ||
]); | ||
} | ||
|
||
|
||
|
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,28 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"id": "SchematicsAddRouting", | ||
"title": "Angular Routing ng-add Options Schema", | ||
"type": "object", | ||
"properties": { | ||
"project": { | ||
"type": "string", | ||
"description": "The name of the project.", | ||
"aliases": [ | ||
"p" | ||
] | ||
}, | ||
"module": { | ||
"type": "string", | ||
"default": "app", | ||
"description": "Allows specification of the declaring module.", | ||
"alias": "m", | ||
"subtype": "filepath" | ||
}, | ||
"path": { | ||
"type": "string", | ||
"format": "path", | ||
"description": "The path to create the state.", | ||
"visible": false | ||
} | ||
} | ||
} |
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,5 @@ | ||
export interface RouterOptions { | ||
project: string; | ||
module?: string; | ||
path?: string; | ||
} |
Oops, something went wrong.