forked from TypeStrong/grunt-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
366 lines (340 loc) · 12.7 KB
/
Gruntfile.js
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
module.exports = function (grunt) {
'use strict';
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
clean: {
test: [
'test/**/*.js',
'test/**/*.js.map',
'test/**/*.html.ts',
'.tscache/**/*',
'!test/test.js',
'!test/expected/**/*'
]
},
jshint: {
options: grunt.util._.extend(grunt.file.readJSON('.jshintrc'), {
reporter: './node_modules/jshint-path-reporter'
}),
support: ['Gruntfile.js']
},
tslint: {
options: {
configuration: grunt.file.readJSON('tslint.json'),
formatter: 'tslint-path-formatter'
},
source: {
src: ['tasks/**/*.ts']
}
},
'ts-internal': {
options: {
target: 'es5',
module: 'commonjs',
comments: true,
sourcemap: true,
verbose: true,
fast: 'always'
},
build: {
src: ['tasks/**/*.ts']
},
test: {
src: ['test/test.ts']
}
},
nodeunit: {
tests: ['test/test.js']
},
watch: {
dev: {
files: ['**/*.ts', '!**/*.d.ts'],
tasks: ['run'],
}
},
ts: {
// Set the default options, see : http://gruntjs.com/configuring-tasks#options
options: {
// 'es3' (default) | 'es5'
target: 'es3',
// Use amd for asynchonous loading or commonjs 'amd' (default) | 'commonjs'
module: 'commonjs',
// Generate a source map file for each result js file (true (default) | false)
sourcemap: true,
// Generate a declaration .d.ts file for each resulting js file (true | false (default))
declaration: false,
// ??? (true | false (default))
nolib: false,
// Leave comments in compiled js code (true | false (default))
comments: false,
// Print the tsc command (true | false (default))
verbose: true
},
// A specific target
work: {
// The source typescript files, see : http://gruntjs.com/configuring-tasks#files
src: ['test/work/**/*.ts'],
// If specified, generate an out.js file which is the merged js file
out: 'test/work/out.js',
// Override the default options, see : http://gruntjs.com/configuring-tasks#options
options: {
sourcemap: true,
declaration: true,
},
},
simple: {
test: true,
options: {
sourcemap: true,
declaration: true
},
src: ['test/simple/ts/zoo.ts'],
outDir: 'test/simple/js/',
},
abtest: {
test: true,
src: ['test/abtest/**/*.ts'],
reference: 'test/abtest/reference.ts',
out: 'test/abtest/out.js',
},
amdloadersrc: {
test: true,
src: ['test/amdloader/ts/app/**/*.ts'],
html: ['test/amdloader/ts/app/**/*.html'],
reference: 'test/amdloader/ts/app/reference.ts',
outDir: 'test/amdloader/js/app',
amdloader: 'test/amdloader/js/app/loader.js',
// watch: 'test/amdloader/app'
},
amdloadertest: {
test: true,
src: ['test/amdloader/ts/test/**/*.ts'],
html: ['test/amdloader/ts/test/**/*.html'],
reference: 'test/amdloader/ts/test/reference.ts',
outDir: 'test/amdloader/js/test',
amdloader: 'test/amdloader/js/test/loader.js',
},
amdtest: {
test: true,
src: ['test/amdtest/**/*.ts'],
options: {
module: 'amd'
}
},
warnbothcomments: {
test: true,
src: ['test/abtest/**/*.ts'],
reference: 'test/abtest/reference.ts',
out: 'test/abtest/out.js',
options: {
// this should cause a warning
comments: true,
removeComments: false,
},
},
htmltest: {
test: true,
src: ['test/html/**/*.ts'],
html: ['test/html/**/*.tpl.html'],
reference: 'test/html/reference.ts',
out: 'test/html/out.js',
},
definitelyTypedTest: {
test: true,
src: ['test/definitelytypedtest/**/*.ts'],
html: ['test/definitelytypedtest/**/*.tpl.html'],
reference: 'test/definitelytypedtest/reference.ts',
out: 'test/definitelytypedtest/out.js',
},
nocompile: {
test: true,
src: ['test/nocompile/**/*.ts'],
reference: 'test/nocompile/reference.ts',
out: 'test/nocompile/out.js',
options: {
compile: false,
}
},
outdirtest: {
test: true,
src: ['test/outdirtest/**/*.ts'],
outDir: 'test/outdirtest/js',
},
sourceroottest: {
test: true,
src: ['test/sourceroot/src/**/*.ts'],
html: ['test/sourceroot/src/**/*.html'],
reference: 'test/sourceroot/src/reference.ts',
out: 'test/sourceroot/public/js/app.js',
options: {
sourceRoot: 'js',
mapRoot: 'map', // assuming we move all map files with some other grunt task
},
},
templatecache: {
test: true,
src: ['test/templatecache/**/*.ts'],
reference: 'test/templatecache/ts/reference.ts',
amdloader: 'test/templatecache/js/loader.js',
outDir: 'test/templatecache/js',
templateCache: {
baseUrl: 'test/templatecache/js/',
src: ['test/templatecache/js/**/*.html'],
dest: 'test/templatecache/js/templateCache.js',
},
},
transform: {
test: true,
src: ['test/transform/ts/**/*.ts'],
outDir: 'test/transform/js',
options: {
fast: 'always'
}
},
customcompiler: {
test: true,
src: ['test/customcompiler/ts/**/*.ts'],
outDir: 'test/customcompiler/js',
options: {
compiler: './customcompiler/tsc'
}
},
fail: {
fail: true, // a designed to fail target
src: ['test/fail/**/*.ts'],
outDir: 'test/fail/js',
baseDir: 'test/fail/ts',
// watch: 'test',
options: { // overide the main options for this target
sourcemap: false
}
},
}
});
// Helper to upgrade internal compiler task (fresh dogfood)
// Only do this when stable!
var lkgPath = './tasks-internal';
grunt.registerTask('upgrade', function () {
var done = this.async();
var ncp = require('ncp').ncp;
var rimraf = require('rimraf');
rimraf(lkgPath, function (err) {
if (!err) {
ncp('./tasks', lkgPath, function (err) {
if (!err) {
var next = grunt.file.read('./tasks/ts.js');
grunt.file.delete(lkgPath + '/ts.js');
// change `ts` to `ts-internal`
var pattern = 'grunt.registerMultiTask(\'ts\',';
var internal = 'grunt.registerMultiTask(\'ts-internal\',';
if (next.indexOf(pattern) < 0) {
grunt.fail.warn('can\'t find task declaration-pattern: ' + pattern);
done(false);
return;
}
next = next.replace(pattern, internal);
// Also add header to show which version was used
next = '// v' + grunt.config.get('pkg.version') +
' ' + new Date().toISOString() + '\r\n' + next;
grunt.file.write(lkgPath + '/ts-internal.js', next);
}
else {
console.log(err);
done(false);
}
});
}
else {
console.log(err);
done(false);
}
});
});
// Collect test tasks
grunt.registerTask('test_all', grunt.util._.reduce(grunt.config.get('ts'), function (memo, task, name) {
if (task.test) {
memo.push('ts:' + name);
}
return memo;
}, []));
// Collect test tasks
grunt.registerTask('test_fail', grunt.util._.reduce(grunt.config.get('ts'), function (memo, task, name) {
if (task.fail) {
memo.push('ts:' + name);
}
return memo;
}, []));
// Loading it for testing since we have in a local 'tasks' folder
grunt.loadTasks('tasks');
// in your configuration you would load this like:
//grunt.loadNpmTasks('grunt-ts')
grunt.loadTasks(lkgPath); // dogfood
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-tslint');
grunt.loadNpmTasks('grunt-continue');
grunt.loadNpmTasks('grunt-contrib-nodeunit');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-debug-task');
// Build
grunt.registerTask('prep', ['clean', 'jshint:support']);
grunt.registerTask('build', ['prep', 'ts-internal', 'tslint:source']);
// Test
grunt.registerTask('fail', ['continueOn', 'test_fail', 'continueOff']);
grunt.registerTask('test', ['test_all', 'fail', 'nodeunit']);
// Release
grunt.registerTask('release', ['build', 'test']);
grunt.registerTask('default', ['release']);
//////////////////////////////////////////////
// Dev
//
// `grunt dev` if using grunt watch
// Or
// `grunt run` if using webstorm / manual run
//
// Modify tasksToTest based on what you are working on
var tasksToTest = ['ts:fail', 'nodeunit'];
grunt.registerTask('dev', ['run', 'watch']);
grunt.registerTask('run', function () {
// Clear the console and move to 0 0
// http://stackoverflow.com/a/14976765/390330
console.log('\u001b[2J\u001b[0;0H');
console.log('>>>>>>>>>>> Cleared console >>>>>>>>>>> \n\n'.grey);
var done = this.async();
// Using a simple chain of ts:internal followed by ts:yourtest would not have run the updated grunt-ts
// We are spawn to ensure that `ts:` is reloaded after compile
function runTask(taskName, callback) {
grunt.util.spawn({
cmd: 'grunt',
args: [taskName]
}, function (err, output) {
if (err) {
console.log(output.stderr || output.stdout);
done(err);
}
else {
console.log(output.stdout);
console.log('\n'); // looks better
callback();
}
});
}
// Add build task
tasksToTest.unshift('ts-internal:build');
// Now execute
var currentIndex = 0;
function getNextTaskFunction() {
currentIndex++;
if (currentIndex === tasksToTest.length) {
return done;
}
else {
return function () {
runTask(tasksToTest[currentIndex], getNextTaskFunction());
};
}
}
runTask(tasksToTest[0], getNextTaskFunction());
});
//////////////////////////////////////////////
};