forked from ivogabe/gulp-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
194 lines (166 loc) · 4.79 KB
/
gulpfile.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
var gulp = require('gulp');
var rimraf = require('rimraf');
var fs = require('fs');
var path = require('path');
var mergeStream = require('merge-stream');
var ts = require('./release/main');
var plumber = require('gulp-plumber');
var sourcemaps = require('gulp-sourcemaps');
var concat = require('gulp-concat');
var header = require('gulp-header');
var diff = require('gulp-diff');
var tsVersions = {
dev: './typescript/dev',
release23: './typescript/2.3',
};
function findTSDefinition(location) {
return path.join(path.dirname(require.resolve(location)), 'typescript.d.ts');
}
function createProject() {
return ts.createProject('lib/tsconfig.json');
}
var tsProject = createProject();
var paths = {
scripts: ['lib/**.ts'],
definitionTypeScript: [findTSDefinition('typescript')],
releaseBeta: 'release-2',
release: 'release'
};
var tests = fs.readdirSync(path.join(__dirname, 'test')).filter(function(dir) {
return dir !== 'baselines' && dir !== 'output' && dir.substr(0, 1) !== '.';
});
// Clean
gulp.task('clean', function(cb) {
rimraf(paths.releaseBeta, cb);
});
gulp.task('clean-test', function(cb) {
rimraf('test/output', cb);
});
gulp.task('clean-release', function(cb) {
rimraf(paths.release, cb);
});
// Compile sources
gulp.task('scripts', ['clean'], function() {
var tsResult = gulp.src(paths.scripts.concat(paths.definitionTypeScript))
.pipe(tsProject());
return mergeStream(tsResult.js, tsResult.dts)
.pipe(gulp.dest(paths.releaseBeta));
});
// Type checking against multiple versions of TypeScript
gulp.task('typecheck-dev', function() {
return gulp.src(paths.scripts.concat([
'!definitions/typescript.d.ts',
findTSDefinition(tsVersions.dev)
])).pipe(createProject()());
});
gulp.task('typecheck-2.3', function() {
return gulp.src(paths.scripts.concat([
'!definitions/typescript.d.ts',
findTSDefinition(tsVersions.release23)
])).pipe(createProject()());
});
gulp.task('typecheck', ['typecheck-dev', 'typecheck-2.3']);
// Tests
// We run every test on multiple typescript versions:
var libs = [
['2.4', undefined],
['2.3', require(tsVersions.release23)],
['dev', require(tsVersions.dev)]
];
// helper function for running a test.
function runTest(name, callback) {
var newTS = require('./release-2/main');
var test = require('./test/' + name + '/gulptask.js');
var done = 0;
fs.mkdirSync('test/output/' + name);
for (var i = 0; i < libs.length; i++) {
(function(i) {
var lib = libs[i];
var output = 'test/output/' + name + '/' + lib[0] + '/';
var errors = [];
var finishInfo;
var reporter = {
error: function(err) {
(function() {
if (path.sep === '\\') { //filenames embedded in error output contain OS-dependent path separators
var colon = err.message.indexOf(":");
if (colon === -1 || !err.diagnostic || err.message.indexOf(path.sep) === -1) {
return;
}
var fileName = err.message.slice(0, colon);
var detail = err.message.slice(colon);
fileName = fileName.replace(/\\/g, '/');
err.message = fileName + detail;
}
})();
errors.push(err);
},
finish: function(info) {
finishInfo = info;
}
};
fs.mkdirSync(output);
test(newTS, lib[1], output, reporter).on('finish', function() {
fs.writeFileSync(output + 'errors.txt', errors.join('\n') + '\n' + JSON.stringify(finishInfo, null, 4));
done++;
callback();
});
})(i);
}
}
gulp.task('test-run', ['clean-test', 'scripts'], function(cb) {
fs.mkdirSync('test/output/');
var pending = tests.length * libs.length;
if (pending === 0) {
cb();
return;
}
var isFailed = false;
for (var i = 0; i < tests.length; i++) {
runTest(tests[i], function(failed) {
isFailed = isFailed || failed;
pending--;
if (pending === 0) {
if (isFailed) {
cb(new Error('Tests failed'));
} else {
cb();
}
}
// This allows catching possible counting errors.
else if (pending < 0) {
throw new Error('Callback called more than expected!');
}
});
}
})
gulp.task('test', ['test-run'], function(cb) {
var failed = false;
function onError(error) {
failed = true;
}
return gulp.src('test/output/**/*.*')
.pipe(plumber())
.pipe(diff('test/baselines/'))
.on('error', onError)
.pipe(diff.reporter({ fail: true }))
.on('error', onError)
.on('finish', function() {
if (failed) {
throw new Error('Tests failed');
}
});
});
// Accept new baselines
gulp.task('test-baselines-accept', function(cb) {
rimraf('test/baselines', function() {
gulp.src('test/output/**').pipe(gulp.dest('test/baselines')).on('finish', cb);
});
});
gulp.task('release', function() {
return gulp.src(paths.releaseBeta + '/**').pipe(gulp.dest(paths.release));
});
gulp.task('watch', ['scripts'], function() {
gulp.watch(paths.scripts, ['scripts']);
});
gulp.task('default', ['scripts', 'typecheck', 'test']);