Skip to content

Commit a39ac0e

Browse files
clydinMRHarrison
authored andcommitted
refactor(angular-cli): convert blueprints to typescript (angular#4306)
1 parent 9c428dc commit a39ac0e

File tree

10 files changed

+135
-116
lines changed

10 files changed

+135
-116
lines changed

packages/angular-cli/blueprints/class/index.js renamed to packages/angular-cli/blueprints/class/index.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ const dynamicPathParser = require('../../utilities/dynamic-path-parser');
33
const Blueprint = require('../../ember-cli/lib/models/blueprint');
44
const getFiles = Blueprint.prototype.files;
55

6-
module.exports = {
6+
export default Blueprint.extend({
77
description: '',
88

99
availableOptions: [
1010
{ name: 'spec', type: Boolean }
1111
],
1212

13-
normalizeEntityName: function (entityName) {
14-
var parsedPath = dynamicPathParser(this.project, entityName.split('.')[0]);
13+
normalizeEntityName: function (entityName: string) {
14+
const parsedPath = dynamicPathParser(this.project, entityName.split('.')[0]);
1515

1616
this.dynamicPath = parsedPath;
1717
return parsedPath.name;
1818
},
1919

20-
locals: function (options) {
21-
const rawName = options.args[1];
20+
locals: function (options: any) {
21+
const rawName = options.args[1] as string;
2222
const nameParts = rawName.split('.')
2323
.filter(part => part.length !== 0);
2424

@@ -40,7 +40,7 @@ module.exports = {
4040
},
4141

4242
files: function() {
43-
var fileList = getFiles.call(this);
43+
let fileList = getFiles.call(this) as Array<string>;
4444

4545
if (this.options && !this.options.spec) {
4646
fileList = fileList.filter(p => p.indexOf('__name__.spec.ts') < 0);
@@ -61,4 +61,4 @@ module.exports = {
6161
}
6262
};
6363
}
64-
};
64+
});

packages/angular-cli/blueprints/component/index.js renamed to packages/angular-cli/blueprints/component/index.ts

+22-19
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const stringUtils = require('ember-cli-string-utils');
99
const astUtils = require('../../utilities/ast-utils');
1010
const NodeHost = require('@angular-cli/ast-tools').NodeHost;
1111

12-
module.exports = {
12+
export default Blueprint.extend({
1313
description: '',
1414

1515
availableOptions: [
@@ -25,7 +25,7 @@ module.exports = {
2525
{ name: 'export', type: Boolean, default: false }
2626
],
2727

28-
beforeInstall: function(options) {
28+
beforeInstall: function(options: any) {
2929
if (options.module) {
3030
// Resolve path to module
3131
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
@@ -38,27 +38,28 @@ module.exports = {
3838
} else {
3939
try {
4040
this.pathToModule = findParentModule(this.project, this.dynamicPath.dir);
41-
} catch(e) {
41+
} catch (e) {
4242
if (!options.skipImport) {
4343
throw `Error locating module for declaration\n\t${e}`;
4444
}
4545
}
4646
}
4747
},
4848

49-
normalizeEntityName: function (entityName) {
50-
var parsedPath = dynamicPathParser(this.project, entityName);
49+
normalizeEntityName: function (entityName: string) {
50+
const parsedPath = dynamicPathParser(this.project, entityName);
5151

5252
this.dynamicPath = parsedPath;
5353

54-
var defaultPrefix = '';
54+
let defaultPrefix = '';
5555
if (this.project.ngConfig &&
5656
this.project.ngConfig.apps[0] &&
5757
this.project.ngConfig.apps[0].prefix) {
5858
defaultPrefix = this.project.ngConfig.apps[0].prefix;
5959
}
6060

61-
var prefix = (this.options.prefix === 'false' || this.options.prefix === '') ? '' : (this.options.prefix || defaultPrefix);
61+
let prefix = (this.options.prefix === 'false' || this.options.prefix === '')
62+
? '' : (this.options.prefix || defaultPrefix);
6263
prefix = prefix && `${prefix}-`;
6364

6465
this.selector = stringUtils.dasherize(prefix + parsedPath.name);
@@ -70,7 +71,7 @@ module.exports = {
7071
return parsedPath.name;
7172
},
7273

73-
locals: function (options) {
74+
locals: function (options: any) {
7475
this.styleExt = 'css';
7576
if (this.project.ngConfig &&
7677
this.project.ngConfig.defaults &&
@@ -114,7 +115,7 @@ module.exports = {
114115
},
115116

116117
files: function() {
117-
var fileList = getFiles.call(this);
118+
let fileList = getFiles.call(this) as Array<string>;
118119

119120
if (this.options && this.options.inlineTemplate) {
120121
fileList = fileList.filter(p => p.indexOf('.html') < 0);
@@ -129,15 +130,15 @@ module.exports = {
129130
return fileList;
130131
},
131132

132-
fileMapTokens: function (options) {
133+
fileMapTokens: function (options: any) {
133134
// Return custom template variables here.
134135
return {
135136
__path__: () => {
136-
var dir = this.dynamicPath.dir;
137+
let dir = this.dynamicPath.dir;
137138
if (!options.locals.flat) {
138139
dir += path.sep + options.dasherizedModuleName;
139140
}
140-
var srcDir = this.project.ngConfig.apps[0].root;
141+
const srcDir = this.project.ngConfig.apps[0].root;
141142
this.appDir = dir.substr(dir.indexOf(srcDir) + srcDir.length);
142143
this.generatePath = dir;
143144
return dir;
@@ -148,12 +149,12 @@ module.exports = {
148149
};
149150
},
150151

151-
afterInstall: function(options) {
152+
afterInstall: function(options: any) {
152153
if (options.dryRun) {
153154
return;
154155
}
155156

156-
const returns = [];
157+
const returns: Array<any> = [];
157158
const className = stringUtils.classify(`${options.entity.name}Component`);
158159
const fileName = stringUtils.dasherize(`${options.entity.name}.component`);
159160
const componentDir = path.relative(path.dirname(this.pathToModule), this.generatePath);
@@ -162,17 +163,19 @@ module.exports = {
162163
if (!options.skipImport) {
163164
returns.push(
164165
astUtils.addDeclarationToModule(this.pathToModule, className, importPath)
165-
.then(change => change.apply(NodeHost))
166-
.then((result) => {
166+
.then((change: any) => change.apply(NodeHost))
167+
.then((result: any) => {
167168
if (options.export) {
168169
return astUtils.addExportToModule(this.pathToModule, className, importPath)
169-
.then(change => change.apply(NodeHost));
170+
.then((change: any) => change.apply(NodeHost));
170171
}
171172
return result;
172173
}));
173-
this._writeStatusToUI(chalk.yellow, 'update', path.relative(this.project.root, this.pathToModule));
174+
this._writeStatusToUI(chalk.yellow,
175+
'update',
176+
path.relative(this.project.root, this.pathToModule));
174177
}
175178

176179
return Promise.all(returns);
177180
}
178-
};
181+
});

packages/angular-cli/blueprints/directive/index.js renamed to packages/angular-cli/blueprints/directive/index.ts

+21-18
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const NodeHost = require('@angular-cli/ast-tools').NodeHost;
99
const Blueprint = require('../../ember-cli/lib/models/blueprint');
1010
const getFiles = Blueprint.prototype.files;
1111

12-
module.exports = {
12+
export default Blueprint.extend({
1313
description: '',
1414

1515
availableOptions: [
@@ -21,7 +21,7 @@ module.exports = {
2121
{ name: 'export', type: Boolean, default: false }
2222
],
2323

24-
beforeInstall: function(options) {
24+
beforeInstall: function(options: any) {
2525
if (options.module) {
2626
// Resolve path to module
2727
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
@@ -34,35 +34,36 @@ module.exports = {
3434
} else {
3535
try {
3636
this.pathToModule = findParentModule(this.project, this.dynamicPath.dir);
37-
} catch(e) {
37+
} catch (e) {
3838
if (!options.skipImport) {
3939
throw `Error locating module for declaration\n\t${e}`;
4040
}
4141
}
4242
}
4343
},
4444

45-
normalizeEntityName: function (entityName) {
46-
var parsedPath = dynamicPathParser(this.project, entityName);
45+
normalizeEntityName: function (entityName: string) {
46+
const parsedPath = dynamicPathParser(this.project, entityName);
4747

4848
this.dynamicPath = parsedPath;
4949

50-
var defaultPrefix = '';
50+
let defaultPrefix = '';
5151
if (this.project.ngConfig &&
5252
this.project.ngConfig.apps[0] &&
5353
this.project.ngConfig.apps[0].prefix) {
5454
defaultPrefix = this.project.ngConfig.apps[0].prefix;
5555
}
5656

57-
var prefix = (this.options.prefix === 'false' || this.options.prefix === '') ? '' : (this.options.prefix || defaultPrefix);
57+
let prefix = (this.options.prefix === 'false' || this.options.prefix === '')
58+
? '' : (this.options.prefix || defaultPrefix);
5859
prefix = prefix && `${prefix}-`;
5960

6061

6162
this.selector = stringUtils.camelize(prefix + parsedPath.name);
6263
return parsedPath.name;
6364
},
6465

65-
locals: function (options) {
66+
locals: function (options: any) {
6667
options.spec = options.spec !== undefined ?
6768
options.spec :
6869
this.project.ngConfigObj.get('defaults.spec.directive');
@@ -75,7 +76,7 @@ module.exports = {
7576
},
7677

7778
files: function() {
78-
var fileList = getFiles.call(this);
79+
let fileList = getFiles.call(this) as Array<string>;
7980

8081
if (this.options && !this.options.spec) {
8182
fileList = fileList.filter(p => p.indexOf('__name__.directive.spec.ts') < 0);
@@ -84,11 +85,11 @@ module.exports = {
8485
return fileList;
8586
},
8687

87-
fileMapTokens: function (options) {
88+
fileMapTokens: function (options: any) {
8889
// Return custom template variables here.
8990
return {
9091
__path__: () => {
91-
var dir = this.dynamicPath.dir;
92+
let dir = this.dynamicPath.dir;
9293
if (!options.locals.flat) {
9394
dir += path.sep + options.dasherizedModuleName;
9495
}
@@ -98,12 +99,12 @@ module.exports = {
9899
};
99100
},
100101

101-
afterInstall: function(options) {
102+
afterInstall: function(options: any) {
102103
if (options.dryRun) {
103104
return;
104105
}
105106

106-
const returns = [];
107+
const returns: Array<any> = [];
107108
const className = stringUtils.classify(`${options.entity.name}Directive`);
108109
const fileName = stringUtils.dasherize(`${options.entity.name}.directive`);
109110
const fullGeneratePath = path.join(this.project.root, this.generatePath);
@@ -114,17 +115,19 @@ module.exports = {
114115
if (!options.skipImport) {
115116
returns.push(
116117
astUtils.addDeclarationToModule(this.pathToModule, className, importPath)
117-
.then(change => change.apply(NodeHost))
118-
.then((result) => {
118+
.then((change: any) => change.apply(NodeHost))
119+
.then((result: any) => {
119120
if (options.export) {
120121
return astUtils.addExportToModule(this.pathToModule, className, importPath)
121-
.then(change => change.apply(NodeHost));
122+
.then((change: any) => change.apply(NodeHost));
122123
}
123124
return result;
124125
}));
125-
this._writeStatusToUI(chalk.yellow, 'update', path.relative(this.project.root, this.pathToModule));
126+
this._writeStatusToUI(chalk.yellow,
127+
'update',
128+
path.relative(this.project.root, this.pathToModule));
126129
}
127130

128131
return Promise.all(returns);
129132
}
130-
};
133+
});

packages/angular-cli/blueprints/enum/index.js renamed to packages/angular-cli/blueprints/enum/index.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
const stringUtils = require('ember-cli-string-utils');
2-
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
2+
const dynamicPathParser = require('../../utilities/dynamic-path-parser');
3+
const Blueprint = require('../../ember-cli/lib/models/blueprint');
34

4-
module.exports = {
5+
export default Blueprint.extend({
56
description: '',
67

7-
normalizeEntityName: function (entityName) {
8-
var parsedPath = dynamicPathParser(this.project, entityName);
8+
normalizeEntityName: function (entityName: string) {
9+
const parsedPath = dynamicPathParser(this.project, entityName);
910

1011
this.dynamicPath = parsedPath;
1112
return parsedPath.name;
1213
},
1314

14-
locals: function (options) {
15+
locals: function (options: any) {
1516
this.fileName = stringUtils.dasherize(options.entity.name);
1617

1718
return {
@@ -33,4 +34,4 @@ module.exports = {
3334
}
3435
};
3536
}
36-
};
37+
});
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
const stringUtils = require('ember-cli-string-utils');
2-
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
2+
const dynamicPathParser = require('../../utilities/dynamic-path-parser');
3+
const Blueprint = require('../../ember-cli/lib/models/blueprint');
34

4-
module.exports = {
5+
export default Blueprint.extend({
56
description: '',
6-
7+
78
anonymousOptions: [
89
'<interface-type>'
910
],
10-
11-
normalizeEntityName: function (entityName) {
12-
var parsedPath = dynamicPathParser(this.project, entityName);
11+
12+
normalizeEntityName: function (entityName: string) {
13+
const parsedPath = dynamicPathParser(this.project, entityName);
1314

1415
this.dynamicPath = parsedPath;
1516
return parsedPath.name;
1617
},
1718

18-
locals: function (options) {
19-
var interfaceType = options.args [2]
19+
locals: function (options: any) {
20+
const interfaceType = options.args[2];
2021
this.fileName = stringUtils.dasherize(options.entity.name);
2122
if (interfaceType) {
22-
this.fileName += '.' + interfaceType;
23+
this.fileName += '.' + interfaceType;
2324
}
24-
var prefix = '';
25+
let prefix = '';
2526
if (this.project.ngConfig &&
2627
this.project.ngConfig.defaults &&
2728
this.project.ngConfig.defaults.prefixInterfaces) {
2829
prefix = 'I';
2930
}
30-
return {
31+
return {
3132
dynamicPath: this.dynamicPath.dir,
3233
flat: options.flat,
3334
fileName: this.fileName,
@@ -47,4 +48,4 @@ module.exports = {
4748
}
4849
};
4950
}
50-
};
51+
});

0 commit comments

Comments
 (0)