From f0806c0e2f9dcbbde9cc0fd008556f601df3fa4c Mon Sep 17 00:00:00 2001 From: Jacob Eggers Date: Tue, 7 Jan 2014 03:12:45 +0100 Subject: [PATCH] Adding typescript options --- app/index.js | 16 +++++++++++ route/index.js | 4 ++- script-base.js | 19 +++++++++++++ templates/common/Gruntfile.js | 50 ++++++++++++++++++++++++++++++---- templates/common/_bower.json | 3 +- templates/common/_package.json | 3 +- test/test-file-creation.js | 31 +++++++++++++++++++++ 7 files changed, 117 insertions(+), 9 deletions(-) diff --git a/app/index.js b/app/index.js index 431f6ea31..a27132b9c 100644 --- a/app/index.js +++ b/app/index.js @@ -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' @@ -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'); diff --git a/route/index.js b/route/index.js index f30d60939..0fd7439a8 100644 --- a/route/index.js +++ b/route/index.js @@ -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: [ diff --git a/script-base.js b/script-base.js index 32bd81756..4fdc960b6 100644 --- a/script-base.js +++ b/script-base.js @@ -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'); @@ -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'; } diff --git a/templates/common/Gruntfile.js b/templates/common/Gruntfile.js index 32ba32ec3..4fb4d547e 100644 --- a/templates/common/Gruntfile.js +++ b/templates/common/Gruntfile.js @@ -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'], @@ -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}' ] @@ -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' @@ -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: { @@ -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', diff --git a/templates/common/_bower.json b/templates/common/_bower.json index da12c309c..40181eabc 100644 --- a/templates/common/_bower.json +++ b/templates/common/_bower.json @@ -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) { %>, diff --git a/templates/common/_package.json b/templates/common/_package.json index 3859b32ed..f06b25757 100644 --- a/templates/common/_package.json +++ b/templates/common/_package.json @@ -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" diff --git a/test/test-file-creation.js b/test/test-file-creation.js index 7f97be91b..54e91b47a 100644 --- a/test/test-file-creation.js +++ b/test/test-file-creation.js @@ -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