After running watch, the monitored file is directly deleted, but the unlink event is not triggered. If you modify the content of the file to be deleted first and then delete the file, the unlink event can be triggered.
Version:
OS & version [e.g. MacOS Catalina 11.5.1]:
node version (run node -v): 16.13.1
npm version (run npm -v): 8.1.2
gulp version (run gulp -v): CLI version: 2.3.0 Local version: 4.0.2
Code:
After running watch, the monitored file is directly deleted, but the unlink event is not triggered. If you modify the content of the file to be deleted first and then delete the file, the unlink event can be triggered.
Version:
OS & version [e.g. MacOS Catalina 11.5.1]: node version (run node -v): 16.13.1 npm version (run npm -v): 8.1.2 gulp version (run gulp -v): CLI version: 2.3.0 Local version: 4.0.2 Code:
const { src, dest, watch } = require('gulp'); const rename = require("gulp-rename"); const replace = require("gulp-replace"); const path = require('path') const glob = require('glob') let deleteSync = null import('del').then(function(module) { deleteSync = module.deleteAsync; })
const paths = { src: "../!(dev)/*/", dest: path.join(dirname, "../"), buildDest: path.join(dirname, "../../") }
const renderTagReg = /{%\srender\s'\S'\s%}/gi
glob(paths.src, function(err,files) { console.log(files) })
function handleReplace(match) { return match.replace(/dev/i, 'component') }
function renameFile(path) { if(path.extname === '.liquid') { return { dirname: path.dirname, basename: handleReplace(path.basename), extname: path.extname } }else { return path } }
function changeFile(filePath) { const destPath = path.join(paths.dest, filePath.slice(0,filePath.lastIndexOf('/') + 1 )) return src(filePath) .pipe(rename(function(path) { return renameFile(path) })) .pipe(replace(renderTagReg, handleReplace)) .pipe(dest(destPath)) }
async function deleteFile(filePath) { const destPath = path.join(paths.dest, filePath.replace(/dev/gi, 'component')) await deleteSync([destPath], { force: true }) }
function watchFiles() {
const watcher = watch(paths.src, { events: 'all', depth: 6 }, function(cb) { cb() })
watcher.on('change', function(filePath, stats) { console.log(File ${filePath} was changed); changeFile(filePath) });
watcher.on('add', function(filePath, stats) { console.log(File ${filePath} was added); changeFile(filePath) });
watcher.on('addDir', function(filePath, stats) { console.log(Dir ${filePath} was added); changeFile(filePath) });
watcher.on('unlink', function(filePath, stats) { console.log(File ${filePath} was removed); deleteFile(filePath) });
watcher.on('unlinkDir', function(filePath, stats) { console.log(Dir ${filePath} was removed); deleteFile(filePath) }); }
function copyFileToMainProject() { return src(paths.src) .pipe(rename(function(path) { return renameFile(path) })) .pipe(replace(renderTagReg, handleReplace)) .pipe(dest(paths.buildDest)) }
exports.watch = watchFiles
exports.build = copyFileToMainProject;