Skip to content

Commit 333e4c7

Browse files
committed
Fix script watch task
1 parent b6a14b6 commit 333e4c7

File tree

5 files changed

+47
-17
lines changed

5 files changed

+47
-17
lines changed

tasks/config.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from 'path';
22
import fs from 'fs';
33

4+
const cwd = process.cwd();
45
const pkgPath = path.resolve('package.json');
56
const pkg = JSON.parse(fs.readFileSync(pkgPath));
67

@@ -53,7 +54,7 @@ config.script = {
5354
config.example.js + '/example.js',
5455
],
5556
noParse: [
56-
path.join(process.cwd(), config.build, config.name + '.js'),
57+
path.join(cwd, config.build, config.name + '.js'),
5758
],
5859
output: 'example.js',
5960
dest: config.example.build,
@@ -208,10 +209,13 @@ config.lint = {
208209
// Test config
209210
config.test = {
210211
unit: {
211-
configFile: process.cwd() + '/karma.conf.js',
212+
configFile: cwd + '/karma.conf.js',
212213
autoWatch: false,
213214
singleRun: true,
214215
},
216+
watch: {
217+
configFile: cwd + '/karma.conf.js',
218+
},
215219
};
216220

217221
// Release config

tasks/helpers/script-helper.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,26 @@ import source from 'vinyl-source-stream';
77
import buffer from 'vinyl-buffer';
88
import _ from 'lodash';
99

10-
function bundleScript(bundler, output, dest) {
10+
function bundleScript(bundler, output, dest, cb) {
1111
function onError(error) {
1212
gutil.log(gutil.colors.red('Browserify Error' + error));
1313
}
1414

15+
function onEnd() {
16+
if (cb) {
17+
cb();
18+
}
19+
}
20+
1521
return bundler.bundle()
1622
.on('error', onError)
23+
.on('end', onEnd)
1724
.pipe(source(output))
1825
.pipe(buffer())
1926
.pipe(gulp.dest(dest));
2027
}
2128

22-
function compileScript(watch, opts) {
29+
function compileScript(watch, opts, cb) {
2330
const acceptedOpts = _.pick(opts, 'paths', 'entries', 'noParse', 'debug', 'standalone');
2431
const browserifyOpts = _.defaults({}, acceptedOpts, watchify.args);
2532
const babelifyOpts = { comments: false };
@@ -44,11 +51,11 @@ function compileScript(watch, opts) {
4451
bundler.on('update', () => {
4552
gutil.log('Bundling ' + gutil.colors.green(opts.output));
4653

47-
bundleScript(bundler, opts.output, opts.dest);
54+
return bundleScript(bundler, opts.output, opts.dest, cb);
4855
});
4956
}
5057

51-
return bundleScript(bundler, opts.output, opts.dest);
58+
return bundleScript(bundler, opts.output, opts.dest, cb);
5259
}
5360

5461
const scriptHelper = {

tasks/run.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import gulp from 'gulp';
22

33
gulp.task('run', [
44
'build',
5-
'connect',
65
'watch',
6+
'connect',
77
]);

tasks/test.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@ import gulp from 'gulp';
22
import config from './config';
33
import { Server } from 'karma';
44

5+
function startServer(options, done) {
6+
const server = new Server(options, done);
7+
8+
server.start();
9+
}
10+
511
gulp.task('test', [
612
'test:script',
713
]);
814

9-
gulp.task('test:script', ['test:unit']);
10-
11-
gulp.task('test:unit', (done) => {
12-
const server = new Server(config.test.unit, done);
15+
gulp.task('test:script', (done) => {
16+
startServer(config.test.unit, done);
17+
});
1318

14-
server.start();
19+
gulp.task('test:script:watch', (done) => {
20+
startServer(config.test.watch, done);
1521
});

tasks/watch.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@ import gulp from 'gulp';
22
import config from './config';
33
import { compileScript } from './helpers/script-helper';
44

5-
gulp.task('watch', ['build'], () => {
6-
gulp.watch(config.watch.style.src, ['style']);
7-
gulp.watch(config.watch.script.src, ['lint:script', 'test:script']);
8-
gulp.watch(config.watch.task.src, ['lint:task']);
5+
gulp.task('watch', [
6+
'watch:script',
7+
'watch:style',
8+
'watch:task',
9+
'test:script:watch',
10+
]);
911

10-
compileScript(true, config.watch.script.build);
12+
gulp.task('watch:script', () => {
1113
compileScript(true, config.watch.script.example);
14+
compileScript(true, config.watch.script.build, () => {
15+
gulp.start('lint:script');
16+
});
17+
});
18+
19+
gulp.task('watch:style', () => {
20+
gulp.watch(config.watch.style.src, ['style']);
21+
});
22+
23+
gulp.task('watch:task', () => {
24+
gulp.watch(config.watch.task.src, ['lint:task']);
1225
});

0 commit comments

Comments
 (0)