Skip to content

Instantly share code, notes, and snippets.

@ovrmrw
Last active August 24, 2016 07:59
Show Gist options
  • Save ovrmrw/1de8affec11468eceadadbbca6d48c97 to your computer and use it in GitHub Desktop.
Save ovrmrw/1de8affec11468eceadadbbca6d48c97 to your computer and use it in GitHub Desktop.
chokidarで任意のフォルダのファイル追加・変更を監視するサンプル
import chokidar from 'chokidar'; // @types/chokidar
import parse from 'csv-parse'; // @types/csv-parse
import fs from 'fs'; // @types/node
chokidar.watch('S:/chokidar', { ignored: /[\/\\]\./ }).on('all', (event: string, path: string) => {
if (event === 'add' || event === 'change') {
if (new RegExp(/\.(csv|txt)$/).test(path)) {
console.log(event, path);
fs.readFile(path, 'utf8', (err, data: string) => {
if (err) { throw err; }
console.log(data);
parse(data, { columns: true, auto_parse: true }, (err, results: Array<ObjectFromCsv>) => {
if (err) { throw err; }
const now = new Date().getTime();
const newResults = results.map(result => Object.assign(result, { 'timestamp': now }));
console.log(newResults);
});
});
}
}
});
interface ObjectFromCsv {
[key: string]: string | number | boolean | null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment