-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!') | ||
}); | ||
} | ||
}); | ||
} |