Skip to content

Commit 673d56e

Browse files
amcdnlmmalerba
authored andcommitted
feat(schematics): add initial schematics utils (#9451)
* feat(schematics): add initial schematics utils * chore: nit * chore: pr feedback * chore: pr feedback * chore: add code owner
1 parent 0b0f401 commit 673d56e

18 files changed

+1704
-0
lines changed

.github/CODEOWNERS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@
172172
/src/e2e-app/slide-toggle/** @devversion
173173
/src/e2e-app/tabs/** @andrewseguin
174174

175+
# Schematics
176+
/schematics/** @amcdnl
177+
175178
# Universal app
176179
/src/universal-app/** @jelbourn
177180

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@ npm-debug.log
3838
testem.log
3939
/.chrome
4040
/.git
41+
42+
# schematics
43+
/schematics/**/*.js
44+
/schematics/**/*.map

schematics/collection.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// This is the root config file where the schematics are defined.
2+
{
3+
"$schema": "./node_modules/@angular-devkit/schematics/collection-schema.json",
4+
"schematics": {}
5+
}

schematics/package.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "@angular/material-schematics",
3+
"version": "1.0.23",
4+
"description": "Material Schematics",
5+
"scripts": {
6+
"build": "tsc -p tsconfig.json",
7+
"test": "npm run build && jasmine **/*_spec.js"
8+
},
9+
"keywords": [
10+
"schematics",
11+
"angular",
12+
"material",
13+
"angular-cli"
14+
],
15+
"files": [
16+
"**/*.json",
17+
"**/*.d.ts",
18+
"**/*.ts",
19+
"**/*.__styleext__",
20+
"**/*.js",
21+
"**/*.html",
22+
"**/*.map"
23+
],
24+
"author": "",
25+
"license": "MIT",
26+
"schematics": "./collection.json",
27+
"dependencies": {
28+
"@angular-devkit/core": "^0.0.22",
29+
"@angular-devkit/schematics": "^0.0.42",
30+
"@schematics/angular": "^0.1.11",
31+
"parse5": "^3.0.3",
32+
"typescript": "^2.5.2"
33+
},
34+
"devDependencies": {
35+
"@types/jasmine": "^2.6.0",
36+
"@types/node": "^8.0.31",
37+
"jasmine": "^2.8.0"
38+
}
39+
}

schematics/tsconfig.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": "tsconfig",
4+
"lib": [
5+
"es2017",
6+
"dom"
7+
],
8+
"module": "commonjs",
9+
"moduleResolution": "node",
10+
"noEmitOnError": false,
11+
"rootDir": "src/",
12+
"skipDefaultLibCheck": true,
13+
"skipLibCheck": true,
14+
"sourceMap": true,
15+
"target": "es6",
16+
"types": [
17+
"jasmine",
18+
"node"
19+
]
20+
},
21+
"include": [
22+
"src/**/*"
23+
],
24+
"exclude": [
25+
"src/*/files/**/*"
26+
]
27+
}

schematics/utils/ast.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {SchematicsException} from '@angular-devkit/schematics';
2+
import {Tree} from '@angular-devkit/schematics';
3+
import * as ts from 'typescript';
4+
import {addImportToModule} from './devkit-utils/ast-utils';
5+
import {getAppModulePath} from './devkit-utils/ng-ast-utils';
6+
import {InsertChange} from './devkit-utils/change';
7+
import {getConfig, getAppFromConfig} from './devkit-utils/config';
8+
import {normalize} from '@angular-devkit/core';
9+
10+
/**
11+
* Reads file given path and returns TypeScript source file.
12+
*/
13+
export function getSourceFile(host: Tree, path: string): ts.SourceFile {
14+
const buffer = host.read(path);
15+
if (!buffer) {
16+
throw new SchematicsException(`Could not find file for path: ${path}`);
17+
}
18+
const content = buffer.toString();
19+
const source = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true);
20+
return source;
21+
}
22+
23+
/**
24+
* Import and add module to root app module.
25+
*/
26+
export function addModuleImportToRootModule(host: Tree, moduleName: string, src: string) {
27+
const config = getConfig(host);
28+
const app = getAppFromConfig(config, '0');
29+
const modulePath = getAppModulePath(host, app);
30+
addModuleImportToModule(host, modulePath, moduleName, src);
31+
}
32+
33+
/**
34+
* Import and add module to specific module path.
35+
* @param host the tree we are updating
36+
* @param modulePath src location of the module to import
37+
* @param moduleName name of module to import
38+
* @param src src location to import
39+
*/
40+
export function addModuleImportToModule(
41+
host: Tree, modulePath: string, moduleName: string, src: string) {
42+
const moduleSource = getSourceFile(host, modulePath);
43+
const changes = addImportToModule(moduleSource, modulePath, moduleName, src);
44+
const recorder = host.beginUpdate(modulePath);
45+
46+
changes.forEach((change) => {
47+
if (change instanceof InsertChange) {
48+
recorder.insertLeft(change.pos, change.toAdd);
49+
}
50+
});
51+
52+
host.commitUpdate(recorder);
53+
}
54+
55+
/**
56+
* Gets the app index.html file
57+
*/
58+
export function getIndexHtmlPath(host: Tree) {
59+
const config = getConfig(host);
60+
const app = getAppFromConfig(config, '0');
61+
return normalize(`/${app.root}/${app.index}`);
62+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# NOTE
2+
This code is directly taken from [angular schematics package](https://github.com/angular/devkit/tree/master/packages/schematics/angular/utility).

0 commit comments

Comments
 (0)