forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 0
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 initial schematics utils (angular#9451)
* feat(schematics): add initial schematics utils * chore: nit * chore: pr feedback * chore: pr feedback * chore: add code owner
- Loading branch information
Showing
18 changed files
with
1,704 additions
and
0 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 |
---|---|---|
|
@@ -38,3 +38,7 @@ npm-debug.log | |
testem.log | ||
/.chrome | ||
/.git | ||
|
||
# schematics | ||
/schematics/**/*.js | ||
/schematics/**/*.map |
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 @@ | ||
// This is the root config file where the schematics are defined. | ||
{ | ||
"$schema": "./node_modules/@angular-devkit/schematics/collection-schema.json", | ||
"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,39 @@ | ||
{ | ||
"name": "@angular/material-schematics", | ||
"version": "1.0.23", | ||
"description": "Material Schematics", | ||
"scripts": { | ||
"build": "tsc -p tsconfig.json", | ||
"test": "npm run build && jasmine **/*_spec.js" | ||
}, | ||
"keywords": [ | ||
"schematics", | ||
"angular", | ||
"material", | ||
"angular-cli" | ||
], | ||
"files": [ | ||
"**/*.json", | ||
"**/*.d.ts", | ||
"**/*.ts", | ||
"**/*.__styleext__", | ||
"**/*.js", | ||
"**/*.html", | ||
"**/*.map" | ||
], | ||
"author": "", | ||
"license": "MIT", | ||
"schematics": "./collection.json", | ||
"dependencies": { | ||
"@angular-devkit/core": "^0.0.22", | ||
"@angular-devkit/schematics": "^0.0.42", | ||
"@schematics/angular": "^0.1.11", | ||
"parse5": "^3.0.3", | ||
"typescript": "^2.5.2" | ||
}, | ||
"devDependencies": { | ||
"@types/jasmine": "^2.6.0", | ||
"@types/node": "^8.0.31", | ||
"jasmine": "^2.8.0" | ||
} | ||
} |
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,27 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": "tsconfig", | ||
"lib": [ | ||
"es2017", | ||
"dom" | ||
], | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"noEmitOnError": false, | ||
"rootDir": "src/", | ||
"skipDefaultLibCheck": true, | ||
"skipLibCheck": true, | ||
"sourceMap": true, | ||
"target": "es6", | ||
"types": [ | ||
"jasmine", | ||
"node" | ||
] | ||
}, | ||
"include": [ | ||
"src/**/*" | ||
], | ||
"exclude": [ | ||
"src/*/files/**/*" | ||
] | ||
} |
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 {SchematicsException} from '@angular-devkit/schematics'; | ||
import {Tree} from '@angular-devkit/schematics'; | ||
import * as ts from 'typescript'; | ||
import {addImportToModule} from './devkit-utils/ast-utils'; | ||
import {getAppModulePath} from './devkit-utils/ng-ast-utils'; | ||
import {InsertChange} from './devkit-utils/change'; | ||
import {getConfig, getAppFromConfig} from './devkit-utils/config'; | ||
import {normalize} from '@angular-devkit/core'; | ||
|
||
/** | ||
* Reads file given path and returns TypeScript source file. | ||
*/ | ||
export function getSourceFile(host: Tree, path: string): ts.SourceFile { | ||
const buffer = host.read(path); | ||
if (!buffer) { | ||
throw new SchematicsException(`Could not find file for path: ${path}`); | ||
} | ||
const content = buffer.toString(); | ||
const source = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true); | ||
return source; | ||
} | ||
|
||
/** | ||
* Import and add module to root app module. | ||
*/ | ||
export function addModuleImportToRootModule(host: Tree, moduleName: string, src: string) { | ||
const config = getConfig(host); | ||
const app = getAppFromConfig(config, '0'); | ||
const modulePath = getAppModulePath(host, app); | ||
addModuleImportToModule(host, modulePath, moduleName, src); | ||
} | ||
|
||
/** | ||
* Import and add module to specific module path. | ||
* @param host the tree we are updating | ||
* @param modulePath src location of the module to import | ||
* @param moduleName name of module to import | ||
* @param src src location to import | ||
*/ | ||
export function addModuleImportToModule( | ||
host: Tree, modulePath: string, moduleName: string, src: string) { | ||
const moduleSource = getSourceFile(host, modulePath); | ||
const changes = addImportToModule(moduleSource, modulePath, moduleName, src); | ||
const recorder = host.beginUpdate(modulePath); | ||
|
||
changes.forEach((change) => { | ||
if (change instanceof InsertChange) { | ||
recorder.insertLeft(change.pos, change.toAdd); | ||
} | ||
}); | ||
|
||
host.commitUpdate(recorder); | ||
} | ||
|
||
/** | ||
* Gets the app index.html file | ||
*/ | ||
export function getIndexHtmlPath(host: Tree) { | ||
const config = getConfig(host); | ||
const app = getAppFromConfig(config, '0'); | ||
return normalize(`/${app.root}/${app.index}`); | ||
} |
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,2 @@ | ||
# NOTE | ||
This code is directly taken from [angular schematics package](https://github.com/angular/devkit/tree/master/packages/schematics/angular/utility). |
Oops, something went wrong.