diff --git a/src/components/App.js b/src/components/App.js index ea03911..729568f 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -1,4 +1,4 @@ -import { NotFoundWrapper } from '../containers' +import { NotFoundContainer } from '../containers' import React, { Component } from 'react' import routes from '../utils/routes' import { Route, Switch } from 'react-router-dom' @@ -18,7 +18,7 @@ export default class App extends Component { )} /> ))} - } /> + } /> ) diff --git a/src/server.js b/src/server.js index b972df4..7ac0754 100644 --- a/src/server.js +++ b/src/server.js @@ -12,7 +12,7 @@ const port = process.env.PORT || 3000 const app = express() app.set('trust proxy', true) -let config = jsonfile.readFileSync(path.join(process.cwd(), 'config/config.json')) +const config = jsonfile.readFileSync(path.join(process.cwd(), 'config/config.json')) if (config == null) { throw new Error('Config file not found!') } @@ -20,26 +20,22 @@ if (config == null) { const scanner = new Scanner(config) const watcher = chokidar.watch(path.join(process.cwd(), 'content'), { - ignored: /(^|[\/\\])\../, // ignore dotfiles - persistent: true, - ignoreInitial: true -}); + ignored: /(^|[/\\])\../, // ignore dotfiles + persistent: true +}) watcher - .on('ready', () => { - scanner.scan() + .on('add', filepath => { + console.log(`[Watcher] File ${filepath} has been added`) + scanner.addFile(filepath) }) - .on('add', path => { - console.log(`[Watcher] File ${path} has been added`) - scanner.scan() + .on('change', filepath => { + console.log(`[Watcher] File ${filepath} has been changed`) + scanner.updateFile(filepath) }) - .on('change', path => { - console.log(`[Watcher] File ${path} has been changed`) - scanner.scan() - }) - .on('unlink', path => { - console.log(`[Watcher] File ${path} has been removed`) - scanner.scan() + .on('unlink', filepath => { + console.log(`[Watcher] File ${filepath} has been removed`) + scanner.deleteFile(filepath) }) app.use(morgan('common')) @@ -65,7 +61,7 @@ app.get('/favicon.ico', (req, res) => { let head = jsonfile.readFileSync(path.join(process.cwd(), 'config/head.json')) if (head == null) { head = { - "scripts": [] + scripts: [] } } diff --git a/src/utils/api.js b/src/utils/api.js index 833f0e7..0e84c94 100644 --- a/src/utils/api.js +++ b/src/utils/api.js @@ -18,8 +18,8 @@ function readFile (fileName, type, options) { return new Promise(function (resolve, reject) { fs.readFile(fileName, options, (err, data) => { err ? reject(err) : resolve({ - 'type': type, - 'data': data + type: type, + data: data }) }) }) diff --git a/src/utils/scanner.js b/src/utils/scanner.js index 3521aad..0c6e058 100644 --- a/src/utils/scanner.js +++ b/src/utils/scanner.js @@ -9,50 +9,59 @@ export class Scanner { constructor (config) { this.config = config this.initData() - } initData () { this.data = { - 'posts': [], - 'other': {} + posts: [], + other: {} } } - readdir (dirname) { - return new Promise((resolve, reject) => { - fs.readdir(dirname, function (err, filenames) { - if (err) { - reject(err) - } else { - resolve(filenames) - } - }) - }) + addFile (filepath) { + if (path.extname(filepath) === '.jpg' || path.extname(filepath) === '.png' || path.extname(filepath) === '.gif') { + this.copyImage(filepath) + .then((file) => this.gzipImage(file)) + } else { + this.readfile(filepath) + .then((data) => this.processFile(data[0], data[1])) + .then(() => this.writeData()) + } } - readfile (filename) { - const filePath = path.join(process.cwd(), 'content', filename) + updateFile (filepath) { + this.deleteFile(filepath) + this.addFile(filepath) + } + + deleteFile (filepath) { + this.data.posts = this.data.posts.filter((post) => + post.filename !== filepath + ) + } + + readfile (filePath) { + const relPath = path.relative(path.join(process.cwd(), 'content'), filePath) return new Promise((resolve, reject) => { fs.readFile(filePath, 'utf8', (err, data) => { if (err) { reject(err) } else { - resolve([filename, data]) + resolve([relPath, data]) } }) }) } - copyImage (filename) { + copyImage (filePath) { return new Promise((resolve, reject) => { - const inputPath = path.join(process.cwd(), 'content', filename) - const outputPath = path.join(process.cwd(), 'public/static', filename) - fs.copyFile(inputPath, outputPath, (err) => { + const relPath = path.relative(path.join(process.cwd(), 'content'), filePath) + const outputPath = path.join(process.cwd(), 'public/static', relPath) + fs.copyFile(filePath, outputPath, (err) => { if (err) { reject(err) } else { - resolve(filename) + resolve(relPath) } }) }) @@ -63,14 +72,13 @@ export class Scanner { const inputPath = path.join(process.cwd(), 'public/static', filename) const outputPath = path.join(process.cwd(), 'public/static', `${filename}.gz`) - const fileContents = fs.createReadStream(inputPath); - const writeStream = fs.createWriteStream(outputPath); - const zip = zlib.createGzip(); + const fileContents = fs.createReadStream(inputPath) + const writeStream = fs.createWriteStream(outputPath) + const zip = zlib.createGzip() fileContents.pipe(zip).pipe(writeStream).on('finish', (err) => { if (err) { reject(err) - } - else { + } else { resolve() } }) @@ -80,12 +88,12 @@ export class Scanner { processFile (file, data) { const filePath = path.join(process.cwd(), 'content', file) const metadata = this.fileMetadata(filePath) - + if (this.config['non-content-files'].indexOf(file) === -1) { const frontMatter = fm(data) if (frontMatter.attributes.draft) { - return + return Promise.resolve() } let published @@ -109,6 +117,8 @@ export class Scanner { } else { this.data.other[metadata.filename] = data } + + return Promise.resolve() } writeData () { @@ -138,43 +148,43 @@ export class Scanner { return metadata } - scan () { - this.readdir(path.join(process.cwd(), 'content')) - .then( - (files) => { - const filtered = files.filter( - (file) => ( - fs.statSync(path.join(process.cwd(), 'content', file)).isFile() - ) - ) + // scan () { + // this.readdir(path.join(process.cwd(), 'content')) + // .then( + // (files) => { + // const filtered = files.filter( + // (file) => ( + // fs.statSync(path.join(process.cwd(), 'content', file)).isFile() + // ) + // ) - const images = filtered.filter( - (file) => ( - path.extname(file) == '.jpg' || path.extname(file) == '.png' || path.extname(file) == '.gif' - ) - ) - Promise.all(images.map(this.copyImage)) - .then((files) => files.map(this.gzipImage)) + // const images = filtered.filter( + // (file) => ( + // path.extname(file) == '.jpg' || path.extname(file) == '.png' || path.extname(file) == '.gif' + // ) + // ) + // Promise.all(images.map(this.copyImage)) + // .then((files) => files.map(this.gzipImage)) - const posts = filtered.filter( - (file) => ( - path.extname(file) == '.md' - ) - ) + // const posts = filtered.filter( + // (file) => ( + // path.extname(file) == '.md' + // ) + // ) - return Promise.all(posts.map(this.readfile)) - } - ).then( - (files) => { - files.forEach( - (item) => { this.processFile(item[0], item[1]) } - ) - return this.writeData() - } - ).then( - console.log('[Scanner] Scan complete') - ).catch( - (err) => console.log(err) - ) - } + // return Promise.all(posts.map(this.readfile)) + // } + // ).then( + // (files) => { + // files.forEach( + // (item) => { this.processFile(item[0], item[1]) } + // ) + // return this.writeData() + // } + // ).then( + // console.log('[Scanner] Scan complete') + // ).catch( + // (err) => console.log(err) + // ) + // } } diff --git a/src/utils/serverRender.js b/src/utils/serverRender.js index 07762df..2d692c2 100644 --- a/src/utils/serverRender.js +++ b/src/utils/serverRender.js @@ -7,16 +7,14 @@ import serialize from 'serialize-javascript' import manifest from '../../public/static/manifest.json' export class ServerRenderer { - constructor (head, config) { this.head = head this.config = config } render (req, res, next) { - const activeRoute = routes.find((route) => matchPath(req.url, route)) || false - const head = this.head + const head = this.head const config = this.config if (!activeRoute) { @@ -31,7 +29,7 @@ export class ServerRenderer { const promise = activeRoute.getData ? activeRoute.getData(req.path) : Promise.resolve() - + promise.then((data) => { const context = [data, config] const markup = renderToString( @@ -39,14 +37,14 @@ export class ServerRenderer { ) - + res.status(200).send(renderFullPage(markup, head, data, config)) - }).catch(next) + }).catch(next) } } } -function renderFullPage (html, head, data, config) { +function renderFullPage (html, head, data, config) { const initialData = [data, config] return `