From 7e19e86349a0c9fc6a09cc10a0161b1bce42f7ed Mon Sep 17 00:00:00 2001 From: LordMathis Date: Sun, 8 Dec 2019 16:39:35 +0100 Subject: [PATCH] Move watcher to scanner --- src/server.js | 20 +------------------- src/utils/scanner.js | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/server.js b/src/server.js index 82e0440..90fb9bd 100644 --- a/src/server.js +++ b/src/server.js @@ -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')) diff --git a/src/utils/scanner.js b/src/utils/scanner.js index c91d3ce..6ba7e9c 100644 --- a/src/utils/scanner.js +++ b/src/utils/scanner.js @@ -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)