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 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)

View File

@ -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()
)
)

View File

@ -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(
<Router location={req.url} context={{ data }}>
<App/>
@ -39,7 +39,7 @@ function renderFullPage (html, head, data) {
<!DOCTYPE html>
<html>
<head>
<title>${config.title}</title>
<title>${data[1].title}</title>
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css?family=Open+Sans|Open+Sans+Condensed:700&amp;subset=latin-ext" rel="stylesheet" rel="preload">
<!-- Font Awesome -->