Skip to content
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): add initial schematics utils #9451

Merged
merged 5 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,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 @@ -35,3 +35,7 @@ npm-debug.log
testem.log
/.chrome
/.git

# schematics
/schematics/**/*.js
/schematics/**/*.map
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to put this under src?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you like to move all the code under src?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, just curious; we can move them if we ever need to

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add @param JsDoc? This will help disambiguate which module mouldePath and moduleName are referring to

* @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