forked from yavin5/angular-async-http
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.js
252 lines (225 loc) · 6.42 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
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
'use strict';
var gulp = require('gulp');
var gutil = require('gulp-util');
var htmlExtender = require('gulp-html-extend');
var del = require('del');
var typescript = require('gulp-typescript');
var async = require('async');
var merge = require('merge2');
var replace = require('gulp-replace');
var fs = require('fs');
var Gaze = require('gaze').Gaze;
var mocha = require('gulp-mocha');
var istanbul = require('gulp-istanbul');
var vinylPaths = require('vinyl-paths');
var tsConfig = require('./src/tsconfig.json');
var settings = {
distFolder: 'dist',
projectName: 'angular-async-http',
tsConfig: tsConfig
};
gulp.task('default', ['prepublish'], function (cb) {
watch(cb);
});
gulp.task('test', function (cb) {
test(cb);
});
gulp.task('prepublish', function (cb) {
prepublish(cb);
});
// ------------------- TASKS ---------------------
function prepublish(cb) {
async.series([
function (next) {
_clean(next);
},
function (next) {
_build(next);
},
function (next) {
_prepublishPackage(next);
}
], cb);
}
function _clean(cb) {
gulp.src(settings.distFolder + '/*', { read: false })
.pipe(vinylPaths(del))
.on('finish', function () {
cb();
});
}
function _build(cb) {
async.series([
function (next) {
_deployMisc(next);
},
function (next) {
_compileTypescript(next);
}
], cb);
}
function _deployMisc(cb) {
gutil.log('Starting ', gutil.colors.cyan('_deployMisc'));
gulp.src(['src/test/*.js']).pipe(gulp.dest(settings.distFolder + '/test'));
gulp.src(['package.json', 'LICENSE', 'README.md', 'src/systemjs.config.js', 'src/build.js'])
.pipe(htmlExtender({ annotations: false, verbose: false }))
.pipe(gulp.dest(settings.distFolder))
.on('finish', function () {
gutil.log('Finished ', gutil.colors.cyan('_deployMisc'));
cb();
});
}
function _compileTypescript(cb) {
_compileTypescriptFromSrc(['src/**/*.ts', '!src/**/*spec.ts', '!src/config/**/*.ts'], cb);
}
function watch(cb) {
gutil.log('Starting ', gutil.colors.cyan('_watch'));
_watchTypescript();
gulp.watch(['package.json', 'LICENSE', 'README.md', 'src/test/*.js'], ['_deployMisc']);
gulp.watch(['src/**/*.spec.ts'], ['_compileSpecs']);
gulp.watch(['src/**/*e2e*.ts'], ['_compileE2e']);
gutil.log(gutil.colors.cyan('Watchers started'));
cb();
}
function _watchTypescript() {
gutil.log('Watching', gutil.colors.cyan('Typescript'));
var gaze = new Gaze(['src/**/*.ts', 'src/**/*.html', '!src/**/*spec.ts']);
gaze.on('all', function (event, filepath) {
_compileChangedTypescript(event, filepath);
});
gaze.on('error', function (error) {
gaze.close();
gutil.log('error occured during', gutil.colors.cyan('typescript'), 'watch. Compiling and re-watching', gutil.colors.cyan('typescript'), 'in a few seconds...');
setTimeout(function () {
_compileTypescript(function () {
_watchTypescript();
});
}, 3000);
});
}
function _compileChangedTypescript(event, src) {
if (event == 'changed') {
src = src.substring(src.lastIndexOf("/"));
src = src.substring(src.lastIndexOf("\\"));
_compileTypescriptFromSrc(["src/**" + src], function () {
}, true);
}
else if (event == 'added') {
_compileTypescript(function () {
});
}
else if (event == 'deleted') {
var jsDistFileLoc = src.replace("\\src\\", '\\' + settings.distFolder + '\\').replace('.ts', '.js');
var dTsDistFileLoc = src.replace("\\src\\", '\\' + settings.distFolder + '\\').replace('.ts', '.d.ts');
gutil.log("Deleting", gutil.colors.magenta(jsDistFileLoc));
return gulp.src([jsDistFileLoc, dTsDistFileLoc], {read: false})
.pipe(rimraf());
}
}
function _compileTypescriptFromSrc(srcArray, cb) {
gutil.log("Starting", gutil.colors.cyan('_compileTypescriptFromSrc'), "for file(s)", gutil.colors.magenta(srcArray));
var compilerOptions = settings.tsConfig.compilerOptions;
//if we changed 1 file its not needed to compile the whole source code
if (srcArray.length == 1 && srcArray[0].slice(-2) == "ts") {
compilerOptions['isolatedModules'] = true;
}
var tsProject = typescript.createProject(compilerOptions);
var tsResult = gulp.src(srcArray)
.pipe(tsProject());
merge([
tsResult.dts.pipe(gulp.dest(settings.distFolder)),
tsResult.js.pipe(gulp.dest(settings.distFolder))
]).on('finish', function () {
gutil.log('Finished ', gutil.colors.cyan('_compileTypescriptFromSrc'));
cb();
});
}
/**
* Copy part of the package.json to the dist folder for publishing.
*/
function _prepublishPackage(cb) {
var json = JSON.parse(fs.readFileSync('./package.json'));
var publishJson = {
name: json.name,
version: json.version,
description: json.description,
typings: json.typings,
main: json.main,
repository: json.repository,
keywords: json.keywords,
author: json.author,
license: json.license,
bugs: json.bugs,
homepage: json.homepage,
publishConfig: json.publishConfig,
dependencies: json.dependencies
};
var src = require('stream').Readable({ objectMode: true });
src._read = function () {
this.push(new gutil.File({
cwd: "",
base: "",
path: "package.json",
contents: new Buffer(JSON.stringify(publishJson, null, 2))
}));
this.push(null)
};
src.pipe(gulp.dest(settings.distFolder + '/'))
.on('finish', function () {
cb();
});
}
function test(cb) {
async.series([
function (next) {
_clean(next);
},
function (next) {
_buildDeploy(next);
},
function (next) {
_compileSpecs(next);
},
function (next) {
_preTest(next);
},
function (next) {
_runMocha(next);
}
], cb);
};
function _buildDeploy(cb) {
async.series([
function (next) {
_deployMisc(next);
},
function (next) {
_compileTypescript(next);
}
], cb);
}
function _compileSpecs(cb) {
_compileTypescriptFromSrc(['src/**/*.spec.ts'], cb);
}
function _preTest(cb){
return gulp.src(['dist/**/*.js'])
// Covering files
.pipe(istanbul())
// Force `require` to return covered files
.pipe(istanbul.hookRequire())
.on('error', gutil.log)
.on('finish', function () {
cb();
});
}
function _runMocha(cb) {
return gulp.src(['dist/**/*.spec.js'], { read: false })
.pipe(mocha({ reporter: 'list' }))
.pipe(istanbul.writeReports({
// reporters: ['lcovonly']
}))
.on('error', gutil.log)
.on('finish', function () {
cb();
});
}