-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgulpfile.js
More file actions
164 lines (137 loc) · 4.08 KB
/
Copy pathgulpfile.js
File metadata and controls
164 lines (137 loc) · 4.08 KB
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
'use strict';
var fs = require('fs');
var pathExists = require('path-exists');
var gulp = require('gulp');
var del = require('del');
var runSequence = require('run-sequence');
var merge = require('merge-stream');
var plugins = require('gulp-load-plugins')();
var spawn = require('child_process').spawn;
var open = require('open');
var paths = {
gulpfile: 'gulpfile.js',
src: 'src/**/*.ts',
test: 'test/{src,integration}/**/*_test.ts',
dest: 'lib/',
testDest: '.tmp/',
typescriptFiles: ['{src,test}/**/*.ts', '!test/fixtures/**/*.ts'],
generatedTestFiles: '.tmp/generated/**',
testFileDir: 'test/fixtures/generated',
};
var tsProject = plugins.typescript.createProject('tsconfig.json');
var mochaOptions = {
reporter: 'spec',
timeout: 30000
};
var tslintOptions = {
formatter: 'verbose'
};
gulp.task('jshint', function() {
return gulp.src(paths.gulpfile)
.pipe(plugins.jshint())
.pipe(plugins.jshint.reporter('default'))
.pipe(plugins.jshint.reporter('fail'));
});
gulp.task('tslint', function() {
return gulp.src(paths.typescriptFiles)
.pipe(plugins.tslint(tslintOptions))
.pipe(plugins.tslint.report());
});
gulp.task('test', ['clean:testDest'], function(callback) {
test(false, false, callback);
});
gulp.task('test:watch', function(callback) {
test(true, false, callback);
});
gulp.task('test:watch:debug', function(callback) {
test(true, true, callback);
});
gulp.task('clean:dest', function() {
return del(paths.dest);
});
gulp.task('clean:testDest', function() {
return del(paths.testDest);
});
gulp.task('compile', ['clean:dest'], function(){
var tsStream = gulp.src(paths.src)
.pipe(tsProject(plugins.typescript.reporter.defaultReporter()));
var jsStream = tsStream.js
.pipe(gulp.dest(paths.dest));
var dtsStream = tsStream.dts
.pipe(gulp.dest(paths.dest));
return merge(jsStream, dtsStream);
});
gulp.task('build', function(callback) {
runSequence(['jshint', 'tslint', 'test'], 'compile', function(err) {
if (err) {
process.exit(1);
} else {
callback();
}
});
});
gulp.task('default', ['build']);
gulp.task('watch', function() {
gulp.watch([paths.src, paths.test], ['test:watch']);
});
gulp.task('watch:debug', function() {
spawn('node', ['node_modules/node-inspector/bin/inspector.js']);
gulp.watch([paths.src, paths.test], ['test:watch:debug']);
});
gulp.task('updateTestFiles', function() {
return gulp.src(paths.generatedTestFiles)
.pipe(plugins.replace(/typings\/integration/g, 'test/fixtures/typings/integration'))
.pipe(gulp.dest(paths.testFileDir));
});
function test(watching, debug, callback) {
mochaOptions.debug = mochaOptions.debugBrk = debug;
var isCompleted = false;
gulp.src(paths.typescriptFiles)
.pipe(plugins.changed(paths.testDest, {
extension: '.js',
hasChanged: hasChangedForTest
}))
.pipe(plugins.sourcemaps.init())
.pipe(tsProject(plugins.typescript.reporter.defaultReporter())).js
.pipe(plugins.espower())
.pipe(plugins.sourcemaps.write())
.pipe(gulp.dest(paths.testDest))
.on('end', function() {
if (debug) { open('http://127.0.0.1:8080/debug?port=5858'); }
})
.pipe(plugins.spawnMocha(mochaOptions))
.on('error', function() {
if (watching) {
this.emit('end');
} else {
process.exit(1);
}
})
.on('end', function() {
if (!isCompleted) {
callback();
isCompleted = true;
}
});
}
function hasChangedForTest(stream, callback, sourceFile, destPath) {
if (!pathExists.sync(destPath)) {
stream.push(sourceFile);
return callback();
}
var destStat = fs.statSync(destPath);
if (sourceFile.stat.mtime > destStat.mtime) {
stream.push(sourceFile);
} else if (/_test.ts$/.test(sourceFile.path)) {
var testTargetPath = sourceFile.path
.replace(/_test.ts$/, '.ts')
.replace(process.cwd() + '/test', process.cwd());
if (pathExists.sync(testTargetPath)) {
var testTargetStat = fs.statSync(testTargetPath);
if (testTargetStat.mtime > destStat.mtime) {
stream.push(sourceFile);
}
}
}
callback();
}