-
Notifications
You must be signed in to change notification settings - Fork 122
/
ts.ts
482 lines (402 loc) · 21.7 KB
/
ts.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/// <reference path="../defs/tsd.d.ts"/>
/// <reference path="./modules/interfaces.d.ts"/>
'use strict';
/*
* grunt-ts
* Licensed under the MIT license.
*/
import * as _ from 'lodash';
import * as path from 'path';
import * as fs from 'fs';
import {Promise} from 'es6-promise';
import * as utils from './modules/utils';
import * as compileModule from './modules/compile';
import * as referenceModule from './modules/reference';
import * as amdLoaderModule from './modules/amdLoader';
import * as html2tsModule from './modules/html2ts';
import * as templateCacheModule from './modules/templateCache';
import * as transformers from './modules/transformers';
import * as optionsResolver from '../tasks/modules/optionsResolver';
const {asyncSeries, timeIt} = utils;
const fail_event = 'grunt-ts.failure';
const pluginFn = function (grunt: IGrunt) {
/////////////////////////////////////////////////////////////////////
// The grunt task
////////////////////////////////////////////////////////////////////
grunt.registerMultiTask('ts', 'Compile TypeScript files', function () {
// tracks which index in the task "files" property is next for processing
let filesCompilationIndex = 0;
let done: grunt.task.AsyncResultCatcher,
options: Partial<IGruntTSOptions>;
{
const currentGruntTask: grunt.task.IMultiTask<ITargetOptions> = this;
const resolvedFiles: IGruntTSCompilationInfo[] = currentGruntTask.files;
// make async
done = currentGruntTask.async();
// get unprocessed templates from configuration
let rawTaskConfig =
<ITargetOptions>(grunt.config.getRaw(currentGruntTask.name) || {});
let rawTargetConfig =
<ITargetOptions>(grunt.config.getRaw(currentGruntTask.name + '.' + currentGruntTask.target) || {});
optionsResolver.resolveAsync(rawTaskConfig, rawTargetConfig, currentGruntTask.target, resolvedFiles,
grunt.template.process, grunt.file.expand, grunt.log.verbose.writeln).then((result) => {
options = result;
options.warnings.forEach((warning) => {
grunt.log.writeln(warning.magenta);
});
options.errors.forEach((error) => {
grunt.log.writeln(error.red);
});
if (options.errors.length > 0) {
if (options.emitGruntEvents) {
grunt.event.emit(fail_event);
}
done(false);
return;
}
proceed();
}).catch((error) => {
grunt.log.writeln((error + '').red);
done(false);
});
}
function proceed() {
var srcFromVS_RelativePathsFromGruntFile: string[] = [];
// Run compiler
asyncSeries(options.CompilationTasks, (currentFiles) => {
// Create a reference file?
var reference = processIndividualTemplate(options.reference);
var referenceFile;
var referencePath;
if (!!reference) {
referenceFile = path.resolve(reference);
referencePath = path.dirname(referenceFile);
}
function isReferenceFile(filename: string) {
return path.resolve(filename) === referenceFile;
}
// Create an output file?
var outFile = currentFiles.out;
var outFile_d_ts: string;
if (!!outFile) {
outFile = path.resolve(outFile);
outFile_d_ts = outFile.replace('.js', '.d.ts');
}
function isOutFile(filename: string): boolean {
return path.resolve(filename) === outFile_d_ts;
}
// see https://github.com/grunt-ts/grunt-ts/issues/77
function isBaseDirFile(filename: string, targetFiles: string[]) {
var baseDirFile: string = '.baseDir.ts';
var bd = options.baseDir || utils.findCommonPath(targetFiles, '/');
return path.resolve(filename) === path.resolve(path.join(bd, baseDirFile));
}
// Create an amd loader?
let amdloader = options.amdloader;
let amdloaderFile: string, amdloaderPath: string;
if (!!amdloader) {
amdloaderFile = path.resolve(amdloader);
amdloaderPath = path.dirname(amdloaderFile);
}
// Compiles all the files
// Uses the blind tsc compile task
// logs errors
function runCompilation(options: Partial<IGruntTSOptions>, compilationInfo: IGruntTSCompilationInfo): Promise<boolean> {
grunt.log.writeln('Compiling...'.yellow);
// Time the compiler process
var starttime = new Date().getTime();
var endtime;
// Compile the files
return compileModule.compileAllFiles(options, compilationInfo)
.then((result: ICompileResult) => {
// End the timer
endtime = new Date().getTime();
grunt.log.writeln('');
// Analyze the results of our tsc execution,
// then tell the user our analysis results
// and mark the build as fail or success
if (!result) {
grunt.log.error('Error: No result from tsc.'.red);
return false;
}
if (result.code === 8) {
grunt.log.error('Error: Node was unable to run tsc. Possibly it could not be found?'.red);
return false;
}
// In TypeScript 1.3 and above, the result code corresponds to the ExitCode enum in
// TypeScript/src/compiler/sys.ts
var isError = (result.code !== 0);
// If the compilation errors contain only type errors, JS files are still
// generated. If tsc finds type errors, it will return an error code, even
// if JS files are generated. We should check this for this,
// only type errors, and call this a successful compilation.
// Assumptions:
// Level 1 errors = syntax errors - prevent JS emit.
// Level 2 errors = semantic errors - *not* prevents JS emit.
// Level 5 errors = compiler flag misuse - prevents JS emit.
var level1ErrorCount = 0, level5ErrorCount = 0, nonEmitPreventingWarningCount = 0;
var hasTS7017Error = false;
var hasPreventEmitErrors = _.reduce(result.output.split('\n'), (memo, errorMsg) => {
var isPreventEmitError = false;
if (errorMsg.search(/error TS7017:/g) >= 0) {
hasTS7017Error = true;
}
if (errorMsg.search(/error TS1\d+:/g) >= 0) {
level1ErrorCount += 1;
isPreventEmitError = true;
} else if (errorMsg.search(/error TS5\d+:/) >= 0) {
level5ErrorCount += 1;
isPreventEmitError = true;
} else if (errorMsg.search(/error TS\d+:/) >= 0) {
nonEmitPreventingWarningCount += 1;
}
return memo || isPreventEmitError;
}, false) || false;
// Because we can't think of a better way to determine it,
// assume that emitted JS in spite of error codes implies type-only errors.
var isOnlyTypeErrors = !hasPreventEmitErrors;
if (hasTS7017Error) {
grunt.log.writeln(('Note: You may wish to enable the suppressImplicitAnyIndexErrors' +
' grunt-ts option to allow dynamic property access by index. This will' +
' suppress TypeScript error TS7017.').magenta);
}
// Log error summary
if (level1ErrorCount + level5ErrorCount + nonEmitPreventingWarningCount > 0) {
if ((level1ErrorCount + level5ErrorCount > 0) || options.failOnTypeErrors) {
grunt.log.write(('>> ').red);
} else {
grunt.log.write(('>> ').green);
}
if (level5ErrorCount > 0) {
grunt.log.write(level5ErrorCount.toString() + ' compiler flag error' +
(level5ErrorCount === 1 ? '' : 's') + ' ');
}
if (level1ErrorCount > 0) {
grunt.log.write(level1ErrorCount.toString() + ' syntax error' +
(level1ErrorCount === 1 ? '' : 's') + ' ');
}
if (nonEmitPreventingWarningCount > 0) {
grunt.log.write(nonEmitPreventingWarningCount.toString() +
' non-emit-preventing type warning' +
(nonEmitPreventingWarningCount === 1 ? '' : 's') + ' ');
}
grunt.log.writeln('');
if (isOnlyTypeErrors && !options.failOnTypeErrors) {
grunt.log.write(('>> ').green);
grunt.log.writeln('Type errors only.');
}
}
// !!! To do: To really be confident that the build was actually successful,
// we have to check timestamps of the generated files in the destination.
var isSuccessfulBuild = (!isError ||
(isError && isOnlyTypeErrors && !options.failOnTypeErrors)
);
if (isSuccessfulBuild) {
// Report successful build.
let time = (endtime - starttime) / 1000;
grunt.log.writeln('');
let message = 'TypeScript compilation complete: ' + time.toFixed(2) + 's';
if (utils.shouldPassThrough(options)) {
message += ' for TypeScript pass-through.';
} else {
message += ' for ' + result.fileCount + ' TypeScript files.';
}
grunt.log.writeln(message.green);
} else {
// Report unsuccessful build.
grunt.log.error(('Error: tsc return code: ' + result.code).yellow);
}
return isSuccessfulBuild;
}).catch(function(err) {
grunt.log.writeln(('Error: ' + err).red);
if (options.emitGruntEvents) {
grunt.event.emit(fail_event);
}
return false;
});
}
// Find out which files to compile, codegen etc.
// Then calls the appropriate functions + compile function on those files
function filterFilesTransformAndCompile(): Promise<boolean> {
var filesToCompile: string[] = [];
if (currentFiles.src || options.vs) {
_.map(currentFiles.src, (file) => {
if (filesToCompile.indexOf(file) === -1) {
filesToCompile.push(file);
}
});
_.map(srcFromVS_RelativePathsFromGruntFile, (file) => {
if (filesToCompile.indexOf(file) === -1) {
filesToCompile.push(file);
}
});
} else {
filesCompilationIndex += 1;
}
// ignore directories, and clear the files of output.d.ts and baseDirFile
filesToCompile = filesToCompile.filter((file) => {
var stats = fs.lstatSync(file);
return !stats.isDirectory() && !isOutFile(file) && !isBaseDirFile(file, filesToCompile);
});
///// Html files:
// Note:
// compile html files must be before reference file creation
var generatedFiles = [];
if (options.html) {
let html2tsOptions : html2tsModule.IHtml2TSOptions = {
moduleFunction: _.template(options.htmlModuleTemplate),
varFunction: _.template(options.htmlVarTemplate),
htmlOutputTemplate: options.htmlOutputTemplate,
htmlOutDir: options.htmlOutDir,
flatten: options.htmlOutDirFlatten,
eol: (options.newLine || utils.eol)
};
let htmlFiles = grunt.file.expand(options.html);
generatedFiles = _.map(htmlFiles, (filename) => html2tsModule.compileHTML(filename, html2tsOptions));
generatedFiles.forEach((fileName) => {
if (filesToCompile.indexOf(fileName) === -1 &&
grunt.file.isMatch(currentFiles.glob, fileName)) {
filesToCompile.push(fileName);
}
});
}
///// Template cache
// Note: The template cache files do not go into generated files.
// Note: You are free to generate a `ts OR js` file for template cache, both should just work
if (options.templateCache) {
if (!options.templateCache.src || !options.templateCache.dest || !options.templateCache.baseUrl) {
grunt.log.writeln('templateCache : src, dest, baseUrl must be specified if templateCache option is used'.red);
}
else {
let templateCacheSrc = grunt.file.expand(options.templateCache.src); // manual reinterpolation
let templateCacheDest = path.resolve(options.templateCache.dest);
let templateCacheBasePath = path.resolve(options.templateCache.baseUrl);
templateCacheModule.generateTemplateCache(templateCacheSrc,
templateCacheDest, templateCacheBasePath, (options.newLine || utils.eol));
}
}
///// Reference File
// Generate the reference file
// Create a reference file if specified
if (!!referencePath) {
var result = timeIt(() => {
return referenceModule.updateReferenceFile(
filesToCompile.filter(f => !isReferenceFile(f)),
generatedFiles,
referenceFile,
referencePath,
(options.newLine || utils.eol));
});
if (result.it === true) {
grunt.log.writeln(('Updated reference file (' + result.time + 'ms).').green);
}
}
///// AMD loader
// Create the amdLoader if specified
if (!!amdloaderPath) {
var referenceOrder: amdLoaderModule.IReferences
= amdLoaderModule.getReferencesInOrder(referenceFile, referencePath, generatedFiles);
amdLoaderModule.updateAmdLoader(referenceFile, referenceOrder, amdloaderFile, amdloaderPath, currentFiles.outDir);
}
// Transform files as needed. Currently all of this logic in is one module
transformers.transformFiles(filesToCompile /*TODO: only unchanged files*/,
filesToCompile, options);
currentFiles.src = filesToCompile;
// Return promise to compliation
if (utils.shouldCompile(options)) {
if (filesToCompile.length > 0 || options.testExecute || utils.shouldPassThrough(options)) {
return runCompilation(options, currentFiles).then((success: boolean) => {
return success;
});
}
else {
// Nothing to do
grunt.log.writeln('No files to compile'.red);
return Promise.resolve(true);
}
}
else { // Nothing to do
return Promise.resolve(true);
}
}
// Time (in ms) when last compile took place
var lastCompile = 0;
// Watch a folder?
if (!!options.watch) {
// get path(s)
var watchpath = grunt.file.expand([options.watch]);
// create a file watcher for path
var chokidar = require('chokidar');
var watcher = chokidar.watch(watchpath, { ignoreInitial: true, persistent: true });
// Log what we are doing
grunt.log.writeln(('Watching all TypeScript / Html files under : ' + watchpath).cyan);
// A file has been added/changed/deleted has occurred
watcher
.on('add', function (path) {
handleFileEvent(path, '+++ added ');
// Reset the time for last compile call
lastCompile = new Date().getTime();
})
.on('change', function (path) {
handleFileEvent(path, '### changed ');
// Reset the time for last compile call
lastCompile = new Date().getTime();
})
.on('unlink', function (path) {
handleFileEvent(path, '--- removed ');
// Reset the time for last compile call
lastCompile = new Date().getTime();
})
.on('error', function (error) {
console.error('Error happened in chokidar: ', error);
});
}
// Reset the time for last compile call
lastCompile = new Date().getTime();
// Run initial compile
return filterFilesTransformAndCompile();
// local event to handle file event
function handleFileEvent(filepath: string, displaystr: string) {
const acceptedExtentions = ['.ts', '.tsx', '.js', '.jsx', '.html'];
acceptedExtentions.forEach(
(extension) => {
// If extension is accepted and was not just run
if (utils.endsWith(filepath.toLowerCase(), extension) && (new Date().getTime() - lastCompile) > 100) {
// Log and run the debounced version.
grunt.log.writeln((displaystr + ' >>' + filepath).yellow);
filterFilesTransformAndCompile();
return;
}
// Uncomment for debugging which files were ignored
// else if ((new Date().getTime() - lastCompile) <= 100){
// grunt.log.writeln((' ///' + ' >>' + filepath).grey);
// }
}
);
}
}).then((res: boolean[]) => {
// Ignore res? (either logs or throws)
if (!options.watch) {
if (res.some((success: boolean) => {
return !success;
})) {
if (options.emitGruntEvents) {
grunt.event.emit(fail_event);
}
done(false);
}
else {
done();
}
}
}, done);
}
});
function processIndividualTemplate(template: string) {
if (template) {
return grunt.template.process(template, {});
}
return template;
}
};
export = pluginFn;