mm.site/scripts/watcher.js

65 lines
1.2 KiB
JavaScript

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