Skip to content

Commit 859d106

Browse files
committed
Add emitGruntEvents option and fix #308
1 parent 7e5bab3 commit 859d106

File tree

9 files changed

+104
-36
lines changed

9 files changed

+104
-36
lines changed

Gruntfile.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ module.exports = function (grunt) {
394394
fail: true,
395395
html: ['test/html/**/*.tpl.html'],
396396
out: 'test/html/out.js',
397+
options: {
398+
emitGruntEvents: true
399+
}
397400
},
398401
htmltest: {
399402
test: true,
@@ -585,15 +588,17 @@ module.exports = function (grunt) {
585588
baseDir: 'test/fail/ts',
586589
// watch: 'test',
587590
options: { // overide the main options for this target
588-
sourceMap: false
591+
sourceMap: false,
592+
emitGruntEvents: true
589593
}
590594
},
591595
test_failOnTypeErrors: {
592596
fail: true,
593597
src: ['test/failontypeerror/**/*.ts'],
594598
outDir: 'test/failontypeerror/js',
595599
options: {
596-
failOnTypeErrors: true
600+
failOnTypeErrors: true,
601+
emitGruntEvents: true
597602
}
598603
},
599604
files_testfailedcompilation: {
@@ -607,7 +612,8 @@ module.exports = function (grunt) {
607612
dest: 'test/files_testfailedcompilation/b/out.js'
608613
}],
609614
options: {
610-
fast: 'never'
615+
fast: 'never',
616+
emitGruntEvents: true
611617
}
612618
},
613619
withemptymodule: {
@@ -621,7 +627,8 @@ module.exports = function (grunt) {
621627
withwrongmodule: {
622628
fail: true,
623629
options: {
624-
module: 'nothing'
630+
module: 'nothing',
631+
emitGruntEvents: true
625632
},
626633
src: 'test/withwrongmodule/ts/*.ts',
627634
outDir: 'test/withwrongmodule/js'
@@ -895,13 +902,29 @@ module.exports = function (grunt) {
895902
return memo;
896903
}, []));
897904

898-
// Collect test tasks
899-
grunt.registerTask('test_fail', grunt.util._.reduce(grunt.config.get('ts'), function (memo, task, name) {
900-
if (task.fail) {
901-
memo.push('ts:' + name);
902-
}
903-
return memo;
904-
}, []));
905+
(function() {
906+
// Collect fail tasks
907+
var grunt_ts_total_failures = 0,
908+
failTasks = grunt.util._.reduce(grunt.config.get('ts'), function (memo, task, name) {
909+
if (task.fail) {
910+
memo.push('ts:' + name);
911+
}
912+
return memo;
913+
}, []);
914+
grunt.registerTask('test_fail', failTasks);
915+
grunt.event.on('grunt-ts.failure', function() {
916+
grunt_ts_total_failures += 1;
917+
});
918+
grunt.registerTask('validate_failure_count', 'Counts failure events emitted by grunt-ts', function() {
919+
console.log('Expected ' + failTasks.length + ' task failures, got ' +
920+
grunt_ts_total_failures + ' failures.');
921+
if (failTasks.length === 0) {
922+
console.log('Expected more than zero failures.');
923+
return false;
924+
}
925+
return (grunt_ts_total_failures === failTasks.length);
926+
});
927+
}());
905928

906929
// Loading it for testing since we have in a local 'tasks' folder
907930
grunt.loadTasks('tasks');
@@ -922,7 +945,7 @@ module.exports = function (grunt) {
922945
grunt.registerTask('build', ['prep', 'ts-internal', 'tslint:source']);
923946

924947
// Test
925-
grunt.registerTask('fail', ['continueOn', 'test_fail', 'continueOff']);
948+
grunt.registerTask('fail', ['continueOn', 'test_fail', 'continueOff', 'validate_failure_count']);
926949
grunt.registerTask('test', ['stageFiles', 'test_all', 'fail', 'nodeunit:fast', 'nodeunit:slow',
927950
'tslint:transformedHtml', 'clean:testPost']);
928951

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ For file ordering, look at [JavaScript Generation](#javascript-generation).
9999
|[compiler](#compiler)|option|`string` - path to custom compiler|
100100
|[declaration](#declaration)|option|`true`, `false` (default) - indicates that definition files should be emitted.|
101101
|[emitDecoratorMetadata](#emitdecoratormetadata)|option|`true`, `false` (default) - set to true to emit metadata for ES7 decorators (will enable experimentalDecorators)|
102+
|[emitGruntEvents](#emitgruntevents)|option|`true`, `false` (default) - set to true to raise an event in Grunt upon failed builds.|
102103
|[experimentalAsyncFunctions](#experimentalasyncfunctions)|option|`true`, `false` (default) - set to true to enable support for ES7 async functions (in ES6 mode only)|
103104
|[experimentalDecorators](#experimentaldecorators)|option|`true`, `false` (default) - set to true to enable support for ES7 decorators|
104105
|[failOnTypeErrors](#failontypeerrors)|option|`true` (default), `false` - fail Grunt pipeline if there is a type error. (See also [noEmitOnError](#noemithelpers))|
@@ -519,6 +520,32 @@ grunt.initConfig({
519520
});
520521
````
521522

523+
#### emitGruntEvents
524+
525+
````javascript
526+
true | false (default)
527+
````
528+
529+
Set to true to emit events in Grunt upon significant events in grunt-ts. This is used by the task `validate_failure_count` in the Gruntfile.js of grunt-ts itself. Currently, the only supported event is `grunt-ts.failure` which will be raised upon a failed build if `emitGruntEvents` is true. This is only available in grunt-ts 5.2.0 or higher.
530+
531+
```javascript
532+
grunt.initConfig({
533+
ts: {
534+
options: {
535+
emitGruntEvents: true
536+
}
537+
}
538+
});
539+
```
540+
541+
Example usage:
542+
543+
```javascript
544+
grunt.event.on('grunt-ts.failure', function() {
545+
console.log('It failed!!!!!!');
546+
});
547+
```
548+
522549
#### experimentalAsyncFunctions
523550

524551
````javascript

tasks/modules/defaults.js

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

tasks/modules/defaults.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import * as utils from './utils';
44

5-
export const TypeScriptDefaults: IGruntTSOptions = {
5+
const TypeScriptDefaults: IGruntTSOptions = {
66
allowBool: false,
77
allowImportModule: false,
88
amdloader: null,
@@ -29,7 +29,8 @@ export const TypeScriptDefaults: IGruntTSOptions = {
2929
htmlOutputTemplate: null,
3030
htmlOutDir: null,
3131
htmlOutDirFlatten: null,
32-
failOnTypeErrors: true,
32+
failOnTypeErrors: null,
33+
emitGruntEvents: null,
3334
noEmitOnError: false,
3435
preserveConstEnums: false,
3536
suppressImplicitAnyIndexErrors: false,
@@ -61,5 +62,7 @@ function applyGruntTSDefaults(options: IGruntTSOptions) {
6162
options.htmlOutDirFlatten = false;
6263
options.fast = 'watch';
6364
options.removeComments = true;
65+
options.failOnTypeErrors = true;
66+
options.emitGruntEvents = false;
6467
return options;
6568
}

tasks/modules/interfaces.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ interface ITaskOptions {
8181
experimentalAsyncFunctions: string;
8282
/** Sepecifies the root directory of input files. Use to control the output directory structure with --outDir. */
8383
rootDir: string;
84+
/** grunt-ts setting to emit events in Grunt */
85+
emitGruntEvents: boolean;
8486
}
8587

8688
interface IVisualStudioProjectSupport {

tasks/modules/optionsResolver.js

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tasks/modules/optionsResolver.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const propertiesFromTarget = ['amdloader', 'html', 'htmlOutDir', 'htmlOutDirFlat
1717
'htmlOutputTemplate', 'htmlOutDirFlatten', 'htmlVarTemplate', 'inlineSourceMap', 'inlineSources', 'isolatedModules',
1818
'mapRoot', 'module', 'newLine', 'noEmit', 'noEmitHelpers', 'noEmitOnError', 'noImplicitAny', 'noResolve',
1919
'preserveConstEnums', 'removeComments', 'sourceRoot', 'sourceMap', 'suppressImplicitAnyIndexErrors', 'target',
20-
'verbose', 'jsx', 'moduleResolution', 'experimentalAsyncFunctions', 'rootDir'],
20+
'verbose', 'jsx', 'moduleResolution', 'experimentalAsyncFunctions', 'rootDir', 'emitGruntEvents'],
2121
delayTemplateExpansion = ['htmlModuleTemplate', 'htmlVarTemplate', 'htmlOutputTemplate'];
2222

2323
let templateProcessor: (templateString: string, options: any) => string = null;
@@ -415,5 +415,9 @@ function applyGruntTSDefaults(options: IGruntTSOptions) {
415415
options.removeComments = GruntTSDefaults.removeComments;
416416
}
417417

418+
if (!('failOnTypeErrors' in options)) {
419+
options.failOnTypeErrors = GruntTSDefaults.failOnTypeErrors;
420+
}
421+
418422
return options;
419423
}

tasks/ts.js

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

tasks/ts.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import * as templateCacheModule from './modules/templateCache';
2121
import * as transformers from './modules/transformers';
2222
import * as optionsResolver from '../tasks/modules/optionsResolver';
2323
const {asyncSeries, timeIt} = utils;
24+
const fail_event = 'grunt-ts.failure';
2425

2526
function pluginFn(grunt: IGrunt) {
2627

@@ -60,6 +61,9 @@ function pluginFn(grunt: IGrunt) {
6061
});
6162

6263
if (options.errors.length > 0) {
64+
if (options.emitGruntEvents) {
65+
grunt.event.emit(fail_event);
66+
}
6367
done(false);
6468
return;
6569
}
@@ -221,7 +225,6 @@ function pluginFn(grunt: IGrunt) {
221225
}
222226

223227
grunt.log.writeln('');
224-
225228
if (isOnlyTypeErrors && !options.failOnTypeErrors) {
226229
grunt.log.write(('>> ').green);
227230
grunt.log.writeln('Type errors only.');
@@ -253,6 +256,9 @@ function pluginFn(grunt: IGrunt) {
253256
return isSuccessfulBuild;
254257
}).catch(function(err) {
255258
grunt.log.writeln(('Error: ' + err).red);
259+
if (options.emitGruntEvents) {
260+
grunt.event.emit(fail_event);
261+
}
256262
return false;
257263
});
258264
}
@@ -278,14 +284,6 @@ function pluginFn(grunt: IGrunt) {
278284
});
279285

280286
} else {
281-
// todo: fix this.
282-
// if (_.isArray(options.files)) {
283-
// filesToCompile = grunt.file.expand(files[filesCompilationIndex].src);
284-
// } else if (options.files[target.dest]) {
285-
// filesToCompile = grunt.file.expand(files[target.dest]);
286-
// } else {
287-
// filesToCompile = grunt.file.expand([(<{ src: string }><any>options.files).src]);
288-
// }
289287
filesCompilationIndex += 1;
290288
}
291289

@@ -451,6 +449,9 @@ function pluginFn(grunt: IGrunt) {
451449
if (res.some((success: boolean) => {
452450
return !success;
453451
})) {
452+
if (options.emitGruntEvents) {
453+
grunt.event.emit(fail_event);
454+
}
454455
done(false);
455456
}
456457
else {

0 commit comments

Comments
 (0)