forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
150 lines (134 loc) · 4.5 KB
/
index.ts
File metadata and controls
150 lines (134 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import * as chalk from 'chalk';
import * as path from 'path';
import { NodeHost } from '../../lib/ast-tools';
import { CliConfig } from '../../models/config';
import { dynamicPathParser } from '../../utilities/dynamic-path-parser';
import { getAppFromConfig } from '../../utilities/app-utils';
import { resolveModulePath } from '../../utilities/resolve-module-file';
const stringUtils = require('ember-cli-string-utils');
const astUtils = require('../../utilities/ast-utils');
const findParentModule = require('../../utilities/find-parent-module').default;
const Blueprint = require('../../ember-cli/lib/models/blueprint');
const getFiles = Blueprint.prototype.files;
export default Blueprint.extend({
description: '',
aliases: ['p'],
availableOptions: [
{
name: 'flat',
type: Boolean,
description: 'Flag to indicate if a dir is created.'
},
{
name: 'spec',
type: Boolean,
description: 'Specifies if a spec file is generated.'
},
{
name: 'skip-import',
type: Boolean,
default: false,
description: 'Allows for skipping the module import.'
},
{
name: 'module',
type: String,
aliases: ['m'],
description: 'Allows specification of the declaring module.'
},
{
name: 'export',
type: Boolean,
default: false,
description: 'Specifies if declaring module exports the pipe.'
},
{
name: 'app',
type: String,
aliases: ['a'],
description: 'Specifies app name to use.'
}
],
beforeInstall: function(options: any) {
const appConfig = getAppFromConfig(this.options.app);
if (options.module) {
this.pathToModule =
resolveModulePath(options.module, this.project, this.project.root, appConfig);
} else {
try {
this.pathToModule = findParentModule(this.project.root, appConfig.root, this.generatePath);
} catch (e) {
if (!options.skipImport) {
throw `Error locating module for declaration\n\t${e}`;
}
}
}
},
normalizeEntityName: function (entityName: string) {
const appConfig = getAppFromConfig(this.options.app);
const parsedPath = dynamicPathParser(this.project, entityName, appConfig);
this.dynamicPath = parsedPath;
return parsedPath.name;
},
locals: function (options: any) {
options.flat = options.flat !== undefined ?
options.flat : CliConfig.getValue('defaults.pipe.flat');
options.spec = options.spec !== undefined ?
options.spec : CliConfig.getValue('defaults.pipe.spec');
return {
dynamicPath: this.dynamicPath.dir,
flat: options.flat
};
},
files: function() {
let fileList = getFiles.call(this) as Array<string>;
if (this.options && !this.options.spec) {
fileList = fileList.filter(p => p.indexOf('__name__.pipe.spec.ts') < 0);
}
return fileList;
},
fileMapTokens: function (options: any) {
// Return custom template variables here.
return {
__path__: () => {
let dir = this.dynamicPath.dir;
if (!options.locals.flat) {
dir += path.sep + options.dasherizedModuleName;
}
this.generatePath = dir;
return dir;
}
};
},
afterInstall: function(options: any) {
const returns: Array<any> = [];
const className = stringUtils.classify(`${options.entity.name}Pipe`);
const fileName = stringUtils.dasherize(`${options.entity.name}.pipe`);
const fullGeneratePath = path.join(this.project.root, this.generatePath);
const moduleDir = path.parse(this.pathToModule).dir;
const relativeDir = path.relative(moduleDir, fullGeneratePath);
const importPath = relativeDir ? `./${relativeDir}/${fileName}` : `./${fileName}`;
if (!options.skipImport) {
if (options.dryRun) {
this._writeStatusToUI(chalk.yellow,
'update',
path.relative(this.project.root, this.pathToModule));
return;
}
returns.push(
astUtils.addDeclarationToModule(this.pathToModule, className, importPath)
.then((change: any) => change.apply(NodeHost))
.then((result: any) => {
if (options.export) {
return astUtils.addExportToModule(this.pathToModule, className, importPath)
.then((change: any) => change.apply(NodeHost));
}
return result;
}));
this._writeStatusToUI(chalk.yellow,
'update',
path.relative(this.project.root, this.pathToModule));
}
return Promise.all(returns);
}
});