Skip to content
This repository was archived by the owner on Mar 26, 2018. It is now read-only.

Commit e579df9

Browse files
awkeddiemonge
authored andcommitted
feat(app): add support for --typescript option
Uses grunt-tsd to retrieve an initial set of typescript definitions. Adds templates for typescript versions of angular items. Based upon commit 'refs/pull/539/head' of github.com:yeoman/generator-angular
1 parent 8a9a8af commit e579df9

24 files changed

+478
-18
lines changed

app/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ var Generator = module.exports = function Generator(args, options) {
5656
this.env.options.coffee = this.options.coffee;
5757
}
5858

59+
if (typeof this.env.options.typescript === 'undefined') {
60+
this.option('typescript', {
61+
desc: 'Generate TypeScript instead of JavaScript'
62+
});
63+
64+
// attempt to detect if user is using TS or not
65+
// if cml arg provided, use that; else look for the existence of ts
66+
if (!this.options.typescript &&
67+
this.expandFiles(path.join(this.appPath, '/scripts/**/*.ts'), {}).length > 0) {
68+
this.options.typescript = true;
69+
}
70+
71+
this.env.options.typescript = this.options.typescript;
72+
}
73+
5974
this.hookFor('angular:common', {
6075
args: args
6176
});
@@ -303,10 +318,14 @@ Generator.prototype.createIndexHtml = function createIndexHtml() {
303318

304319
Generator.prototype.packageFiles = function packageFiles() {
305320
this.coffee = this.env.options.coffee;
321+
this.typescript = this.env.options.typescript;
306322
this.template('root/_bower.json', 'bower.json');
307323
this.template('root/_bowerrc', '.bowerrc');
308324
this.template('root/_package.json', 'package.json');
309325
this.template('root/_Gruntfile.js', 'Gruntfile.js');
326+
if (this.typescript) {
327+
this.template('root/_tsd.json', 'tsd.json');
328+
}
310329
this.template('root/README.md', 'README.md');
311330
};
312331

main/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ util.inherits(Generator, ScriptBase);
1212

1313
Generator.prototype.createAppFile = function createAppFile() {
1414
this.angularModules = this.env.options.angularDeps;
15+
this.ngCookies = this.env.options.ngCookies;
16+
this.ngResource = this.env.options.ngResource;
17+
this.ngSanitize = this.env.options.ngSanitize;
1518
this.ngRoute = this.env.options.ngRoute;
1619
this.appTemplate('app', 'scripts/app');
1720
};

readme.md

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ angular.module('myMod').config(function ($provide) {
188188
## Options
189189
In general, these options can be applied to any generator, though they only affect generators that produce scripts.
190190

191-
### CoffeeScript
192-
For generators that output scripts, the `--coffee` option will output CoffeeScript instead of JavaScript.
191+
### CoffeeScript and TypeScript
192+
For generators that output scripts, the `--coffee` option will output CoffeeScript instead of JavaScript, and `--typescript` will output TypeScript instead of JavaScript.
193193

194194
For example:
195195
```bash
@@ -202,9 +202,42 @@ angular.module('myMod')
202202
.controller 'UserCtrl', ($scope) ->
203203
```
204204

205-
A project can mix CoffeScript and JavaScript files.
205+
For example:
206+
```bash
207+
yo angular:controller user --typescript
208+
```
209+
210+
Produces `app/scripts/controller/user.ts`:
211+
```typescript
212+
/// <reference path="../app.ts" />
213+
214+
'use strict';
215+
216+
module demoApp {
217+
export interface IUserScope extends ng.IScope {
218+
awesomeThings: any[];
219+
}
220+
221+
export class UserCtrl {
222+
223+
constructor (private $scope:IUserScope) {
224+
$scope.awesomeThings = [
225+
'HTML5 Boilerplate',
226+
'AngularJS',
227+
'Karma'
228+
];
229+
}
230+
}
231+
}
232+
233+
angular.module('demoApp')
234+
.controller('UserCtrl', demoApp.UserCtrl);
235+
```
236+
237+
238+
A project can mix TypeScript, CoffeScript, and JavaScript files.
206239

207-
To output JavaScript files, even if CoffeeScript files exist (the default is to output CoffeeScript files if the generator finds any in the project), use `--coffee=false`.
240+
To output JavaScript files, even if CoffeeScript (or TypeScript) files exist (the default is to output CoffeeScript files if the generator finds any in the project), use `--coffee=false` and/or `--typescript=false`.
208241

209242
### Minification Safe
210243

route/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ var Generator = module.exports = function Generator() {
1414
required: false
1515
});
1616

17+
var coffee = this.env.options.coffee;
18+
var typescript = this.env.options.typescript;
1719
var bower = require(path.join(process.cwd(), 'bower.json'));
1820
var match = require('fs').readFileSync(path.join(
1921
this.env.options.appPath,
20-
'scripts/app.' + (this.env.options.coffee ? 'coffee' : 'js')
22+
'scripts/app.' + (coffee ? 'coffee' : typescript ? 'ts': 'js')
2123
), 'utf-8').match(/\.when/);
2224

2325
if (
@@ -52,10 +54,11 @@ Generator.prototype.rewriteAppJs = function () {
5254
this.uri = this.options.uri;
5355
}
5456

57+
var typescript = this.env.options.typescript;
5558
var config = {
5659
file: path.join(
5760
this.env.options.appPath,
58-
'scripts/app.' + (coffee ? 'coffee' : 'js')
61+
'scripts/app.' + (coffee ? 'coffee' : typescript ? 'ts': 'js')
5962
),
6063
needle: '.otherwise',
6164
splicable: [

script-base.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ var Generator = module.exports = function Generator() {
3434

3535
this.env.options.testPath = this.env.options.testPath || bowerJson.testPath || 'test/spec';
3636

37+
this.env.options.typescript = this.options.typescript;
38+
if (typeof this.env.options.typescript === 'undefined') {
39+
this.option('typescript');
40+
41+
// attempt to detect if user is using TS or not
42+
// if cml arg provided, use that; else look for the existence of ts
43+
if (!this.options.typescript &&
44+
this.expandFiles(path.join(this.env.options.appPath, '/scripts/**/*.ts'), {}).length > 0) {
45+
this.options.typescript = true;
46+
}
47+
48+
this.env.options.typescript = this.options.typescript;
49+
}
50+
3751
this.env.options.coffee = this.options.coffee;
3852
if (typeof this.env.options.coffee === 'undefined') {
3953
this.option('coffee');
@@ -56,6 +70,11 @@ var Generator = module.exports = function Generator() {
5670
this.scriptSuffix = '.coffee';
5771
}
5872

73+
if (this.env.options.typescript) {
74+
sourceRoot = '/templates/typescript';
75+
this.scriptSuffix = '.ts';
76+
}
77+
5978
this.sourceRoot(path.join(__dirname, sourceRoot));
6079
};
6180

templates/common/app/.buildignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
*.coffee
1+
*.coffee
2+
*.ts

templates/common/root/_Gruntfile.js

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ module.exports = function (grunt) {
4444
coffeeTest: {
4545
files: ['test/spec/{,*/}*.{coffee,litcoffee,coffee.md}'],
4646
tasks: ['newer:coffee:test', 'karma']
47+
},<% } else if (typescript) { %>
48+
typescript: {
49+
files: ['<%%= yeoman.app %>/scripts/{,*/}*.ts'],
50+
tasks: ['typescript:base']
51+
},
52+
typescriptTest: {
53+
files: ['test/spec/{,*/}*.ts'],
54+
tasks: ['typescript:test', 'karma']
4755
},<% } else { %>
4856
js: {
4957
files: ['<%%= yeoman.app %>/scripts/{,*/}*.js'],
@@ -73,7 +81,7 @@ module.exports = function (grunt) {
7381
},
7482
files: [
7583
'<%%= yeoman.app %>/{,*/}*.html',
76-
'.tmp/styles/{,*/}*.css',<% if (coffee) { %>
84+
'.tmp/styles/{,*/}*.css',<% if (coffee || typescript) { %>
7785
'.tmp/scripts/{,*/}*.js',<% } %>
7886
'<%%= yeoman.app %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}'
7987
]
@@ -139,10 +147,10 @@ module.exports = function (grunt) {
139147
},
140148
all: {
141149
src: [
142-
'Gruntfile.js'<% if (!coffee) { %>,
150+
'Gruntfile.js'<% if (!coffee && !typescript) { %>,
143151
'<%%= yeoman.app %>/scripts/{,*/}*.js'<% } %>
144152
]
145-
}<% if (!coffee) { %>,
153+
}<% if (!coffee && !typescript) { %>,
146154
test: {
147155
options: {
148156
jshintrc: 'test/.jshintrc'
@@ -248,7 +256,41 @@ module.exports = function (grunt) {
248256
src: ['<%%= yeoman.app %>/styles/{,*/}*.{scss,sass}'],
249257
ignorePath: /(\.\.\/){1,2}bower_components\//
250258
}<% } %>
251-
},<% if (coffee) { %>
259+
}, <% if (typescript) { %>
260+
// Compiles TypeScript to JavaScript
261+
typescript: {
262+
base: {
263+
src: ['<%%= yeoman.app %>/scripts/{,*/}*.ts'],
264+
dest: '.tmp/scripts',
265+
options: {
266+
module: 'amd', //or commonjs
267+
target: 'es5', //or es3
268+
'base_path': '<%%= yeoman.app %>/scripts', //quoting base_path to get around jshint warning.
269+
sourcemap: true,
270+
declaration: true
271+
}
272+
},
273+
test: {
274+
src: ['test/spec/{,*/}*.ts', 'test/e2e/{,*/}*.ts'],
275+
dest: '.tmp/spec',
276+
options: {
277+
module: 'amd', //or commonjs
278+
target: 'es5', //or es3
279+
sourcemap: true,
280+
declaration: true
281+
}
282+
}
283+
},
284+
tsd: {
285+
refresh: {
286+
options: {
287+
// execute a command
288+
command: 'reinstall',
289+
config: 'tsd.json'
290+
}
291+
}
292+
},
293+
<% } %><% if (coffee) { %>
252294

253295
// Compiles CoffeeScript to JavaScript
254296
coffee: {
@@ -496,17 +538,20 @@ module.exports = function (grunt) {
496538
// Run some tasks in parallel to speed up the build process
497539
concurrent: {
498540
server: [<% if (coffee) { %>
499-
'coffee:dist',<% } %><% if (compass) { %>
541+
'coffee:dist',<% } %><% if (typescript) { %>
542+
'typescript:base',<% } %><% if (compass) { %>
500543
'compass:server'<% } else { %>
501544
'copy:styles'<% } %>
502545
],
503546
test: [<% if (coffee) { %>
504-
'coffee',<% } %><% if (compass) { %>
547+
'coffee',<% } %><% if (typescript) { %>
548+
'typescript',<% } %><% if (compass) { %>
505549
'compass'<% } else { %>
506550
'copy:styles'<% } %>
507551
],
508552
dist: [<% if (coffee) { %>
509-
'coffee',<% } %><% if (compass) { %>
553+
'coffee',<% } %><% if (typescript) { %>
554+
'typescript',<% } %><% if (compass) { %>
510555
'compass:dist',<% } else { %>
511556
'copy:styles',<% } %>
512557
'imagemin',
@@ -534,7 +579,8 @@ module.exports = function (grunt) {
534579

535580
grunt.task.run([
536581
'clean:server',
537-
'wiredep',
582+
'wiredep',<% if (typescript) { %>
583+
'tsd:refresh',<% } %>
538584
'concurrent:server',
539585
'postcss:server',
540586
'connect:livereload',
@@ -549,7 +595,8 @@ module.exports = function (grunt) {
549595

550596
grunt.registerTask('test', [
551597
'clean:server',
552-
'wiredep',
598+
'wiredep',<% if (typescript) { %>
599+
'tsd:refresh',<% } %>
553600
'concurrent:test',
554601
'postcss',
555602
'connect:test',
@@ -558,7 +605,8 @@ module.exports = function (grunt) {
558605

559606
grunt.registerTask('build', [
560607
'clean:dist',
561-
'wiredep',
608+
'wiredep',<% if (typescript) { %>
609+
'tsd:refresh',<% } %>
562610
'useminPrepare',
563611
'concurrent:dist',
564612
'postcss',

templates/common/root/_package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
"grunt-newer": "^1.1.0",
2525
"grunt-ng-annotate": "^0.9.2",
2626
"grunt-postcss": "^0.5.5",
27-
"grunt-svgmin": "^2.0.0",
27+
"grunt-svgmin": "^2.0.0",<% if (typescript) { %>
28+
"grunt-tsd": "^0.1.0",
29+
"grunt-typescript": "^0.6.2",<% } %>
2830
"grunt-usemin": "^3.0.0",
2931
"grunt-wiredep": "^2.0.0",
3032
"jit-grunt": "^0.9.1",

templates/common/root/_tsd.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"version": "v4",
3+
"repo": "borisyankov/DefinitelyTyped",
4+
"ref": "master",
5+
"path": "typings",
6+
"bundle": "typings/tsd.d.ts",
7+
"installed": {
8+
"jquery/jquery.d.ts": {
9+
"commit": "f3244190e20af6901e865f4cc58127d19216baa1"
10+
},
11+
"angularjs/angular.d.ts": {
12+
"commit": "f3244190e20af6901e865f4cc58127d19216baa1"
13+
},
14+
"angularjs/angular-resource.d.ts": {
15+
"commit": "aadd63ecae3feb76ea2d4be80511e266b5c2c4a7"
16+
},
17+
"angularjs/angular-route.d.ts": {
18+
"commit": "f23cd55e319a0f67362ca0dbc4cf3e2ec1339f4e"
19+
},
20+
"angularjs/angular-mocks.d.ts": {
21+
"commit": "f23cd55e319a0f67362ca0dbc4cf3e2ec1339f4e"
22+
},
23+
"angularjs/angular-cookies.d.ts": {
24+
"commit": "f23cd55e319a0f67362ca0dbc4cf3e2ec1339f4e"
25+
},
26+
"angularjs/angular-sanitize.d.ts": {
27+
"commit": "f23cd55e319a0f67362ca0dbc4cf3e2ec1339f4e"
28+
},
29+
"jasmine/jasmine.d.ts": {
30+
"commit": "f23cd55e319a0f67362ca0dbc4cf3e2ec1339f4e"
31+
}
32+
}
33+
}

templates/typescript/app.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path="../../typings/angularjs/angular.d.ts" /><% if (ngCookies) { %>
2+
/// <reference path="../../typings/angularjs/angular-cookies.d.ts" /><% } %><% if (ngResource) { %>
3+
/// <reference path="../../typings/angularjs/angular-resource.d.ts" /><% } %><% if (ngSanitize) { %>
4+
/// <reference path="../../typings/angularjs/angular-sanitize.d.ts" /><% } %><% if (ngRoute) { %>
5+
/// <reference path="../../typings/angularjs/angular-route.d.ts" /><% } %>
6+
7+
'use strict';
8+
9+
angular.module('<%= scriptAppName %>', [<%= angularModules %>])<% if (ngRoute) { %>
10+
.config(($routeProvider:ng.route.IRouteProvider) => {
11+
$routeProvider
12+
.when('/', {
13+
templateUrl: 'views/main.html',
14+
controller: 'MainCtrl'
15+
})
16+
.otherwise({
17+
redirectTo: '/'
18+
});
19+
})<% } %>;

0 commit comments

Comments
 (0)