Move watcher to scanner
This commit is contained in:
parent
b53a7c3509
commit
7e19e86349
|
@ -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'))
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue