forked from martinsbalodis/web-scraper-chrome-extension
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgulpfile.js
104 lines (97 loc) · 2.71 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
const gulp = require('gulp')
const browserify = require('browserify')
const watchify = require('watchify')
const source = require('vinyl-source-stream')
const notify = require('gulp-notify')
const Server = require('karma').Server
const path = require('path')
const babelify = require('babelify')
const mocha = require('gulp-spawn-mocha')
// We do karma in gulp instead of npm because we need to recompute all the generated bundles that are loaded to the browser
const runTests = (function () {
let timeout
return function (done = function () {}) {
if (timeout) clearTimeout(timeout)
timeout = setTimeout(function () {
runKarma(done)
runJSDOMTests()
}, 100)
}
})()
function runKarma (done) {
const server = new Server({
configFile: path.join(__dirname, 'karma.conf.js'),
singleRun: true
}, done)
server.start()
}
function runJSDOMTests () {
return gulp.src([
'tests/jsdomSpec.js',
'tests/spec/*Spec.js',
'tests/spec/Selector/*Spec.js'
])
.pipe(mocha({
compilers: 'js:babel-register'
}).on('error', console.error))
}
gulp.task('build:watch', () => generateBuilder(true, true))
gulp.task('build', () => generateBuilder(false, false))
gulp.task('default', ['build:watch'])
function generateBuilder (isWatch, debug) {
const wrapper = isWatch ? watchify : (x) => x
const bundlerBackground = wrapper(browserify({
standalone: 'backgroundScraper',
entries: [
'extension/background_page/background_script.js'
],
debug
}))
const bundlerScraper = wrapper(browserify({
standalone: 'contentScraper',
entries: [
'extension/content_script/content_scraper_browser.js'
],
debug
}))
const bundlerDevtools = wrapper(browserify({
standalone: 'contentScraper',
entries: [
'extension/scripts/App.js'
],
debug
}))
setBundler(bundlerBackground, 'background-scraper.js')
setBundler(bundlerScraper, 'content-scraper.js')
setBundler(bundlerDevtools, 'devtools-scraper.js')
function gulpBundle (bundler, file) {
bundler.bundle()
.on('error', function (err) {
return notify().write(err)
})
.pipe(source(file))
.pipe(gulp.dest('extension/generated/'))
.on('error', function (e) {
console.error(e)
})
.on('end', function () {
runTests()
console.log('finished bundling')
// TODO launch tests
})
}
function setBundler (bundler, file) {
bundler
.transform(babelify, {})
.on('update', function () {
gulpBundle(bundler, file)
})
.on('error', function (err) {
return notify().write(err)
})
.on('log', function (log) {
console.log(log)
})
return gulpBundle(bundler, file)
}
}