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