]> git.proxmox.com Git - mirror_xterm.js.git/blob - gulpfile.js
Merge pull request #565 from sourcelair/issue-#531-coveralls
[mirror_xterm.js.git] / gulpfile.js
1 const browserify = require('browserify');
2 const buffer = require('vinyl-buffer');
3 const coveralls = require('gulp-coveralls');
4 const fs = require('fs-extra');
5 const gulp = require('gulp');
6 const istanbul = require('gulp-istanbul');
7 const merge = require('merge-stream');
8 const mocha = require('gulp-mocha');
9 const mochaPhantomJs = require('gulp-mocha-phantomjs');
10 const sorcery = require('sorcery');
11 const source = require('vinyl-source-stream');
12 const sourcemaps = require('gulp-sourcemaps');
13 const ts = require('gulp-typescript');
14
15
16 let buildDir = process.env.BUILD_DIR || 'build';
17
18
19 /**
20 * Compile TypeScript sources to JavaScript files and create a source map file for each TypeScript
21 * file compiled.
22 */
23 gulp.task('tsc', function () {
24 // Remove the lib/ directory to prevent confusion if files were deleted in src/
25 fs.emptyDirSync('lib');
26
27 // Build all TypeScript files (including tests) to lib/, based on the configuration defined in
28 // `tsconfig.json`.
29 let tsProject = ts.createProject('tsconfig.json');
30 let tsResult = tsProject.src().pipe(sourcemaps.init()).pipe(tsProject());
31 let tsc = tsResult.js.pipe(sourcemaps.write('.', {includeContent: false, sourceRoot: ''})).pipe(gulp.dest('lib'));
32
33 // Copy all addons from src/ to lib/
34 let copyAddons = gulp.src('src/addons/**/*').pipe(gulp.dest('lib/addons'));
35
36 // Copy stylesheets from src/ to lib/
37 let copyStylesheets = gulp.src('src/**/*.css').pipe(gulp.dest('lib'));
38
39 return merge(tsc, copyAddons, copyStylesheets);
40 });
41
42 /**
43 * Bundle JavaScript files produced by the `tsc` task, into a single file named `xterm.js` with
44 * Browserify.
45 */
46 gulp.task('browserify', ['tsc'], function() {
47 // Ensure that the build directory exists
48 fs.ensureDirSync(buildDir);
49
50 let browserifyOptions = {
51 basedir: buildDir,
52 debug: true,
53 entries: ['../lib/xterm.js'],
54 standalone: 'Terminal',
55 cache: {},
56 packageCache: {}
57 };
58 let bundleStream = browserify(browserifyOptions)
59 .bundle()
60 .pipe(source('xterm.js'))
61 .pipe(buffer())
62 .pipe(sourcemaps.init({loadMaps: true, sourceRoot: '..'}))
63 .pipe(sourcemaps.write('./'))
64 .pipe(gulp.dest(buildDir));
65
66 // Copy all add-ons from lib/ to buildDir
67 let copyAddons = gulp.src('lib/addons/**/*').pipe(gulp.dest(`${buildDir}/addons`));
68
69 // Copy stylesheets from src/ to lib/
70 let copyStylesheets = gulp.src('lib/**/*.css').pipe(gulp.dest(buildDir));
71
72 return merge(bundleStream, copyAddons, copyStylesheets);
73 });
74
75 gulp.task('instrument-test', function () {
76 return gulp.src(['lib/**/*.js'])
77 // Covering files
78 .pipe(istanbul())
79 // Force `require` to return covered files
80 .pipe(istanbul.hookRequire());
81 });
82
83 gulp.task('test-mocha', ['instrument-test'], function () {
84 return gulp.src(['lib/*test.js', 'lib/**/*test.js'], {read: false})
85 .pipe(mocha())
86 .pipe(istanbul.writeReports());
87 });
88
89 gulp.task('test-mocha-phantomjs', function () {
90 return gulp.src('test-harness.html')
91 .pipe(mochaPhantomJs());
92 });
93
94 /**
95 * Use `sorcery` to resolve the source map chain and point back to the TypeScript files.
96 * (Without this task the source maps produced for the JavaScript bundle points into the
97 * compiled JavaScript files in lib/).
98 */
99 gulp.task('sorcery', ['browserify'], function () {
100 var chain = sorcery.loadSync(`${buildDir}/xterm.js`);
101 var map = chain.apply();
102 chain.writeSync();
103 });
104
105 /**
106 * Submit coverage results to coveralls.io
107 */
108 gulp.task('coveralls', function () {
109 gulp.src('coverage/**/lcov.info')
110 .pipe(coveralls());
111 });
112
113 gulp.task('build', ['sorcery']);
114 gulp.task('test', ['test-mocha', 'test-mocha-phantomjs']);
115 gulp.task('default', ['build']);