diff --git a/utils/parse-content.js b/utils/parse-content.js new file mode 100644 index 0000000..689c4bf --- /dev/null +++ b/utils/parse-content.js @@ -0,0 +1,44 @@ +const fs = require('fs') +const path = require('path') +const _set = require('lodash/set') +const _merge = require('lodash/merge') +const globCb = require('glob') +const util = require('util') + +const glob = util.promisify(globCb) +const readFile = util.promisify(fs.readFile) + +const options = { + contentDir: './content/', + outputFile: './src/data.json' +} + +const getNestedKey = filePath => { + const pathParsed = path.parse(filePath) + const objectKey = pathParsed.dir.replace(options.contentDir, '').replace(/\//g, '.') + return `${objectKey}.${pathParsed.name}` +} + +const getFileContents = filePath => + readFile(filePath, 'utf8') + .then(data => { + let obj = {} + _set(obj, getNestedKey(filePath), data) + return obj + }) + +const readFiles = async paths => Promise.all(paths.map(getFileContents)) + +const combineJSON = async () => { + const paths = await glob(`${options.contentDir}/**/**.json`) + const results = await readFiles(paths) + const data = _merge({}, ...results) + return JSON.stringify(data, null, 2) +} + +const writeJSON = async () => { + const json = await combineJSON() + fs.writeFileSync(options.outputFile, json) +} + +writeJSON() diff --git a/utils/parse-posts.js b/utils/parse-posts.js deleted file mode 100644 index ad4bd7b..0000000 --- a/utils/parse-posts.js +++ /dev/null @@ -1,45 +0,0 @@ -const fs = require('fs') -const path = require('path') - -const globCb = require('glob') -const util = require('util') - -const glob = util.promisify(globCb) -const readFile = util.promisify(fs.readFileSync) - -const options = { - contentDir: './content/', - outputFile: './src/data.json' -} - -const getFileContents = async file => { - const result = await readFile(file, 'utf8') - return result -} - -const readFiles = async paths => { - const results = await Promise.all(paths.map(getFileContents)) - return results -} - -const makeJSON = async () => { - const paths = await glob(`${options.contentDir}/**/**.json`) - const results = await parseFiles(paths) - return console.log(results) - let data = {} - results.map(item => { - const pathParsed = path.parse(item.history[0]) - const objectKey = pathParsed.dir.replace(options.markdownDir, '').replace(/\//g, '.') - const nest = `${objectKey}.${pathParsed.name}` - _set(data, nest, {...item}) - }) - return JSON.stringify(data, null, 2) -} - -const markdownToJson = async () => { - const json = await makeJSON() - return console.log(json) - fs.writeFileSync(options.outputFile, json) -} - -markdownToJson()