Skip to content

Commit dd980be

Browse files
committed
Merge pull request #254 from TypeStrong/resolve-251
Resolve 251
2 parents 1120652 + b0c9a8a commit dd980be

File tree

17 files changed

+307
-63
lines changed

17 files changed

+307
-63
lines changed

Gruntfile.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,20 @@ module.exports = function (grunt) {
178178
},
179179
files_testFilesUsedWithDestAsAJSFile: {
180180
test: true,
181-
files: [{ src: ['test/multifile/a/**/*.ts'], dest: 'test/multifile/a/testDest.js' }],
181+
files: [
182+
{ src: ['test/multifile/a/**/*.ts'],
183+
dest: 'test/multifile/files_testFilesUsedWithDestAsAJSFile/testDest.js' }
184+
],
182185
options: {
183186
fast: 'never'
184187
}
185188
},
186189
files_testFilesUsedWithDestAsAFolder: {
187190
test: true,
188-
files: [{ src: ['test/multifile/a/**/*.ts'], dest: 'test/multifile/a' }],
191+
files: [
192+
{ src: ['test/multifile/a/**/*.ts'],
193+
dest: 'test/multifile/files_testFilesUsedWithDestAsAJSFolder' }
194+
],
189195
options: {
190196
fast: 'never'
191197
}
@@ -198,6 +204,7 @@ module.exports = function (grunt) {
198204
}
199205
},
200206
files_testWarnIfFilesHasDestArray: {
207+
// TODO: This test is currently broken. grunt-ts does not warn.
201208
test: true,
202209
files: [{ src: ['test/multifile/a/**/*.ts'], dest: ['test/multifile/a', 'test/multifile/b'] }],
203210
options: {
@@ -214,8 +221,8 @@ module.exports = function (grunt) {
214221
files_testFilesObjectFormatWorks: {
215222
test: true,
216223
files: {
217-
'test/multifile/a.js': ['test/multifile/a/**/*.ts'],
218-
'test/multifile/b.js': ['test/multifile/b/**/*.ts']
224+
'test/files_ObjectFormat/a.js': ['test/multifile/a/**/*.ts'],
225+
'test/files_ObjectFormat/b.js': ['test/multifile/b/**/*.ts', 'test/simple/ts/**/*.ts']
219226
},
220227
options: {
221228
fast: 'never'

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,17 @@ The easiest/fastest way to work on grunt-ts is to modify `tasksToTest` toward th
968968

969969
Without using `tasksToTest` while working on grunt-ts, the old grunt-ts remains in memory for successive tasks on the same run. This means you might have to run your grunt commands twice; once to compile grunt-ts and once to see how the new grunt-ts works with your code.
970970

971+
### Quickstart for debugging Grunt plugins with Node Inspector
972+
973+
Install [Node Inspector](https://github.com/node-inspector/node-inspector) via npm:
974+
975+
`npm install -g node-inspector`
976+
977+
Example command-line to debug a grunt-ts task on Windows:
978+
979+
`node-debug --debug-brk %appdata%\npm\node_modules\grunt-cli\bin\grunt ts:files_testFilesUsedWithDestAsAJSFile`
980+
981+
Set breakpoints in the Chrome dev tools, or use `debugger;` where needed.
971982

972983
### Additional commands
973984
Update the current `grunt-ts` to be the last known good version (dogfood). Commit message should be `Update LKG`.

tasks/modules/compile.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function executeNode(args) {
2626
});
2727
}
2828
/////////////////////////////////////////////////////////////////
29-
// Fast Compilation
29+
// Fast Compilation
3030
/////////////////////////////////////////////////////////////////
3131
// Map to store if the cache was cleared after the gruntfile was parsed
3232
var cacheClearedOnce = {};
@@ -62,14 +62,14 @@ function getTsc(binPath) {
6262
exports.grunt.log.writeln('Using tsc v' + pkg.version);
6363
return path.join(binPath, 'tsc');
6464
}
65-
function compileAllFiles(targetFiles, target, task, targetName) {
65+
function compileAllFiles(targetFiles, target, task, targetName, outFile) {
6666
// Make a local copy so we can modify files without having external side effects
6767
var files = _.map(targetFiles, function (file) { return file; });
6868
var newFiles = files;
6969
if (task.fast === 'watch') {
7070
// if this is the first time its running after this file was loaded
7171
if (cacheClearedOnce[exports.grunt.task.current.target] === undefined) {
72-
// Then clear the cache for this target
72+
// Then clear the cache for this target
7373
clearCache(targetName);
7474
}
7575
}
@@ -159,7 +159,15 @@ function compileAllFiles(targetFiles, target, task, targetName) {
159159
}
160160
}
161161
// Target options:
162-
if (target.out) {
162+
if (outFile) {
163+
if (utils.isJavaScriptFile(outFile)) {
164+
args.push('--out', outFile);
165+
}
166+
else {
167+
args.push('--outDir', outFile);
168+
}
169+
}
170+
else if (target.out) {
163171
args.push('--out', target.out);
164172
}
165173
if (target.outDir) {
@@ -174,13 +182,18 @@ function compileAllFiles(targetFiles, target, task, targetName) {
174182
}
175183
else {
176184
if (target.dest === 'src') {
177-
console.warn(('WARNING: Destination for target "' + targetName + '" is "src", which is the default. If you have' + ' forgotten to specify a "dest" parameter, please add it. If this is correct, you may wish' + ' to change the "dest" parameter to "src/" or just ignore this warning.').magenta);
185+
console.warn(('WARNING: Destination for target "' + targetName + '" is "src", which is the default. If you have' +
186+
' forgotten to specify a "dest" parameter, please add it. If this is correct, you may wish' +
187+
' to change the "dest" parameter to "src/" or just ignore this warning.').magenta);
178188
}
179189
if (Array.isArray(target.dest)) {
180190
if (target.dest.length === 0) {
181191
}
182192
else if (target.dest.length > 0) {
183-
console.warn((('WARNING: "dest" for target "' + targetName + '" is an array. This is not supported by the' + ' TypeScript compiler or grunt-ts.' + ((target.dest.length > 1) ? ' Only the first "dest" will be used. The' + ' remaining items will be truncated.' : ''))).magenta);
193+
console.warn((('WARNING: "dest" for target "' + targetName + '" is an array. This is not supported by the' +
194+
' TypeScript compiler or grunt-ts.' +
195+
((target.dest.length > 1) ? ' Only the first "dest" will be used. The' +
196+
' remaining items will be truncated.' : ''))).magenta);
184197
args.push('--outDir', target.dest[0]);
185198
}
186199
}
@@ -233,4 +246,3 @@ function compileAllFiles(targetFiles, target, task, targetName) {
233246
});
234247
}
235248
exports.compileAllFiles = compileAllFiles;
236-
//# sourceMappingURL=compile.js.map

tasks/modules/compile.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import _ = require('lodash');
77
import utils = require('./utils');
88
import cache = require('./cacheUtils');
99

10-
var Promise: typeof Promise = require('es6-promise').Promise;
10+
var Promise = require('es6-promise').Promise;
1111
export var grunt: IGrunt = require('grunt');
1212

1313
export interface ICompileResult {
@@ -36,7 +36,7 @@ function executeNode(args: string[]): Promise<ICompileResult> {
3636
}
3737

3838
/////////////////////////////////////////////////////////////////
39-
// Fast Compilation
39+
// Fast Compilation
4040
/////////////////////////////////////////////////////////////////
4141

4242
// Map to store if the cache was cleared after the gruntfile was parsed
@@ -85,7 +85,11 @@ function getTsc(binPath: string): string {
8585
return path.join(binPath, 'tsc');
8686
}
8787

88-
export function compileAllFiles(targetFiles: string[], target: ITargetOptions, task: ITaskOptions, targetName: string): Promise<ICompileResult> {
88+
export function compileAllFiles(targetFiles: string[],
89+
target: ITargetOptions,
90+
task: ITaskOptions,
91+
targetName: string,
92+
outFile: string): Promise<ICompileResult> {
8993

9094
// Make a local copy so we can modify files without having external side effects
9195
var files = _.map(targetFiles, (file) => file);
@@ -96,7 +100,7 @@ export function compileAllFiles(targetFiles: string[], target: ITargetOptions, t
96100
// if this is the first time its running after this file was loaded
97101
if (cacheClearedOnce[grunt.task.current.target] === undefined) {
98102

99-
// Then clear the cache for this target
103+
// Then clear the cache for this target
100104
clearCache(targetName);
101105
}
102106
}
@@ -196,7 +200,13 @@ export function compileAllFiles(targetFiles: string[], target: ITargetOptions, t
196200
}
197201

198202
// Target options:
199-
if (target.out) {
203+
if (outFile) {
204+
if (utils.isJavaScriptFile(outFile)) {
205+
args.push('--out', outFile);
206+
} else {
207+
args.push('--outDir', outFile);
208+
}
209+
} else if (target.out) {
200210
args.push('--out', target.out);
201211
}
202212

tasks/modules/utils.js

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tasks/modules/utils.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export function isJavaScriptFile(filePath: string): boolean {
8080
return false;
8181
}
8282

83-
/** function for formatting strings
83+
/** function for formatting strings
8484
* ('{0} says {1}','la','ba' ) => 'la says ba'
8585
*/
8686
export function format(str: string, ...args: any[]) {
@@ -136,7 +136,7 @@ export function getTempFile(prefix?: string, dir: string = '', extension = '.tmp
136136
* Get all files from a directory and all its subdirectories.
137137
* @param {String} dirPath A path to a directory
138138
* @param {RegExp|Function} exclude Defines which files should be excluded.
139-
Can be a RegExp (whole filepath is tested) or a Function which will get the filepath
139+
Can be a RegExp (whole filepath is tested) or a Function which will get the filepath
140140
as an argument and should return true (exclude file) or false (do not exclude).
141141
* @returns {Array} An array of files
142142
*/
@@ -147,8 +147,8 @@ export function getFiles(dirPath, exclude?: (filename: string) => boolean): stri
147147
/**
148148
* Get all directories from a directory and all its subdirectories.
149149
* @param {String} dirPath A path to a directory
150-
* @param {RegExp|Function} exclude Defines which directories should be excluded.
151-
Can be a RegExp (whole dirpath is tested) or a Function which will get the dirpath
150+
* @param {RegExp|Function} exclude Defines which directories should be excluded.
151+
Can be a RegExp (whole dirpath is tested) or a Function which will get the dirpath
152152
as an argument and should return true (exclude dir) or false (do not exclude).
153153
* @returns {Array} An array of directories
154154
*/
@@ -159,8 +159,8 @@ export function getDirs(dirPath, exclude?: (filename: string) => boolean): strin
159159
/**
160160
* Get all files or directories from a directory and all its subdirectories.
161161
* @param {String} dirPath A path to a directory
162-
* @param {RegExp|Function} exclude Defines which files or directories should be excluded.
163-
Can be a RegExp (whole path is tested) or a Function which will get the path
162+
* @param {RegExp|Function} exclude Defines which files or directories should be excluded.
163+
Can be a RegExp (whole path is tested) or a Function which will get the path
164164
as an argument and should return true (exclude) or false (do not exclude).
165165
* @param {Boolean} getFiles Whether to get files (true) or directories (false).
166166
* @returns {Array} An array of files or directories
@@ -252,3 +252,13 @@ export function firstElementWithValue<T>(elements: T[], defaultResult: T = null)
252252
});
253253
return result;
254254
}
255+
256+
export function getOrGetFirst(getFrom: string | string[]) : string {
257+
if (_.isArray(getFrom)) {
258+
if (getFrom.length > 0) {
259+
return getFrom[0];
260+
}
261+
return '';
262+
}
263+
return <string>getFrom;
264+
}

tasks/ts.js

Lines changed: 23 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)