Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'chokidar' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
exports.watch = function(patterns, excludes, fileList) {
var options = {
ignorePermissionErrors: true,
ignored: createIgnore(excludes)
};
var chokidarWatcher = new chokidar.FSWatcher(options);
watchPatterns(patterns, chokidarWatcher);
var bind = function(fn) {
return function(path) {
return fn.call(fileList, helper.normalizeWinPath(path));
};
};
// register events
chokidarWatcher.on('add', bind(fileList.addFile))
.on('change', bind(fileList.changeFile))
.on('unlink', bind(fileList.removeFile));
return chokidarWatcher;
};
getPackages().forEach(p => {
const srcDir = path.resolve(p, 'src');
try {
fs.accessSync(srcDir, fs.F_OK);
chokidar
.watch(path.resolve(p, 'src'), {ignoreInitial: true})
.on('all', (event, filename) => {
const filePath = path.resolve(srcDir, filename);
if ((event === 'add' || event === 'change') && exists(filePath)) {
// eslint-disable-next-line no-console
console.log(chalk.green('->'), `${event}: ${filename}`);
rebuild(filePath);
} else if (event === 'unlink') {
const buildFile = path.resolve(srcDir, '..', 'build', filename);
try {
fs.unlinkSync(buildFile);
process.stdout.write(
chalk.red(' \u2022 ') +
path.relative(path.resolve(srcDir, '..', '..'), buildFile) +
' (deleted)' +
function createFileWatcher(pattern, command) {
// default task is 'START' // check for blank spaces
if ((/ /g).test(command) === false)
command = 'START ' + command;
const task = getTask(command);
if (!task || !taskFunctionMap[task])
return logger(invalidCommandMsg(command));
const fileWatcher = chokidar.watch(pattern);
const func = taskFunctionMap[task];
const npmScript = command.slice(task.length).trim();
fileWatcher.on('change', path => func(npmScript));
}
function init(options: EncodedFSWatcherOptions) {
let decodedOptions = decodeOptions(options);
watcher = new FSWatcher(decodedOptions);
watcher.on('all', sendEvent);
sendEvent('ready');
// only used for testing
watcher.once('ready', async () => {
// Wait an additional macrotask. This seems to be necessary before changes
// can be picked up.
await new Promise(resolve => setImmediate(resolve));
sendEvent('_chokidarReady');
});
}
});
};
const copyFrom = `./${config.copyFolderName}`;
// 复制目录
var myCopy = function() {
if (!fs.existsSync(copyFrom)) {
return;
}
exists(copyFrom, './node_modules/' + config.copyName, copy);
};
if (config.copyName) {
myCopy();
var args = process.argv.splice(2);
if (args.includes('watchChange')) {
var chokidar = require('chokidar');
chokidar.watch(copyFrom).on('change', myCopy);
}
}
module.exports = myCopy;
const cronRule = this.cronService.getCronStringFromJobPeriod(job.period);
scheduler = schedule.scheduleJob({start: startTime, end: endTime, rule: cronRule}, () => {
this.awsService.s3Sync(job);
});
} else if (job.type === JobType.Live) {
// LIVE JOB
const watchedPath = [];
job.files.forEach((item) => {
watchedPath.push(item.path);
});
const lazyS3Sync = sugar.Function.lazy(() => {
this.awsService.s3Sync(job);
}, 10000, true, 2);
scheduler = chokidar.watch(watchedPath, {awaitWriteFinish: true});
scheduler.on('ready', () => {
lazyS3Sync();
scheduler.on('all', (path, event) => {
lazyS3Sync();
});
});
scheduler.on('error', (err) => {
this.logService.printLog(LogType.ERROR, 'Can\'t run live sync for ' + job.name + ' because of: \r\n' + err);
job.alert = true;
this.jobService.save(job);
});
}
if (scheduler === null) {
return new rxjs_1.Observable(obs => {
const watcher = new FSWatcher({ persistent: true }).add(src_1.getSystemPath(path));
watcher
.on('change', path => {
obs.next({
path: src_1.normalize(path),
time: new Date(),
type: 0 /* Changed */,
});
})
.on('add', path => {
obs.next({
path: src_1.normalize(path),
time: new Date(),
type: 1 /* Created */,
});
})
.on('unlink', path => {
return new Observable_1.Observable(obs => {
const watcher = new FSWatcher({ persistent: true }).add(this._getSystemPath(path));
watcher
.on('change', path => {
obs.next({
path: src_1.normalize(path),
time: new Date(),
type: 0 /* Changed */,
});
})
.on('add', path => {
obs.next({
path: src_1.normalize(path),
time: new Date(),
type: 1 /* Created */,
});
})
.on('unlink', path => {
return new rxjs_1.Observable(obs => {
const opts = { persistent: false };
const watcher = new FSWatcher(opts).add(src_1.getSystemPath(path));
watcher
.on('change', path => {
obs.next({
path: src_1.normalize(path),
time: new Date(),
type: 0 /* Changed */,
});
})
.on('add', path => {
obs.next({
path: src_1.normalize(path),
time: new Date(),
type: 1 /* Created */,
});
})
.on('unlink', path => {
exports.watch = function(patterns, excludes, fileList, usePolling, emitter) {
var watchedPatterns = getWatchedPatterns(patterns);
var options = {
usePolling: usePolling,
ignorePermissionErrors: true,
ignoreInitial: true,
ignored: createIgnore(watchedPatterns, excludes)
};
var chokidarWatcher = new chokidar.FSWatcher(options);
watchPatterns(watchedPatterns, chokidarWatcher);
var bind = function(fn) {
return function(path) {
return fn.call(fileList, helper.normalizeWinPath(path));
};
};
// register events
chokidarWatcher.on('add', bind(fileList.addFile))
.on('change', bind(fileList.changeFile))
.on('unlink', bind(fileList.removeFile))
// If we don't subscribe; unhandled errors from Chokidar will bring Karma down
// (see GH Issue #959)
.on('error', function(e) {