Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit 18fe75d

Browse files
jschwartyBrocco
authored andcommitted
feat(@schematics/angular): Add pipe schematic
1 parent 8627da4 commit 18fe75d

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-0
lines changed

packages/schematics/angular/collection.json

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
"description": "Create an Angular module.",
3131
"schema": "./module/schema.json"
3232
},
33+
"pipe": {
34+
"factory": "./pipe",
35+
"description": "Create an Angular pipe.",
36+
"schema": "./pipe/schema.json"
37+
},
3338
"service": {
3439
"factory": "./service",
3540
"description": "Create an Angular service.",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { <%= classify(name) %>Pipe } from './<%= dasherize(name) %>.pipe';
2+
3+
describe('<%= classify(name) %>Pipe', () => {
4+
it('create an instance', () => {
5+
const pipe = new <%= classify(name) %>Pipe();
6+
expect(pipe).toBeTruthy();
7+
});
8+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
3+
@Pipe({
4+
name: '<%= camelize(name) %>'
5+
})
6+
export class <%= classify(name) %>Pipe implements PipeTransform {
7+
8+
transform(value: any, args?: any): any {
9+
return null;
10+
}
11+
12+
}
+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
// TODO: replace `options: any` with an actual type generated from the schema.
9+
// tslint:disable:no-any
10+
import {addDeclarationToModule} from '../utility/ast-utils';
11+
import {InsertChange} from '../utility/change';
12+
13+
import {
14+
Rule,
15+
Tree,
16+
apply,
17+
branchAndMerge,
18+
chain,
19+
filter,
20+
mergeWith,
21+
move,
22+
noop,
23+
template,
24+
url,
25+
} from '@angular-devkit/schematics';
26+
import * as stringUtils from '../strings';
27+
28+
import 'rxjs/add/operator/merge';
29+
import * as ts from 'typescript';
30+
import {buildRelativePath, findModule} from '../utility/find-module';
31+
32+
33+
function addDeclarationToNgModule(options: any): Rule {
34+
return (host: Tree) => {
35+
if (options.skipImport) {
36+
return host;
37+
}
38+
39+
let modulePath;
40+
if (options.module) {
41+
if (!host.exists(options.module)) {
42+
throw new Error(`Module specified (${options.module}) does not exist.`);
43+
}
44+
modulePath = options.module;
45+
} else {
46+
modulePath = findModule(host, options.sourceDir + '/' + options.path);
47+
}
48+
49+
const sourceText = host.read(modulePath) !.toString('utf-8');
50+
const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
51+
52+
const pipePath = `/${options.sourceDir}/${options.path}/`
53+
+ (options.flat ? '' : stringUtils.dasherize(options.name) + '/')
54+
+ stringUtils.dasherize(options.name)
55+
+ '.pipe';
56+
const relativePath = buildRelativePath(modulePath, pipePath);
57+
const changes = addDeclarationToModule(source, modulePath,
58+
stringUtils.classify(`${options.name}Pipe`),
59+
relativePath);
60+
const recorder = host.beginUpdate(modulePath);
61+
for (const change of changes) {
62+
if (change instanceof InsertChange) {
63+
recorder.insertLeft(change.pos, change.toAdd);
64+
}
65+
}
66+
host.commitUpdate(recorder);
67+
68+
return host;
69+
};
70+
}
71+
72+
export default function (options: any): Rule {
73+
74+
const templateSource = apply(url('./files'), [
75+
options.spec ? noop() : filter(path => !path.endsWith('.spec.ts')),
76+
template({
77+
...stringUtils,
78+
'if-flat': (s: string) => options.flat ? '' : s,
79+
...options,
80+
}),
81+
move(options.sourceDir),
82+
]);
83+
84+
return chain([
85+
branchAndMerge(chain([
86+
filter(path => path.endsWith('.module.ts') && !path.endsWith('-routing.module.ts')),
87+
addDeclarationToNgModule(options),
88+
mergeWith(templateSource),
89+
])),
90+
]);
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"$schema": "http://json-schema.org/schema",
3+
"id": "SchematicsAngularPipe",
4+
"title": "Angular Pipe Options Schema",
5+
"type": "object",
6+
"properties": {
7+
"name": {
8+
"type": "string"
9+
},
10+
"path": {
11+
"type": "string",
12+
"default": "app"
13+
},
14+
"sourceDir": {
15+
"type": "string",
16+
"default": "src"
17+
},
18+
"flat": {
19+
"type": "boolean",
20+
"default": false,
21+
"description": "Flag to indicate if a dir is created."
22+
},
23+
"spec": {
24+
"type": "boolean",
25+
"default": true,
26+
"description": "Specifies if a spec file is generated."
27+
},
28+
"skipImport": {
29+
"type": "boolean",
30+
"default": false,
31+
"description": "Allows for skipping the module import."
32+
},
33+
"module": {
34+
"type": "string",
35+
"default": "",
36+
"description": "Allows specification of the declaring module."
37+
},
38+
"export": {
39+
"type": "boolean",
40+
"default": false,
41+
"description": "Specifies if declaring module exports the pipe."
42+
}
43+
},
44+
"required": [
45+
"name"
46+
]
47+
}

0 commit comments

Comments
 (0)