Last active
August 24, 2016 07:59
-
-
Save ovrmrw/1de8affec11468eceadadbbca6d48c97 to your computer and use it in GitHub Desktop.
chokidarで任意のフォルダのファイル追加・変更を監視するサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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