-
-
Notifications
You must be signed in to change notification settings - Fork 564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unlink not work #1257
Labels
Comments
All other events are triggered normally |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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;
The text was updated successfully, but these errors were encountered: