Skip to content

Commit c0ebed2

Browse files
committed
watch this even more or less
1 parent b0ad4b1 commit c0ebed2

File tree

12 files changed

+133
-37
lines changed

12 files changed

+133
-37
lines changed

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations/gulpfile_js_app_styles.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations/gulpfile_js_app_styles_watch.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/webResources.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gulp-tasks/module-tasks/copy-task.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,62 @@ var clean = require('gulp-rimraf');
66

77
module.exports = function (gulp, module) {
88

9-
var exclude = '*.css|*.js|*.svg|*.map';
9+
var exclude = /(\.css|\.js|\.svg|\.map|\.less|\.map)$/i;
1010

11-
module.task('copy-clean', function () {
11+
// Sometimes node is unable to read the stat property (file-busy)
12+
13+
function isFile(file) {
14+
try {
15+
return file.stat && file.stat.isFile();
16+
} catch (e) {
17+
return false;
18+
}
19+
}
20+
21+
// Cleaning the copy must go in two phases.
22+
// gulp-rimraf will throw an exception if a file is already deleted by deleting the containing directory.
23+
// So we first clean the files and then any empty directory leftovers.
24+
25+
// This task can only start AFTER the other modules tasks. The build will crash if any of the other tasks
26+
// is writing output files and it's somehow included in the gulp.src().
27+
28+
module.task('copy-clean', ['scripts', 'styles', 'svg'], function () {
1229

1330
var outputFiles = [
14-
path.join(module.folders.dest, '**/!(' + exclude + ')')
31+
path.join(module.folders.dest, '**/*')
1532
];
1633

1734
return gulp.src(outputFiles, { read: false })
1835
.pipe(filter(function (file) {
19-
// gulp-rimraf will throw an exception if a file is already deleted by deleting the containing directory.
20-
return file.stat.isFile();
36+
return !exclude.test(file.path) && isFile(file);
2137
}))
2238
.pipe(clean({ force: true }));
2339

2440
});
2541

26-
module.task('copy-clean-dirs', ['copy-clean'], function() {
42+
module.task('copy-clean-dirs', ['copy-clean'], function () {
2743
var outputFiles = [
28-
path.join(module.folders.dest, '**/!(' + exclude + ')')
44+
path.join(module.folders.dest, '**/*')
2945
];
3046

3147
return gulp.src(outputFiles, { read: false })
32-
.pipe(filter(function(file) {
48+
.pipe(filter(function (file) {
3349
// Only include directories
34-
return !file.stat.isFile();
50+
return !exclude.test(file.path) && !isFile(file);
3551
}))
36-
.pipe(sort(function(fileA, fileB) {
52+
.pipe(sort(function (fileA, fileB) {
3753
// Reorder directories -- deepest first
3854

3955
var a = fileA.path.split(path.sep).length;
4056
var b = fileB.path.split(path.sep).length;
4157

4258
return b - a;
4359
}))
44-
.pipe(filter(function(file) {
60+
.pipe(filter(function (file) {
4561
// Only empty directories
4662
var files = fs.readdirSync(file.path);
4763
return !files || files.length == 0;
4864
}))
49-
// .pipe(filter(function(file) {
50-
// console.log(file);
51-
// return true;
52-
// }))
5365
.pipe(clean({ force: true }));
5466
});
5567

@@ -68,7 +80,7 @@ module.exports = function (gulp, module) {
6880
return gulp.src(glob)
6981
.pipe(filter(function (file) {
7082
// Only copy files -- don't copy empty directories
71-
return file.stat.isFile();
83+
return !exclude.test(file.path) && isFile(file);
7284
}))
7385
.pipe(gulp.dest(module.folders.dest));
7486

gulp-tasks/module-tasks/scripts-task.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ module.exports = function (gulp, module) {
1919
'!**/*.ignore.js'
2020
];
2121

22-
module.watch('scripts-watch', function() {
23-
gulp.watch(inputFiles, [ module.name + '-scripts' ]);
22+
module.watch('scripts', function() {
23+
return {
24+
glob: inputFiles
25+
};
26+
// gulp.watch(inputFiles, [ module.name + '-scripts' ]);
2427
});
2528

2629
module.task('scripts-clean', function() {

gulp-tasks/module-tasks/styles-task.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,17 @@ module.exports = function (gulp, module) {
2121
'!**/*.ignore.css'
2222
];
2323

24-
module.watch('styles-watch', function() {
25-
gulp.watch(inputFiles, [ module.name + '-styles' ]);
24+
module.watch('styles', function() {
25+
26+
// Include all *.less files in the watch
27+
var files = inputFiles.concat([]);
28+
files.splice(1, 0, path.join(module.folders.src, '**/*.less'));
29+
30+
return {
31+
glob: files,
32+
tasks: [ 'styles' ]
33+
};
34+
2635
});
2736

2837
module.task('styles-clean', function() {
@@ -68,6 +77,16 @@ module.exports = function (gulp, module) {
6877
.pipe(gulpIf(/\.less$/ , less()))
6978
.pipe(concat(module.name + '.css'))
7079
.pipe(prefixer(['last 2 versions', '> 1%', 'ie 8']))
80+
.pipe(filter(function(file) {
81+
82+
// For gulp-sourcemaps to work with less import statements we'll need to rebase the file
83+
84+
file.base = path.join(file.base, module.folders.src);
85+
file.path = path.join(file.base, module.name + '.css');
86+
87+
return true;
88+
89+
}))
7190
.pipe(sourcemaps.write('.', { sourceRoot: '../src/' + module.name }))
7291
.pipe(gulp.dest(module.folders.dest))
7392

gulp-tasks/module-tasks/templates-task.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ module.exports = function(gulp, module) {
2020
path.join(module.folders.src, '**/*.ng.svg')
2121
];
2222

23-
module.watch('templates-watch', function() {
24-
gulp.watch(inputFiles, [ module.name + '-templates' ]);
23+
module.watch('templates', function() {
24+
return {
25+
glob: inputFiles
26+
};
2527
});
2628

2729
module.task('templates-clean', function() {

gulp-tasks/modules-task.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,46 @@ module.exports = function (gulp) {
9393

9494
// - - - - - - - 8-< - - - - - - - - - - - - - - - - - - - -
9595

96-
function moduleWatchTask(taskname, depsOrFn, fn) {
96+
function moduleWatchTask(taskname, fn) {
9797
var module = this;
98-
var fulltaskname = module.name + '-' + taskname;
98+
var fulltaskname = module.name + '-' + taskname + '-watch';
99+
100+
module.task(taskname + '-watch', null, function(cb) {
101+
102+
var opt = fn();
103+
104+
if(!opt) {
105+
throw new Error('No options returned from watch: ' + taskname);
106+
}
107+
108+
if(!opt.glob) {
109+
throw new Error('No glob watch property in options: ' + taskname);
110+
}
111+
112+
opt.tasks = opt.tasks || [taskname];
113+
114+
var i = opt.tasks.length;
115+
116+
while(i--) {
117+
opt.tasks[i] = module.name + '-' + opt.tasks[i];
118+
}
119+
120+
var watcher = gulp.watch(opt.glob, opt.tasks);
121+
122+
function onEvent(event) {
123+
console.log(event);
124+
}
125+
126+
watcher.on('change', function(event) {
127+
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
128+
var exec = require('child_process').exec;
129+
130+
exec('growlnotify -n angular-project-template -m "' + module.name + '-' + taskname + ': build triggered."')
131+
});
132+
133+
cb();
134+
}, true);
99135

100-
module.task(taskname, depsOrFn, fn, true);
101136
module.watchTasks.push(fulltaskname);
102137
}
103138

gulp-tasks/watch.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/sh
2+
3+
# At the moment of writing gulp doesn't handle thrown exceptions very well when watching for file modifications.
4+
# This ensures the watch is up and running.
5+
6+
# https://github.com/gulpjs/gulp/issues/216
7+
8+
gulp build
9+
10+
until gulp watch
11+
do growlnotify -name gulp -m "gulp watch restarted"
12+
sleep 1
13+
done

0 commit comments

Comments
 (0)