forked from GoogleCloudPlatform/nodejs-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
142 lines (125 loc) · 4.58 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
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require('source-map-support').install();
const async = require('async');
const mocha = require('gulp-mocha');
const clangFormat = require('clang-format');
const del = require('del');
const format = require('gulp-clang-format');
const fs = require('fs');
const gulp = require('gulp');
const merge = require('merge2');
const path = require('path');
const sourcemaps = require('gulp-sourcemaps');
const spawn = require('child_process').spawn;
const ts = require('gulp-typescript');
const tslint = require('gulp-tslint');
const outDir = 'build';
const sources = [ 'src/**/*.ts' ];
const tests = [ 'test/**/*.ts' ];
const allFiles = [ '*.js' ].concat(sources, tests);
let exitOnError = true;
function onError() {
if (exitOnError) {
process.exit(1);
}
}
gulp.task('test.check-format', () => {
return gulp.src(allFiles)
.pipe(format.checkFormat('file', clangFormat))
.on('warning', onError);
});
gulp.task('format', () => {
return gulp.src(allFiles, {base : '.'})
.pipe(format.format('file', clangFormat))
.pipe(gulp.dest('.'));
});
gulp.task('test.check-lint', () => {
return gulp.src(allFiles)
.pipe(tslint({formatter : 'verbose'}))
.pipe(tslint.report())
.on('warning', onError);
});
gulp.task('clean', () => { return del([ `${outDir}` ]); });
gulp.task('compile', () => {
const tsResult = gulp.src(sources)
.pipe(sourcemaps.init())
.pipe(ts.createProject('tsconfig.json')())
.on('error', onError);
return merge([
tsResult.dts.pipe(gulp.dest(`${outDir}/definitions`)),
tsResult.js
.pipe(sourcemaps.write(
'.', {includeContent : false, sourceRoot : '../../src'}))
.pipe(gulp.dest(`${outDir}/src`)),
tsResult.js.pipe(gulp.dest(`${outDir}/src`))
]);
});
gulp.task('test.compile', [ 'compile' ], () => {
return gulp.src(tests, {base : '.'})
.pipe(sourcemaps.init())
.pipe(ts.createProject('tsconfig.json')())
.on('error', onError)
.pipe(
sourcemaps.write('.', {includeContent : false, sourceRoot : '../..'}))
.pipe(gulp.dest(`${outDir}/`));
});
function dockerBuild(tag, baseDir, cb) {
spawn('docker', [ 'build', '-t', tag, baseDir ], {
stdio : 'inherit'
}).on('close', cb);
}
gulp.task('test.prepare',
[ 'test.compile', 'test.check-format', 'test.check-lint' ], cb => {
const baseDir = __dirname;
const testDir = path.join(baseDir, 'test', 'definitions');
dockerBuild('test/nodejs', baseDir, () => {
fs.readdir(testDir, 'utf8', (err, items) => {
if (err) {
return cb(err);
}
const buildDirs =
items
.filter(pathname => {
// TODO: When the infrastructure is available, update
// the tests so that integration testing is
// also done.
return pathname !== 'integration' &&
fs.lstatSync(path.join(testDir, pathname))
.isDirectory();
})
.map(pathname => {
return (cb) => {
dockerBuild(`test/definitions/${pathname}`,
path.join(testDir, pathname), cb);
};
});
async.series(buildDirs, (err) => { cb(err); });
});
});
});
gulp.task('test.unit', [ 'test.prepare' ], () => {
return gulp.src([ `${outDir}/test/**/*.js` ])
.pipe(mocha({reporter : 'spec', timeout : 25000}));
});
gulp.task('watch', () => {
exitOnError = false;
gulp.start([ 'test.compile' ]);
// TODO: also run unit tests in a non-fatal way
return gulp.watch(allFiles, [ 'test.compile' ]);
});
gulp.task('test', [ 'test.unit' ]);
gulp.task('default', [ 'compile' ]);