This repository has been archived by the owner on Feb 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
gulpfile.js
154 lines (137 loc) · 5.11 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
require('source-map-support').install();
var clangFormat = require('clang-format');
var formatter = require('gulp-clang-format');
var fs = require('fs');
var fsx = require('fs-extra');
var gulp = require('gulp');
var gutil = require('gulp-util');
var merge = require('merge2');
var mocha = require('gulp-mocha');
var path = require('path');
var sourcemaps = require('gulp-sourcemaps');
var spawn = require('child_process').spawn;
var ts = require('gulp-typescript');
var typescript = require('typescript');
var style = require('dart-style');
var which = require('which');
var tslint = require('gulp-tslint');
gulp.task('test.check-format', function() {
return gulp.src(['*.js', 'lib/**/*.ts', 'test/**/*.ts'])
.pipe(formatter.checkFormat('file', clangFormat))
.on('warning', onError);
});
gulp.task('test.check-lint', function() {
return gulp.src(['lib/**/*.ts', 'test/**/*.ts'])
.pipe(tslint())
.pipe(tslint.report('verbose'))
.on('warning', onError);
});
var hasError;
var failOnError = true;
var onError = function(err) {
hasError = true;
gutil.log(err.message);
if (failOnError) {
process.exit(1);
}
};
var tsProject =
ts.createProject('tsconfig.json', {noEmit: false, declaration: true, typescript: typescript});
gulp.task('compile', function() {
hasError = false;
var tsResult =
gulp.src(['lib/**/*.ts', 'typings/**/*.d.ts', 'node_modules/typescript/lib/typescript.d.ts'])
.pipe(sourcemaps.init())
.pipe(ts(tsProject))
.on('error', onError);
return merge([
tsResult.dts.pipe(gulp.dest('build/definitions')),
// Write external sourcemap next to the js file
tsResult.js.pipe(sourcemaps.write('.')).pipe(gulp.dest('build/lib')),
tsResult.js.pipe(gulp.dest('build/lib')),
]);
});
gulp.task('test.compile', ['compile'], function(done) {
if (hasError) {
done();
return;
}
return gulp
.src(
['test/*.ts', 'typings/**/*.d.ts', 'node_modules/dart-style/dart-style.d.ts'],
{base: '.'})
.pipe(sourcemaps.init())
.pipe(ts(tsProject))
.on('error', onError)
.js.pipe(sourcemaps.write())
.pipe(gulp.dest('build/')); // '/test/' comes from base above.
});
gulp.task('test.unit', ['test.compile'], function(done) {
if (hasError) {
done();
return;
}
return gulp.src('build/test/**/*.js').pipe(mocha({
timeout: 4000, // Needed by the type-based tests :-(
}));
});
// This test transpiles some unittests to dart and runs them in the Dart VM.
gulp.task('test.e2e', ['test.compile'], function(done) {
var testfile = 'helloworld';
// Removes backslashes from __dirname in Windows
var dir = (__dirname.replace(/\\/g, '/') + '/build/e2e');
if (fs.existsSync(dir)) fsx.removeSync(dir);
fs.mkdirSync(dir);
fsx.copySync(__dirname + '/test/e2e/pubspec.yaml', dir + '/pubspec.yaml');
// run node with a shell so we can wildcard all the .ts files
var cmd = 'node build/lib/main.js --translateBuiltins --tsconfig test/e2e/tsconfig.json ' +
'--generateLibraryName=true ' +
'test/e2e/*.ts';
// Paths must be relative to our source root, so run with cwd == dir.
spawn('sh', ['-c', cmd], {stdio: 'inherit'}).on('close', function(code, signal) {
if (code > 0) {
onError(new Error('Failed to transpile ' + testfile + '.ts'));
} else {
try {
var opts = {stdio: 'inherit', cwd: dir};
// Install the unittest packages on every run, using the content of pubspec.yaml
// TODO: maybe this could be memoized or served locally?
spawn(which.sync('pub'), ['install'], opts).on('close', function() {
// Run the tests using built-in test runner.
spawn(which.sync('dart'), [testfile + '.dart'], opts).on('close', done);
});
} catch (e) {
console.log('Dart SDK is not found on the PATH:', e.message);
throw e;
}
}
});
});
gulp.task('test.tsc_e2e', ['test.compile'], function(done) {
// Test that "tsconfig.json" is read correctly.
var outDir = (__dirname.replace(/\\/g, '/') + '/build/tsc_e2e');
if (fs.existsSync(outDir)) fsx.removeSync(outDir);
fs.mkdirSync(outDir);
var cmd = 'node build/lib/main.js --translateBuiltins --tsconfig test/tsc_e2e/tsconfig.json ' +
'--generateLibraryName=true ' +
'test/tsc_e2e/p1/user.ts';
spawn('sh', ['-c', cmd], {stdio: 'inherit'}).on('close', function(code, signal) {
if (code > 0) {
onError(new Error('Failed to transpile ' + testfile + '.ts'));
return;
}
var content = fs.readFileSync(path.join(outDir, 'p1/user.dart'), 'utf-8');
if (!content.match(/library p1\.user/) || !content.match(/import "package:mapped\/dep.dart"/) ||
!content.match(/Future/)) {
throw new Error('incorrect content in p1.dart:\n' + content)
}
});
});
gulp.task(
'test', ['test.check-format', 'test.check-lint', 'test.unit', 'test.e2e', 'test.tsc_e2e']);
gulp.task('watch', ['test.unit'], function() {
failOnError = false;
// Avoid watching generated .d.ts in the build (aka output) directory.
return gulp.watch(['lib/**/*.ts', 'test/**/*.ts'], {ignoreInitial: true}, ['test.unit']);
});
gulp.task('default', ['compile']);