Collect & simulate hooks

This commit is contained in:
Lynn Smeria 2020-03-18 01:30:56 +02:00
parent cc2cf0201f
commit 0e380a26ba
4 changed files with 99 additions and 6 deletions

38
hooks.js Normal file
View file

@ -0,0 +1,38 @@
const fs = require('fs').promises;
const ms = require('ms');
const os = require('os');
const path = require('path');
const getHooksRoot = () => path.join(os.homedir(), '/.syncthing-hooks');
const readHooksRoot = async root => {
try {
const files = await fs.readdir(root);
return files;
} catch {
return [];
}
};
const parseHooks = (root, hooks) =>
hooks
.map(x => ({
path: path.join(root, x),
match: x.match(/(?<folder>.{11})-(?<time>.*)/),
}))
.filter(x => x.match)
.map(x => ({
path: x.path,
folder: x.match.groups.folder,
time: ms(x.match.groups.time),
}));
const collectHooks = async () => {
const root = getHooksRoot();
const hooks = await readHooksRoot(root);
return parseHooks(root, hooks);
};
module.exports = {
collectHooks,
};