Move watcher to scanner

This commit is contained in:
LordMathis 2019-12-08 16:39:35 +01:00
parent b53a7c3509
commit 7e19e86349
No known key found for this signature in database
GPG Key ID: 575849FD91CE470C
2 changed files with 23 additions and 19 deletions

View File

@ -20,25 +20,7 @@ if (config == null) {
const dataHolder = new DataHolder(config)
const scanner = new Scanner(config, dataHolder)
const watcher = chokidar.watch(path.join(process.cwd(), 'content'), {
ignored: /(^|[/\\])\../, // ignore dotfiles
persistent: true
})
watcher
.on('add', filepath => {
console.log(`[Watcher] File ${filepath} has been added`)
scanner.addFile(filepath)
})
.on('change', filepath => {
console.log(`[Watcher] File ${filepath} has been changed`)
scanner.updateFile(filepath)
})
.on('unlink', filepath => {
console.log(`[Watcher] File ${filepath} has been removed`)
scanner.deleteFile(filepath)
})
scanner.watch()
app.use(morgan('common'))

View File

@ -3,6 +3,7 @@ import path from 'path'
import fm from 'front-matter'
import moment from 'moment'
import zlib from 'zlib'
import chokidar from 'chokidar'
export class Scanner {
constructor (config, dataHolder) {
@ -10,6 +11,27 @@ export class Scanner {
this.dataHolder = dataHolder
}
watch () {
const watcher = chokidar.watch(path.join(process.cwd(), 'content'), {
ignored: /(^|[/\\])\../, // ignore dotfiles
persistent: true
})
watcher
.on('add', filepath => {
console.log(`[Watcher] File ${filepath} has been added`)
this.addFile(filepath)
})
.on('change', filepath => {
console.log(`[Watcher] File ${filepath} has been changed`)
this.updateFile(filepath)
})
.on('unlink', filepath => {
console.log(`[Watcher] File ${filepath} has been removed`)
this.deleteFile(filepath)
})
}
addFile (filepath) {
if (path.extname(filepath) === '.jpg' || path.extname(filepath) === '.png' || path.extname(filepath) === '.gif') {
this.copyImage(filepath)