]> git.proxmox.com Git - mirror_xterm.js.git/blob - gulpfile.js
Use gulp-mocha and attach run both test type in npm test
[mirror_xterm.js.git] / gulpfile.js
1 const browserify = require('browserify');
2 const buffer = require('vinyl-buffer');
3 const fs = require('fs-extra');
4 const gulp = require('gulp');
5 const merge = require('merge-stream');
6 const mocha = require('gulp-mocha');
7 const mochaPhantomJs = require('gulp-mocha-phantomjs');
8 const sorcery = require('sorcery');
9 const source = require('vinyl-source-stream');
10 const sourcemaps = require('gulp-sourcemaps');
11 const ts = require('gulp-typescript');
12 const tsify = require('tsify');
13
14
15 let buildDir = process.env.BUILD_DIR || 'build';
16
17
18 /**
19 * Compile TypeScript sources to JavaScript files and create a source map file for each TypeScript
20 * file compiled.
21 */
22 gulp.task('tsc', function () {
23 // Remove the lib/ directory to prevent confusion if files were deleted in src/
24 fs.emptyDirSync('lib');
25
26 // Build all TypeScript files (including tests) to lib/, based on the configuration defined in
27 // `tsconfig.json`.
28 let tsProject = ts.createProject('tsconfig.json');
29 let tsResult = tsProject.src().pipe(sourcemaps.init()).pipe(tsProject());
30 let tsc = tsResult.js.pipe(sourcemaps.write('.', {includeContent: false, sourceRoot: ''})).pipe(gulp.dest('lib'));
31
32 // Copy all addons from src/ to lib/
33 let copyAddons = gulp.src('src/addons/**/*').pipe(gulp.dest('lib/addons'));
34
35 // Copy stylesheets from src/ to lib/
36 let copyStylesheets = gulp.src('src/**/*.css').pipe(gulp.dest('lib'));
37
38 return merge(tsc, copyAddons, copyStylesheets);
39 });
40
41 /**
42 * Bundle JavaScript files produced by the `tsc` task, into a single file named `xterm.js` with
43 * Browserify.
44 */
45 gulp.task('browserify', ['tsc'], function() {
46 // Ensure that the build directory exists
47 fs.ensureDirSync(buildDir);
48
49 let browserifyOptions = {
50 basedir: buildDir,
51 debug: true,
52 entries: ['../lib/xterm.js'],
53 standalone: 'Terminal',
54 cache: {},
55 packageCache: {}
56 };
57 let bundleStream = browserify(browserifyOptions)
58 .plugin(tsify)
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('test-mocha', function () {
76 return gulp.src(['lib/*test.js', 'lib/**/*test.js'], {read: false})
77 .pipe(mocha())
78 });
79
80 gulp.task('test-mocha-phantomjs', function () {
81 return gulp.src('test-harness.html')
82 .pipe(mochaPhantomJs());
83 });
84
85 /**
86 * Use `sorcery` to resolve the source map chain and point back to the TypeScript files.
87 * (Without this task the source maps produced for the JavaScript bundle points into the
88 * compiled JavaScript files in lib/).
89 */
90 gulp.task('sorcery', ['browserify'], function () {
91 var chain = sorcery.loadSync(`${buildDir}/xterm.js`);
92 var map = chain.apply();
93 chain.writeSync();
94 });
95
96 gulp.task('build', ['sorcery']);
97 gulp.task('test', ['test-mocha', 'test-mocha-phantomjs']);
98 gulp.task('default', ['build']);