forked from dart-archive/ts2dart
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
121 lines (107 loc) · 3.89 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
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 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');
gulp.task('test.check-format', function() {
return gulp.src(['*.js', 'lib/**/*.ts', 'test/**/*.ts'])
.pipe(formatter.checkFormat('file', clangFormat))
.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', dir);
// run node with a shell so we can wildcard all the .ts files
var cmd = 'node ../lib/main.js --translateBuiltins --basePath=. --destination=. ' +
'*.ts angular2/src/facade/lang.d.ts';
// Paths must be relative to our source root, so run with cwd == dir.
spawn('sh', ['-c', cmd], {stdio: 'inherit', cwd: dir}).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', ['test.unit', 'test.check-format', 'test.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']);