Skip to content

Commit

Permalink
adds watch for js files
Browse files Browse the repository at this point in the history
  • Loading branch information
smohadjer committed Jan 6, 2022
1 parent 1f80991 commit 3f744a5
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions bin/watch-js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const fse = require("fs-extra");
const chokidar = require('chokidar');
const src = 'app/resources/js/';
const dest = 'public/resources/js/';

// using cwd option so instead of path we get filename
const watcher = chokidar.watch('.', {
ignored: /(^|[\/\\])\../, // ignore dotfiles
persistent: true,
cwd: src
});

// Something to use when events are received.
const log = console.log.bind(console);
// Add event listeners.
watcher
.on('add', path => {
log(`File ${path} has been added`);
copyFile(src + path, dest + path);
})
.on('change', path => {
log(`File ${path} has been changed`);
copyFile(src + path, dest + path);
})
.on('unlink', path => {
log(`File ${path} has been removed`);
fse.unlink(dest + path);
});

function copyFile(source, destination) {
fse.pathExists(source, (err, exists) => {
//console.log(err);

if (exists) {
fse.copy(source, destination, function (err) {
if (err){
console.log('An error occured while copying the folder.')
return console.error(err)
}
console.log(source, ' copy completed!')
});
}
});
}

0 comments on commit 3f744a5

Please sign in to comment.