started on watcher script for easy adding of content

This commit is contained in:
suroh 2023-10-12 10:57:33 +02:00
parent 7aa4b9baba
commit 778a2d197e
1 changed files with 64 additions and 0 deletions

64
scripts/watcher.js Normal file
View File

@ -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()