Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var startWatching = function ( source, pattern, rebuild, done) {
console.log ( chalk.green('Started watching ') + chalk.cyan(pattern));
var gaze = new Gaze(pattern, { cwd : source });
gaze.on('ready', _.once(function(){
done();
}));
gaze.on('all', function ( event, filepath ) {
var name = relative(source, filepath);
fs.lstat(filepath, function(err, stats){
// Ignore errors and directorys
if ( err || stats.isDirectory() === true ) { return; }
console.log ( chalk.cyan( name ) + ' was ' + chalk.green( event ) );
// rebuild the current file `filepath`
// using the metalsmith rebuilder.
export default function initKSS() {
const gaze = new Gaze(program.source);
// Run once no matter what to show most up to date
kssTraverse(gaze.watched());
gaze.on('error', (error) => {
console.log(`An error has occured: ${error}`);
return;
});
gaze.on('nomatch', () => {
console.log('No matches found');
return;
});
gaze.on('all', (event, filepath) => {
// Adding/Deleting files
if (typeof target.options.event === 'string') {
target.options.event = [target.options.event];
}
var eventCwd = process.cwd();
if (target.options.cwd && target.options.cwd.event) {
eventCwd = target.options.cwd.event;
}
// Set cwd if options.cwd.file is set
if (typeof target.options.cwd !== 'string' && target.options.cwd.files) {
target.options.cwd = target.options.cwd.files;
}
// Create watcher per target
watchers.push(new Gaze(patterns, target.options, function(err) {
if (err) {
if (typeof err === 'string') {
err = new Error(err);
}
grunt.log.writeln('ERROR'.red);
grunt.fatal(err);
return taskrun.done();
}
// Log all watched files with --verbose set
if (grunt.option('verbose')) {
var watched = this.watched();
Object.keys(watched).forEach(function(watchedDir) {
watched[watchedDir].forEach(function(watchedFile) {
grunt.log.writeln('Watching ' + path.relative(process.cwd(), watchedFile) + ' for changes.');
});
targets.forEach(function(target, i) {
if (typeof target.files === 'string') {
target.files = [target.files];
}
// Process into raw patterns
var patterns = grunt.util._.chain(target.files).flatten().map(function(pattern) {
return grunt.config.process(pattern);
}).value();
// Default options per target
var options = grunt.util._.defaults(target.options || {}, defaults);
// Create watcher per target
var gaze = new Gaze(patterns, options, function(err) {
if (err) {
grunt.log.error(err.message);
return done();
}
// On changed/added/deleted
this.on('all', function(status, filepath) {
filepath = path.relative(process.cwd(), filepath);
changedFiles[filepath] = status;
runTasks(i, target.tasks, options);
});
// On watcher error
this.on('error', function(err) { grunt.log.error(err); });
});
});
var patterns = _.chain(target.files).flatten().map(function(pattern) {
return grunt.config.process(pattern);
}).value();
// Validate the event option
if (typeof target.options.event === 'string') {
target.options.event = [target.options.event];
}
// Set cwd if options.cwd.file is set
if (typeof target.options.cwd !== 'string' && target.options.cwd.files) {
target.options.cwd = target.options.cwd.files;
}
// Create watcher per target
watchers.push(new Gaze(patterns, target.options, function(err) {
if (err) {
if (typeof err === 'string') { err = new Error(err); }
grunt.log.writeln('ERROR'.red);
grunt.fatal(err);
return taskrun.done();
}
// Log all watched files with --verbose set
if (grunt.option('verbose')) {
var watched = this.watched();
Object.keys(watched).forEach(function(watchedDir) {
watched[watchedDir].forEach(function(watchedFile) {
grunt.log.writeln('Watching ' + path.relative(process.cwd(), watchedFile) + ' for changes.');
});
});
}
// metalsmith-collections fix: keep filename as metadata
saveFilenameInFilesData(files)
const patterns = {}
Object.keys(options.paths).map(pattern => {
let watchPattern = pattern.replace("${source}", metalsmith.source())
if (!isAbsolutePath(watchPattern)){
watchPattern = resolvePath(metalsmith.directory(), pattern);
}
const watchPatternRelative = relativePath(metalsmith.directory(), watchPattern)
patterns[watchPatternRelative] = options.paths[pattern]
})
gaze(
Object.keys(patterns),
{
...options.gaze,
cwd: metalsmith._directory,
},
function watcherReady(err, watcher) {
if (err) {throw err}
Object.keys(patterns).forEach(pattern => {
options.log(`${ok} Watching ${color.cyan(pattern)}`)
})
const previousFilesMap = {...files}
// Delay watch update to be able to bundle multiples update in the same build
// Saving multiples files at the same time create multiples build otherwise
if (err) {
log.error('serve', 'Parsing failed');
log.error('serve', err.message);
process.exit(1);
}
server.listen(options.port, function() {
log.info('serve', 'Listening on http://localhost:' +
options.port + '/ (Ctrl+C to stop)');
});
server.on('error', function(err) {
log.error('serve', 'Server failed to start: ' + err.message);
process.exit(1);
});
});
var gaze = new Gaze('examples/**/*');
var debouncedBuild = debounce(buildExamplesOrFatal, 250);
gaze.on('all', function(event, filepath) {
log.verbose('serve', 'Watch event: ' + event + ' ' + filepath);
debouncedBuild();
});
});
if (!watch) {
const cssFiles = filesList ? filesList : globby.sync(filesPattern);
cssFiles.forEach(handleFile);
if (!filesList) {
detectDanlingFlowFiles(filesPattern, cssFiles);
}
} else {
if (!filePath) {
console.error(
chalk.red(`Watch mode requires a single path... Not ${filesList}`)
);
return;
}
gaze(filesPattern, function(err, files) {
this.on('changed', handleFile);
this.on('added', handleFile);
});
}
};
BowerBrowser.prototype.readBowerJson = function (callback) {
var self = this;
var json = null;
var error = null;
try {
var buffer = fs.readFileSync(jsonPath);
if (!gaze) {
gaze = new Gaze('bower.json', {cwd: cwd});
gaze.on('all', function () {
self.sendBowerData();
});
}
json = JSON.parse(buffer);
}
catch (e) {
error = e;
}
if (typeof callback === 'function') {
callback(error, json);
}
};
function watch(dir) {
var d = Q.defer();
dir = path.resolve(dir);
var gaze = new Gaze("**/*.md", {
cwd: dir
});
gaze.once("all", function(e, filepath) {
gaze.close();
d.resolve(filepath);
});
gaze.once("error", function(err) {
gaze.close();
d.reject(err);
});
return d.promise;
}