forked from bpampuch/pdfmake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
131 lines (115 loc) · 3.25 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
122
123
124
125
126
127
128
129
130
131
var gulp = require('gulp');
var babel = require("gulp-babel");
var webpack = require('webpack');
var mocha = require('gulp-spawn-mocha');
var eslint = require('gulp-eslint');
var each = require('gulp-each');
var fc2json = require('gulp-file-contents-to-json');
var log = require('fancy-log');
var exec = require('child_process').exec;
const fs = require('fs');
var DEBUG = process.env.NODE_ENV === 'debug';
var CI = process.env.CI === 'true';
var vfsBefore = "var vfs = ";
var vfsAfter = "; if (typeof this.pdfMake !== 'undefined' && typeof this.pdfMake.addVirtualFileSystem !== 'undefined') { this.pdfMake.addVirtualFileSystem(vfs); } if (typeof module !== 'undefined') { module.exports = vfs; }";
gulp.task('buildNode', function () {
return gulp.src('src/**/*.js')
.pipe(babel({
presets: [
[
"@babel/preset-env",
{
targets: {
node: "6.10"
},
loose: true
}
]
]
}))
.pipe(gulp.dest("js"));
});
gulp.task('buildBrowser', function (callback) {
webpack(require('./webpack.config.js'), function (err, stats) {
if (err) {
callback(err);
return;
}
log("[webpack]", stats.toString({}));
if (stats.compilation.errors && stats.compilation.errors.length) {
callback(stats.compilation.errors);
return;
}
callback();
});
});
gulp.task('buildWithStandardFonts', function (callback) {
webpack(require('./webpack-standardfonts.config.js'), function (err, stats) {
if (err) {
callback(err);
return;
}
log("[webpack]", stats.toString({}));
if (stats.compilation.errors && stats.compilation.errors.length) {
callback(stats.compilation.errors);
return;
}
callback();
});
});
gulp.task('test', function () {
return gulp.src(['./tests/**/*.spec.js'])
.pipe(mocha({
debugBrk: DEBUG,
R: CI ? 'spec' : 'nyan'
}));
});
gulp.task('lint', function () {
return gulp.src(['./src/**/*.js', './tests/**/*.js'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task('buildFonts', function () {
return gulp.src(['./examples/fonts/*.*'])
.pipe(each(function (content, file, callback) {
var newContent = Buffer.from(content).toString('base64');
callback(null, newContent);
}, 'buffer'))
.pipe(fc2json('vfs_fonts.js', { flat: true }))
.pipe(each(function (content, file, callback) {
var newContent = vfsBefore + content + vfsAfter;
callback(null, newContent);
}, 'buffer'))
.pipe(gulp.dest('build'));
});
gulp.task('watch', function () {
gulp.watch('./src/**', ['test', 'build']);
gulp.watch('./tests/**', ['test']);
});
gulp.task('generateExamples', function (cb) {
var errCount = 0;
var position = 0;
process.chdir('examples');
const items = fs.readdirSync('.');
const files = items.filter(file => file.substring(file.length - 3, file.length) === '.js');
files.forEach(function (file) {
exec(`node ${file}`, function (err, stdout, stderr) {
position++;
log('FILE: ', file, ` (${position}/${files.length})`);
log(stdout);
if (err) {
errCount++;
log(stderr);
}
if (position === files.length) {
if (errCount) {
log('Errors count: ', errCount);
}
cb();
}
});
});
});
gulp.task('build', gulp.series('buildNode', 'buildBrowser'));
gulp.task('default', gulp.series('build', 'buildFonts', 'test', 'lint'));