]> git.proxmox.com Git - mirror_xterm.js.git/blob - gulpfile.js
Bump version to 2.9.2
[mirror_xterm.js.git] / gulpfile.js
1 /**
2 * @license MIT
3 */
4
5 const browserify = require('browserify');
6 const buffer = require('vinyl-buffer');
7 const coveralls = require('gulp-coveralls');
8 const fs = require('fs-extra');
9 const gulp = require('gulp');
10 const istanbul = require('gulp-istanbul');
11 const merge = require('merge-stream');
12 const mocha = require('gulp-mocha');
13 const sorcery = require('sorcery');
14 const source = require('vinyl-source-stream');
15 const sourcemaps = require('gulp-sourcemaps');
16 const ts = require('gulp-typescript');
17 const util = require('gulp-util');
18
19 let buildDir = process.env.BUILD_DIR || 'build';
20 let tsProject = ts.createProject('tsconfig.json');
21 let tsProjectSearchAddon = ts.createProject('./src/addons/search/tsconfig.json');
22 let srcDir = tsProject.config.compilerOptions.rootDir;
23 let outDir = tsProject.config.compilerOptions.outDir;
24
25 // Under some environments like TravisCI, this comes out at absolute which can
26 // break the build. This ensures that the outDir is absolute.
27 if (outDir.indexOf(__dirname) !== 0) {
28 outDir = `${__dirname}/${outDir}`;
29 }
30
31 /**
32 * Compile TypeScript sources to JavaScript files and create a source map file for each TypeScript
33 * file compiled.
34 */
35 gulp.task('tsc', function () {
36 // Remove the ${outDir}/ directory to prevent confusion if files were deleted in ${srcDir}/
37 fs.emptyDirSync(`${outDir}`);
38
39 // Build all TypeScript files (including tests) to ${outDir}/, based on the configuration defined in
40 // `tsconfig.json`.
41 let tsResult = tsProject.src().pipe(sourcemaps.init()).pipe(tsProject());
42 let tsc = tsResult.js.pipe(sourcemaps.write('.', {includeContent: false, sourceRoot: ''})).pipe(gulp.dest(outDir));
43
44 fs.emptyDirSync(`${outDir}/addons/search`);
45 let tsResultSearchAddon = tsProjectSearchAddon.src().pipe(sourcemaps.init()).pipe(tsProjectSearchAddon());
46 let tscSearchAddon = tsResultSearchAddon.js.pipe(sourcemaps.write('.', {includeContent: false, sourceRoot: ''})).pipe(gulp.dest(`${outDir}/addons/search`));
47
48 // Copy all addons from ${srcDir}/ to ${outDir}/
49 let copyAddons = gulp.src([`${srcDir}/addons/**/*`, `!${srcDir}/addons/search`, `!${srcDir}/addons/search/**`]).pipe(gulp.dest(`${outDir}/addons`));
50
51 // Copy stylesheets from ${srcDir}/ to ${outDir}/
52 let copyStylesheets = gulp.src(`${srcDir}/**/*.css`).pipe(gulp.dest(outDir));
53
54 return merge(tsc, tscSearchAddon, copyAddons, copyStylesheets);
55 });
56
57 /**
58 * Bundle JavaScript files produced by the `tsc` task, into a single file named `xterm.js` with
59 * Browserify.
60 */
61 gulp.task('browserify', ['tsc'], function() {
62 // Ensure that the build directory exists
63 fs.ensureDirSync(buildDir);
64
65 let browserifyOptions = {
66 basedir: buildDir,
67 debug: true,
68 entries: [`${outDir}/xterm.js`],
69 standalone: 'Terminal',
70 cache: {},
71 packageCache: {}
72 };
73 let bundleStream = browserify(browserifyOptions)
74 .bundle()
75 .pipe(source('xterm.js'))
76 .pipe(buffer())
77 .pipe(sourcemaps.init({loadMaps: true, sourceRoot: '..'}))
78 .pipe(sourcemaps.write('./'))
79 .pipe(gulp.dest(buildDir));
80
81 // Copy stylesheets from ${outDir}/ to ${buildDir}/
82 let copyStylesheets = gulp.src(`${outDir}/**/*.css`).pipe(gulp.dest(buildDir));
83
84 return merge(bundleStream, copyStylesheets);
85 });
86
87 gulp.task('browserify-addons', ['tsc'], function() {
88 let searchOptions = {
89 basedir: `${buildDir}/addons/search`,
90 debug: true,
91 entries: [`${outDir}/addons/search/search.js`],
92 cache: {},
93 packageCache: {}
94 };
95 let searchBundle = browserify(searchOptions)
96 .bundle()
97 .pipe(source('./addons/search/search.js'))
98 .pipe(buffer())
99 .pipe(sourcemaps.init({loadMaps: true, sourceRoot: ''}))
100 .pipe(sourcemaps.write('./'))
101 .pipe(gulp.dest(buildDir));
102
103 // Copy all add-ons from outDir to buildDir
104 let copyAddons = gulp.src([
105 // Copy JS addons
106 `${outDir}/addons/**/*`,
107 // Exclude TS addons from copy as they are being built via browserify
108 `!${outDir}/addons/search`,
109 `!${outDir}/addons/search/**`
110 ]).pipe(gulp.dest(`${buildDir}/addons`));
111
112 return merge(searchBundle, copyAddons);
113 });
114
115 gulp.task('instrument-test', function () {
116 return gulp.src([`${outDir}/**/*.js`])
117 // Covering files
118 .pipe(istanbul())
119 // Force `require` to return covered files
120 .pipe(istanbul.hookRequire());
121 });
122
123 gulp.task('mocha', ['instrument-test'], function () {
124 return gulp.src([`${outDir}/*test.js`, `${outDir}/**/*test.js`], {read: false})
125 .pipe(mocha())
126 .once('error', () => process.exit(1))
127 .pipe(istanbul.writeReports());
128 });
129
130 /**
131 * Run single test file by file name(without file extension). Example of the command:
132 * gulp mocha-test --test InputHandler.test
133 */
134 gulp.task('mocha-test', ['instrument-test'], function () {
135 let testName = util.env.test;
136 util.log("Run test by Name: " + testName);
137 return gulp.src([`${outDir}/${testName}.js`, `${outDir}/**/${testName}.js`], {read: false})
138 .pipe(mocha())
139 .once('error', () => process.exit(1))
140 .pipe(istanbul.writeReports());
141 });
142
143 /**
144 * Use `sorcery` to resolve the source map chain and point back to the TypeScript files.
145 * (Without this task the source maps produced for the JavaScript bundle points into the
146 * compiled JavaScript files in ${outDir}/).
147 */
148 gulp.task('sorcery', ['browserify'], function () {
149 let chain = sorcery.loadSync(`${buildDir}/xterm.js`);
150 chain.apply();
151 chain.writeSync();
152 });
153
154 gulp.task('sorcery-addons', ['browserify-addons'], function () {
155 var chain = sorcery.loadSync(`${buildDir}/addons/search/search.js`);
156 chain.apply();
157 chain.writeSync();
158 });
159
160 /**
161 * Submit coverage results to coveralls.io
162 */
163 gulp.task('coveralls', function () {
164 gulp.src('coverage/**/lcov.info')
165 .pipe(coveralls());
166 });
167
168 gulp.task('build', ['sorcery', 'sorcery-addons']);
169 gulp.task('test', ['mocha']);
170 gulp.task('default', ['build']);