From 7e059b42c70197f398dbcd5e7645bd8bc72e81a5 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Thu, 31 Oct 2019 22:35:29 +0100 Subject: [PATCH] Read config as file --- src/utils/api.js | 15 ++++++++++----- src/utils/scanner.js | 16 ++++++++-------- src/utils/serverRender.js | 4 ++-- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/utils/api.js b/src/utils/api.js index c816d49..3502c6f 100644 --- a/src/utils/api.js +++ b/src/utils/api.js @@ -1,14 +1,19 @@ import fs from 'fs' import jsonfile from 'jsonfile' import path from 'path' -import config from '../../config/config.json' export function getData (reqPath = '') { if (reqPath === '') { - return readData(config.dataPath) + return Promise.all([ + readJson(path.join(process.cwd(), 'data.json')), + readJson(path.join(process.cwd(), 'config/config.json')) + ]) } else { - const fileName = path.join(process.cwd(), config.contentPath, reqPath + '.md') - return readFile(fileName, 'utf8') + const fileName = path.join(process.cwd(), 'content', reqPath + '.md') + return Promise.all([ + readFile(fileName, 'utf8'), + readJson(path.join(process.cwd(), 'config/config.json')) + ]) } }; @@ -20,7 +25,7 @@ function readFile (fileName, options) { }) } -function readData (dataPath) { +function readJson (dataPath) { return new Promise(function (resolve, reject) { jsonfile.readFile(dataPath, (err, data) => { err ? reject(err) : resolve(data) diff --git a/src/utils/scanner.js b/src/utils/scanner.js index b1e31f9..c1b6965 100644 --- a/src/utils/scanner.js +++ b/src/utils/scanner.js @@ -27,7 +27,7 @@ export class Scanner { } readfile (filename) { - const filePath = path.join(process.cwd(), config.contentPath, filename) + const filePath = path.join(process.cwd(), 'content', filename) return new Promise((resolve, reject) => { fs.readFile(filePath, 'utf8', (err, data) => { if (err) { @@ -41,7 +41,7 @@ export class Scanner { copyImage (filename) { return new Promise((resolve, reject) => { - const inputPath = path.join(process.cwd(), config.contentPath, filename) + const inputPath = path.join(process.cwd(), 'content', filename) const outputPath = path.join(process.cwd(), 'public/static', filename) fs.copyFile(inputPath, outputPath, (err) => { if (err) { @@ -57,7 +57,7 @@ export class Scanner { return new Promise((resolve, reject) => { 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(); @@ -73,9 +73,9 @@ export class Scanner { } processFile (file, data) { - const filePath = path.join(process.cwd(), config.contentPath, file) + const filePath = path.join(process.cwd(), 'content', file) const metadata = this.fileMetadata(filePath) - + if (config['non-content-files'].indexOf(file) === -1) { const frontMatter = fm(data) @@ -108,7 +108,7 @@ export class Scanner { writeData (callback) { return new Promise((resolve, reject) => { - jsonfile.writeFile(config.dataPath, this.data, (err, data) => { + jsonfile.writeFile(path.join(process.cwd(), 'data.json'), this.data, (err, data) => { if (err) { reject(err) } else { @@ -133,12 +133,12 @@ export class Scanner { } scan () { - this.readdir(config.contentPath) + this.readdir(path.join(process.cwd(), 'content')) .then( (files) => { const filtered = files.filter( (file) => ( - fs.statSync(path.join(process.cwd(), config.contentPath, file)).isFile() + fs.statSync(path.join(process.cwd(), 'content', file)).isFile() ) ) diff --git a/src/utils/serverRender.js b/src/utils/serverRender.js index 35f8548..cb757f5 100644 --- a/src/utils/serverRender.js +++ b/src/utils/serverRender.js @@ -5,7 +5,6 @@ import { App } from '../components' import routes from './routes' import serialize from 'serialize-javascript' import manifest from '../../public/static/manifest.json' -import config from '../../config/config.json' export class ServerRenderer { @@ -23,6 +22,7 @@ export class ServerRenderer { : Promise.resolve() promise.then((data) => { + console.log(data) const markup = renderToString( @@ -39,7 +39,7 @@ function renderFullPage (html, head, data) { - ${config.title} + ${data[1].title}