Skip to content

Commit

Permalink
Adding typescript options
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Eggers committed Jan 7, 2014
1 parent c494011 commit f0806c0
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 9 deletions.
16 changes: 16 additions & 0 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ var Generator = module.exports = function Generator(args, options) {
this.env.options.coffee = this.options.coffee;
}

if (typeof this.env.options.typescript === 'undefined') {
this.option('typescript', {
desc: 'Generate TypeScript instead of JavaScript'
});

// attempt to detect if user is using TS or not
// if cml arg provided, use that; else look for the existence of ts
if (!this.options.typescript &&
this.expandFiles(path.join(this.appPath, '/scripts/**/*.ts'), {}).length > 0) {
this.options.typescript = true;
}

this.env.options.typescript = this.options.typescript;
}

if (typeof this.env.options.minsafe === 'undefined') {
this.option('minsafe', {
desc: 'Generate AngularJS minification safe code'
Expand Down Expand Up @@ -263,6 +278,7 @@ Generator.prototype.createIndexHtml = function createIndexHtml() {

Generator.prototype.packageFiles = function () {
this.coffee = this.env.options.coffee;
this.typescript = this.env.options.typescript;
this.template('../../templates/common/_bower.json', 'bower.json');
this.template('../../templates/common/_package.json', 'package.json');
this.template('../../templates/common/Gruntfile.js', 'Gruntfile.js');
Expand Down
4 changes: 3 additions & 1 deletion route/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ util.inherits(Generator, ScriptBase);

Generator.prototype.rewriteAppJs = function () {
var coffee = this.env.options.coffee;
var typescript = this.env.options.typescript;

var config = {
file: path.join(
this.env.options.appPath,
'scripts/app.' + (coffee ? 'coffee' : 'js')
'scripts/app.' + (coffee ? 'coffee' : typescript ? 'ts': 'js')
),
needle: '.otherwise',
splicable: [
Expand Down
19 changes: 19 additions & 0 deletions script-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ var Generator = module.exports = function Generator() {
this.env.options.testPath = this.env.options.testPath || 'test/spec';
}

this.env.options.typescript = this.options.typescript;
if (typeof this.env.options.typescript === 'undefined') {
this.option('typescript');

// attempt to detect if user is using CS or not
// if cml arg provided, use that; else look for the existence of cs
if (!this.options.typescript &&
this.expandFiles(path.join(this.env.options.appPath, '/scripts/**/*.ts'), {}).length > 0) {
this.options.typescript = true;
}

this.env.options.typescript = this.options.typescript;
}

this.env.options.coffee = this.options.coffee;
if (typeof this.env.options.coffee === 'undefined') {
this.option('coffee');
Expand Down Expand Up @@ -59,6 +73,11 @@ var Generator = module.exports = function Generator() {
this.scriptSuffix = '.coffee';
}

if (this.env.options.typescript) {
sourceRoot = '/templates/typescript';
this.scriptSuffix = '.ts';
}

if (this.env.options.minsafe) {
sourceRoot += '-min';
}
Expand Down
50 changes: 44 additions & 6 deletions templates/common/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ module.exports = function (grunt) {
coffeeTest: {
files: ['test/spec/{,*/}*.{coffee,litcoffee,coffee.md}'],
tasks: ['newer:coffee:test', 'karma']
},<% } else if (typescript) { %>
typescript: {
files: ['<%%= yeoman.app %>/scripts/{,*/}*.ts'],
tasks: ['typescript:base']
},
typescriptTest: {
files: ['test/spec/{,*/}*.ts'],
tasks: ['typescript:test']
},<% } else { %>
js: {
files: ['<%%= yeoman.app %>/scripts/{,*/}*.js'],
Expand Down Expand Up @@ -63,7 +71,7 @@ module.exports = function (grunt) {
},
files: [
'<%%= yeoman.app %>/{,*/}*.html',
'.tmp/styles/{,*/}*.css',<% if (coffee) { %>
'.tmp/styles/{,*/}*.css',<% if (coffee || typescript) { %>
'.tmp/scripts/{,*/}*.js',<% } %>
'<%%= yeoman.app %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}'
]
Expand Down Expand Up @@ -111,9 +119,9 @@ module.exports = function (grunt) {
reporter: require('jshint-stylish')
},
all: [
'Gruntfile.js'<% if (!coffee) { %>,
'Gruntfile.js'<% if (!coffee && !typescript) { %>,
'<%%= yeoman.app %>/scripts/{,*/}*.js'<% } %>
]<% if (!coffee) { %>,
]<% if (!coffee && !typescript) { %>,
test: {
options: {
jshintrc: 'test/.jshintrc'
Expand Down Expand Up @@ -160,6 +168,33 @@ module.exports = function (grunt) {
}
},

<% if (typescript) { %>
// Compiles TypeScript to JavaScript
typescript: {
base: {
src: ['<%%= yeoman.app %>/scripts/{,*/}*.ts'],
dest: '.tmp/scripts',
options: {
module: 'amd', //or commonjs
target: 'es5', //or es3
'base_path': '<%%= yeoman.app %>/scripts', //quoting base_path to get around jshint warning.
sourcemap: true,
declaration: true
}
},
test: {
src: ['test/spec/{,*/}*.ts', 'test/e2e/{,*/}*.ts'],
dest: '.tmp/spec',
options: {
module: 'amd', //or commonjs
target: 'es5', //or es3
sourcemap: true,
declaration: true
}
}
},<% } %>


<% if (coffee) { %>
// Compiles CoffeeScript to JavaScript
coffee: {
Expand Down Expand Up @@ -343,17 +378,20 @@ module.exports = function (grunt) {
// Run some tasks in parallel to speed up the build process
concurrent: {
server: [<% if (coffee) { %>
'coffee:dist',<% } %><% if (compass) { %>
'coffee:dist',<% } %><% if (typescript) { %>
'typescript:base',<% } %><% if (compass) { %>
'compass:server'<% } else { %>
'copy:styles'<% } %>
],
test: [<% if (coffee) { %>
'coffee',<% } %><% if (compass) { %>
'coffee',<% } %><% if (typescript) { %>
'typescript',<% } %><% if (compass) { %>
'compass'<% } else { %>
'copy:styles'<% } %>
],
dist: [<% if (coffee) { %>
'coffee',<% } %><% if (compass) { %>
'coffee',<% } %><% if (typescript) { %>
'typescript',<% } %><% if (compass) { %>
'compass:dist',<% } else { %>
'copy:styles',<% } %>
'imagemin',
Expand Down
3 changes: 2 additions & 1 deletion templates/common/_bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"dependencies": {
"angular": "1.2.6",
"json3": "~3.2.6",
"es5-shim": "~2.1.0"<% if (bootstrap) { %>,
"es5-shim": "~2.1.0"<% if (typescript) { %>,
"definitivelyTyped": "https://github.com/borisyankov/DefinitelyTyped.git"<% } %><% if (bootstrap) { %>,
"jquery": "~1.10.2"<% if (compassBootstrap) { %>,
"sass-bootstrap": "~3.0.2"<% } else { %>,
"bootstrap": "~3.0.3"<% } } %><% if (resourceModule) { %>,
Expand Down
3 changes: 2 additions & 1 deletion templates/common/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"grunt-usemin": "~2.0.0",
"jshint-stylish": "~0.1.3",
"load-grunt-tasks": "~0.2.0",
"time-grunt": "~0.2.1"
"time-grunt": "~0.2.1",
"grunt-typescript": "~0.2.7"
},
"engines": {
"node": ">=0.8.0"
Expand Down
31 changes: 31 additions & 0 deletions test/test-file-creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,37 @@ describe('Angular generator', function () {
});
});

it('creates typescript files', function (done) {
var expected = ['app/.htaccess',
'app/404.html',
'app/favicon.ico',
'app/robots.txt',
'app/styles/main.scss',
'app/views/main.html',
['.bowerrc', /"directory": "app\/bower_components"/],
'Gruntfile.js',
'package.json',
['bower.json', /"name":\s+"temp"/],
'app/scripts/app.ts',
'app/index.html',
'app/scripts/controllers/main.ts',
'test/spec/controllers/main.ts'
];
helpers.mockPrompt(angular, {
compass: true,
bootstrap: true,
compassBootstrap: true,
modules: []
});

angular.env.options.typescript = true;
angular.run([], function () {
helpers.assertFiles(expected);
done();
});
});


/**
* Generic test function that can be used to cover the scenarios where a generator is creating both a source file
* and a test file. The function will run the respective generator, and then check for the existence of the two
Expand Down

0 comments on commit f0806c0

Please sign in to comment.