Skip to content

Commit

Permalink
fix(watch): support array of source files
Browse files Browse the repository at this point in the history
closes #663
  • Loading branch information
JeroenVinke committed Aug 21, 2017
1 parent ac235bd commit 22a257e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 43 deletions.
63 changes: 42 additions & 21 deletions lib/resources/tasks/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,44 @@ import copyFiles from './copy-files';
const debounceWaitTime = 100;
let isBuilding = false;
let pendingRefreshPaths = [];
let watches = {};
let watchCallback = () => { };
let watches = [
{ name: 'transpile', callback: transpile, source: project.transpiler.source },
{ name: 'markup', callback: processMarkup, source: project.markupProcessor.source },
{ name: 'CSS', callback: processCSS, source: project.cssProcessor.source }
];

watches[project.transpiler.source] = { name: 'transpile', callback: transpile };
watches[project.markupProcessor.source] = { name: 'markup', callback: processMarkup };
watches[project.cssProcessor.source] = { name: 'CSS', callback: processCSS };
if (typeof project.build.copyFiles === 'object') {
for (let src of Object.keys(project.build.copyFiles)) {
watches[src] = { name: 'file copy', callback: copyFiles };
watches.push({ name: 'file copy', callback: copyFiles, source: src });
}
}

let watch = (callback) => {
watchCallback = callback || watchCallback;

const watchPaths = Object.keys(watches);

for(let i = 0; i < watchPaths.length; i++) {
gulpWatch(
watchPaths[i],
{
read: false, // performance optimization: do not read actual file contents
verbose: true
},
(vinyl) => processChange(vinyl));
// watch every glob individually
for(let watcher of watches) {
if (Array.isArray(watcher.source)) {
for(let glob of watcher.source) {
watchPath(glob);
}
} else {
watchPath(watcher.source);
}
}
};

let watchPath = (p) => {
gulpWatch(
p,
{
read: false, // performance optimization: do not read actual file contents
verbose: true
},
(vinyl) => processChange(vinyl));
};

let processChange = (vinyl) => {
if (vinyl.path && vinyl.cwd && vinyl.path.startsWith(vinyl.cwd)) {
let pathToAdd = vinyl.path.substr(vinyl.cwd.length + 1);
Expand All @@ -60,11 +70,20 @@ let refresh = debounce(() => {
let paths = pendingRefreshPaths.splice(0);
let refreshTasks = [];

// Dynamically compose tasks
for (let src of Object.keys(watches)) {
if (paths.find((x) => minimatch(x, src))) {
log(`Watcher: Adding ${watches[src].name} task to next build...`);
refreshTasks.push(watches[src].callback);
// determine which tasks need to be executed
// based on the files that have changed
for (let watcher of watches) {
if (Array.isArray(watcher.source)) {
for(let source of watcher.source) {
if (paths.find(path => minimatch(path, source))) {
refreshTasks.push(watcher);
}
}
}
else {
if (paths.find(path => minimatch(path, watcher.source))) {
refreshTasks.push(watcher);
}
}
}

Expand All @@ -74,9 +93,11 @@ let refresh = debounce(() => {
return;
}

log(`Watcher: Running ${refreshTasks.map(x => x.name).join(', ')} tasks on next build...`);

let toExecute = gulp.series(
readProjectConfiguration,
gulp.parallel(refreshTasks),
gulp.parallel(refreshTasks.map(x => x.callback)),
writeBundles,
(done) => {
isBuilding = false;
Expand Down
65 changes: 43 additions & 22 deletions lib/resources/tasks/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,44 @@ import copyFiles from './copy-files';
const debounceWaitTime = 100;
let isBuilding = false;
let pendingRefreshPaths = [];
let watches = {};
let watchCallback = () => { };
let watches = [
{ name: 'transpile', callback: transpile, source: project.transpiler.source },
{ name: 'markup', callback: processMarkup, source: project.markupProcessor.source },
{ name: 'CSS', callback: processCSS, source: project.cssProcessor.source }
];

watches[project.transpiler.source] = { name: 'transpile', callback: transpile };
watches[project.markupProcessor.source] = { name: 'markup', callback: processMarkup };
watches[project.cssProcessor.source] = { name: 'CSS', callback: processCSS };
if (typeof project.build.copyFiles === 'object') {
for (let src of Object.keys(project.build.copyFiles)) {
watches[src] = { name: 'file copy', callback: copyFiles };
watches.push({ name: 'file copy', callback: copyFiles, source: src });
}
}

let watch = (callback?) => {
let watch = (callback) => {
watchCallback = callback || watchCallback;

const watchPaths = Object.keys(watches);

for(let i = 0; i < watchPaths.length; i++) {
gulpWatch(
watchPaths[i],
{
read: false, // performance optimization: do not read actual file contents
verbose: true
},
(vinyl) => processChange(vinyl));
// watch every glob individually
for(let watcher of watches) {
if (Array.isArray(watcher.source)) {
for(let glob of watcher.source) {
watchPath(glob);
}
} else {
watchPath(watcher.source);
}
}
};

let watchPath = (p) => {
gulpWatch(
p,
{
read: false, // performance optimization: do not read actual file contents
verbose: true
},
(vinyl) => processChange(vinyl));
};

let processChange = (vinyl) => {
if (vinyl.path && vinyl.cwd && vinyl.path.startsWith(vinyl.cwd)) {
let pathToAdd = vinyl.path.substr(vinyl.cwd.length + 1);
Expand All @@ -60,11 +70,20 @@ let refresh = debounce(() => {
let paths = pendingRefreshPaths.splice(0);
let refreshTasks = [];

// Dynamically compose tasks
for (let src of Object.keys(watches)) {
if (paths.find((x) => minimatch(x, src))) {
log(`Watcher: Adding ${watches[src].name} task to next build...`);
refreshTasks.push(watches[src].callback);
// determine which tasks need to be executed
// based on the files that have changed
for (let watcher of watches) {
if (Array.isArray(watcher.source)) {
for(let source of watcher.source) {
if (paths.find(path => minimatch(path, source))) {
refreshTasks.push(watcher);
}
}
}
else {
if (paths.find(path => minimatch(path, watcher.source))) {
refreshTasks.push(watcher);
}
}
}

Expand All @@ -74,9 +93,11 @@ let refresh = debounce(() => {
return;
}

log(`Watcher: Running ${refreshTasks.map(x => x.name).join(', ')} tasks on next build...`);

let toExecute = gulp.series(
readProjectConfiguration,
gulp.parallel(refreshTasks),
gulp.parallel(refreshTasks.map(x => x.callback)),
writeBundles,
(done) => {
isBuilding = false;
Expand Down

0 comments on commit 22a257e

Please sign in to comment.