#!/bin/node const fs = require('fs'); const fsp = fs.promises; const covers = require('./covers.js') const apikey = '77456d34d3c9185016ef4535935dccf3' function listener() { console.log('File changed, updating covers...'); watcher.close(); fsp.readFile('../list.json') .then(data => { return covers.updatecovers(apikey, JSON.parse(data.toString())); }) .then(list => { list = list.map(result => { if (result.status === 'rejected') { throw result.reason; } return result['value']; }); console.log(`Updated ${list.length} entries`); return fsp.writeFile('../list.json', JSON.stringify(list, null, 4)); }) .catch(reason => { console.log(reason); }) .finally(() => { watcher = fs.watch('../list.json', listener); }); } let watcher = fs.watch('../list.json', listener);