Skip to content

Commit

Permalink
feat(schematics): add initial schematics utils (angular#9451)
Browse files Browse the repository at this point in the history
* feat(schematics): add initial schematics utils

* chore: nit

* chore: pr feedback

* chore: pr feedback

* chore: add code owner
  • Loading branch information
amcdnl authored and mmalerba committed Feb 6, 2018
1 parent 0b0f401 commit 673d56e
Show file tree
Hide file tree
Showing 18 changed files with 1,704 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@
/src/e2e-app/slide-toggle/** @devversion
/src/e2e-app/tabs/** @andrewseguin

# Schematics
/schematics/** @amcdnl

# Universal app
/src/universal-app/** @jelbourn

Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ npm-debug.log
testem.log
/.chrome
/.git

# schematics
/schematics/**/*.js
/schematics/**/*.map
5 changes: 5 additions & 0 deletions schematics/collection.json
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": {}
}
39 changes: 39 additions & 0 deletions schematics/package.json
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"
}
}
27 changes: 27 additions & 0 deletions schematics/tsconfig.json
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/**/*"
]
}
62 changes: 62 additions & 0 deletions schematics/utils/ast.ts
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}`);
}
2 changes: 2 additions & 0 deletions schematics/utils/devkit-utils/README.md
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).
Loading

0 comments on commit 673d56e

Please sign in to comment.