Read config as file

This commit is contained in:
LordMathis 2019-10-31 22:35:29 +01:00
parent 4ce788e750
commit 7e059b42c7
No known key found for this signature in database
GPG Key ID: 575849FD91CE470C
3 changed files with 20 additions and 15 deletions

View File

@ -1,14 +1,19 @@
import fs from 'fs' import fs from 'fs'
import jsonfile from 'jsonfile' import jsonfile from 'jsonfile'
import path from 'path' import path from 'path'
import config from '../../config/config.json'
export function getData (reqPath = '') { export function getData (reqPath = '') {
if (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 { } else {
const fileName = path.join(process.cwd(), config.contentPath, reqPath + '.md') const fileName = path.join(process.cwd(), 'content', reqPath + '.md')
return readFile(fileName, 'utf8') 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) { return new Promise(function (resolve, reject) {
jsonfile.readFile(dataPath, (err, data) => { jsonfile.readFile(dataPath, (err, data) => {
err ? reject(err) : resolve(data) err ? reject(err) : resolve(data)

View File

@ -27,7 +27,7 @@ export class Scanner {
} }
readfile (filename) { readfile (filename) {
const filePath = path.join(process.cwd(), config.contentPath, filename) const filePath = path.join(process.cwd(), 'content', filename)
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf8', (err, data) => { fs.readFile(filePath, 'utf8', (err, data) => {
if (err) { if (err) {
@ -41,7 +41,7 @@ export class Scanner {
copyImage (filename) { copyImage (filename) {
return new Promise((resolve, reject) => { 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) const outputPath = path.join(process.cwd(), 'public/static', filename)
fs.copyFile(inputPath, outputPath, (err) => { fs.copyFile(inputPath, outputPath, (err) => {
if (err) { if (err) {
@ -57,7 +57,7 @@ export class Scanner {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const inputPath = path.join(process.cwd(), 'public/static', filename) const inputPath = path.join(process.cwd(), 'public/static', filename)
const outputPath = path.join(process.cwd(), 'public/static', `${filename}.gz`) const outputPath = path.join(process.cwd(), 'public/static', `${filename}.gz`)
const fileContents = fs.createReadStream(inputPath); const fileContents = fs.createReadStream(inputPath);
const writeStream = fs.createWriteStream(outputPath); const writeStream = fs.createWriteStream(outputPath);
const zip = zlib.createGzip(); const zip = zlib.createGzip();
@ -73,9 +73,9 @@ export class Scanner {
} }
processFile (file, data) { 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) const metadata = this.fileMetadata(filePath)
if (config['non-content-files'].indexOf(file) === -1) { if (config['non-content-files'].indexOf(file) === -1) {
const frontMatter = fm(data) const frontMatter = fm(data)
@ -108,7 +108,7 @@ export class Scanner {
writeData (callback) { writeData (callback) {
return new Promise((resolve, reject) => { 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) { if (err) {
reject(err) reject(err)
} else { } else {
@ -133,12 +133,12 @@ export class Scanner {
} }
scan () { scan () {
this.readdir(config.contentPath) this.readdir(path.join(process.cwd(), 'content'))
.then( .then(
(files) => { (files) => {
const filtered = files.filter( const filtered = files.filter(
(file) => ( (file) => (
fs.statSync(path.join(process.cwd(), config.contentPath, file)).isFile() fs.statSync(path.join(process.cwd(), 'content', file)).isFile()
) )
) )

View File

@ -5,7 +5,6 @@ import { App } from '../components'
import routes from './routes' import routes from './routes'
import serialize from 'serialize-javascript' import serialize from 'serialize-javascript'
import manifest from '../../public/static/manifest.json' import manifest from '../../public/static/manifest.json'
import config from '../../config/config.json'
export class ServerRenderer { export class ServerRenderer {
@ -23,6 +22,7 @@ export class ServerRenderer {
: Promise.resolve() : Promise.resolve()
promise.then((data) => { promise.then((data) => {
console.log(data)
const markup = renderToString( const markup = renderToString(
<Router location={req.url} context={{ data }}> <Router location={req.url} context={{ data }}>
<App/> <App/>
@ -39,7 +39,7 @@ function renderFullPage (html, head, data) {
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>${config.title}</title> <title>${data[1].title}</title>
<!-- Google Fonts --> <!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css?family=Open+Sans|Open+Sans+Condensed:700&amp;subset=latin-ext" rel="stylesheet" rel="preload"> <link href="https://fonts.googleapis.com/css?family=Open+Sans|Open+Sans+Condensed:700&amp;subset=latin-ext" rel="stylesheet" rel="preload">
<!-- Font Awesome --> <!-- Font Awesome -->