forked from dtjohnson/xlsx-populate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
216 lines (192 loc) · 6.46 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"use strict";
/* eslint global-require: "off" */
const gulp = require('gulp');
const browserify = require('browserify');
const babelify = require('babelify');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const rename = require('gulp-rename');
const uglifyjs = require('uglify-js');
const uglifyComposer = require('gulp-uglify/composer');
const sourcemaps = require('gulp-sourcemaps');
const eslint = require("gulp-eslint");
const runSequence = require('run-sequence').use(gulp);
const jsdoc2md = require("jsdoc-to-markdown");
const toc = require('markdown-toc');
const Promise = require("bluebird");
const fs = Promise.promisifyAll(require("fs"));
const karma = require('karma');
const Jasmine = require("jasmine");
// Use the latest uglify.
const uglify = uglifyComposer(uglifyjs, console);
const BROWSERIFY_STANDALONE_NAME = "XlsxPopulate";
const BABEL_CONFIG = {
presets: [
["env", {
targets: {
"browsers": ">0.5%"
}
}]
]
};
const PATHS = {
lib: "./lib/**/*.js",
unit: "./test/unit/**/*.js",
karma: ["./test/helpers/**/*.js", "./test/unit/**/*.spec.js"], // Helpers need to go first
examples: "./examples/**/*.js",
browserify: {
source: "./lib/XlsxPopulate.js",
base: "./browser",
bundle: "xlsx-populate.js",
noEncryptionBundle: "xlsx-populate-no-encryption.js",
sourceMap: "./",
encryptionIngores: ["./lib/Encryptor.js"]
},
readme: {
template: "./docs/template.md",
build: "./README.md"
},
blank: {
workbook: "./blank/blank.xlsx",
template: "./blank/template.js",
build: "./lib/blank.js"
},
jasmineConfigs: {
unit: "./test/unit/jasmine.json",
e2eGenerate: "./test/e2e-generate/jasmine.json",
e2eParse: "./test/e2e-parse/jasmine.json"
}
};
PATHS.lint = [PATHS.lib];
PATHS.unitTestSources = [PATHS.lib, PATHS.unit];
// Function to clear the require cache as running unit tests mess up later tests.
const clearRequireCache = () => {
for (const moduleId in require.cache) {
delete require.cache[moduleId];
}
};
const runKarma = (files, cb) => {
process.chdir(__dirname);
new karma.Server({
files,
frameworks: ['browserify', 'jasmine'],
browsers: ['Chrome', 'Firefox', 'IE'],
preprocessors: {
"./test/**/*.js": ['browserify']
},
plugins: [
'karma-browserify',
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-ie-launcher',
'karma-jasmine'
],
browserify: {
debug: true,
transform: [["babelify", BABEL_CONFIG]],
configure(bundle) {
bundle.once('prebundle', () => {
bundle.transform('babelify').plugin('proxyquire-universal');
});
}
},
singleRun: true,
autoWatch: false,
captureTimeout: 210000,
browserDisconnectTolerance: 3,
browserDisconnectTimeout : 210000,
browserNoActivityTimeout : 210000
}, cb).start();
};
const runJasmine = (configPath, cb) => {
process.chdir(__dirname);
clearRequireCache();
const jasmine = new Jasmine();
jasmine.loadConfigFile(configPath);
jasmine.onComplete(passed => cb(null));
jasmine.execute();
};
const runBrowserify = (ignores, bundle) => {
return browserify({
entries: PATHS.browserify.source,
debug: true,
standalone: BROWSERIFY_STANDALONE_NAME
})
.ignore(ignores)
.transform("babelify", BABEL_CONFIG)
.bundle()
.pipe(source(bundle))
.pipe(buffer())
.pipe(gulp.dest(PATHS.browserify.base))
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(uglify())
.pipe(rename({ extname: '.min.js' }))
.pipe(sourcemaps.write(PATHS.browserify.sourceMap))
.pipe(gulp.dest(PATHS.browserify.base));
};
gulp.task('browser', ['browser-full', 'browser-no-encryption']);
gulp.task('browser-full', ['blank'], () => {
return runBrowserify([], PATHS.browserify.bundle);
});
gulp.task('browser-no-encryption', ['blank'], () => {
return runBrowserify([PATHS.browserify.encryptionIngores], PATHS.browserify.noEncryptionBundle);
});
gulp.task("lint", () => {
return gulp
.src(PATHS.lint)
.pipe(eslint())
.pipe(eslint.format());
});
gulp.task("unit", cb => {
runJasmine(PATHS.jasmineConfigs.unit, cb);
});
gulp.task("e2e-generate", cb => {
runJasmine(PATHS.jasmineConfigs.e2eGenerate, cb);
});
gulp.task("e2e-parse", cb => {
runJasmine(PATHS.jasmineConfigs.e2eParse, cb);
});
gulp.task('e2e-browser', cb => {
runKarma(["./test/helpers/**/*.js", "./browser/xlsx-populate.js", "./test/e2e-browser/**/*.spec.js"], cb);
});
gulp.task('unit-browser', cb => {
runKarma(PATHS.karma, cb);
});
gulp.task("blank", () => {
return Promise
.all([
fs.readFileAsync(PATHS.blank.workbook, "base64"),
fs.readFileAsync(PATHS.blank.template, "utf8")
])
.spread((data, template) => {
const output = template.replace("{{DATA}}", data);
return fs.writeFileAsync(PATHS.blank.build, output);
});
});
gulp.task("docs", () => {
return fs.readFileAsync(PATHS.readme.template, "utf8")
.then(text => {
const tocText = toc(text, { filter: str => str.indexOf('NOTOC-') === -1 }).content;
text = text.replace("<!-- toc -->", tocText);
text = text.replace(/NOTOC-/mg, "");
return jsdoc2md.render({ files: PATHS.lib })
.then(apiText => {
apiText = apiText.replace(/^#/mg, "##");
text = text.replace("<!-- api -->", apiText);
return fs.writeFileAsync(PATHS.readme.build, text);
});
});
});
gulp.task('watch', () => {
// Only watch blank, unit, and docs for changes. Everything else is too slow or noisy.
gulp.watch([PATHS.blank.template, PATHS.blank.workbook], ['blank']);
gulp.watch(PATHS.unitTestSources, ["unit"]);
gulp.watch([PATHS.lib, PATHS.readme.template], ["docs"]);
});
gulp.task('build', cb => {
runSequence(["docs", "browser"], "lint", "unit", "unit-browser", "e2e-parse", "e2e-generate", "e2e-browser", cb);
});
gulp.task("default", cb => {
// Watch just the quick stuff to aid development.
runSequence("blank", ["unit", "docs"], "watch", cb);
});