From 778a2d197e7af5c254db28c352c9a52a75d19e19 Mon Sep 17 00:00:00 2001 From: suroh Date: Thu, 12 Oct 2023 10:57:33 +0200 Subject: [PATCH] started on watcher script for easy adding of content --- scripts/watcher.js | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 scripts/watcher.js diff --git a/scripts/watcher.js b/scripts/watcher.js new file mode 100644 index 0000000..0c117ec --- /dev/null +++ b/scripts/watcher.js @@ -0,0 +1,64 @@ +import fs from 'fs/promises' +import path from 'path' +import { watch } from 'chokidar' + +function main() { + let watchDir = '' + let sections = [] + + process.argv.forEach((val, i) => { + switch (val) { + case '-i': + watchDir = process.argv[i + 1] || '' + break + + default: + break + } + }) + + if (!watchDir) { + console.error('no directory passed') + } + + const w = watch(watchDir, { + persistent: true, + awaitWriteFinish: { + stabilityThreshold: 2000, + pollInterval: 100 + }, + }) + + w.on('ready',() => { + console.log('Watching', watchDir, 'for changes.') + // check each section against json file + console.log(sections) + + + }) + + w.on('add', (filePath) => { + fileAddedHandler(filePath, sections) + }) + + w.on('change', path => console.log(`File ${path} has been changed`)) + w.on('unlink', path => console.log(`File ${path} has been removed`)) +} + +function fileAddedHandler(filePath, array) { + let section, media + + let fp = filePath.replace(/^\.+/, '') + fp = path.parse(fp).dir.split(path.sep) + console.log(fp) + + // 5 is recurse 4 is not + console.log(fp[3], fp.length) + section = fp[3] + media = fp[4] + + console.log(media) +} + + +main()