diff options
author | Patryk Niedźwiedziński <pniedzwiedzinski19@gmail.com> | 2020-01-28 18:16:03 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-28 18:16:03 +0100 |
commit | 42c089293fed2db9c8a09d8e55f046169b5f0844 (patch) | |
tree | 18a053fe7f1e03fc5419a8c3d2a8bc8193cb27f0 /lib/generateApi.ts | |
parent | 7b8d75cd87e392d9ffb3defa538a041c4f7e0f0c (diff) | |
parent | acfd4d0c722ceeca6aad51be64baf117e73f286c (diff) | |
download | kronikarz-42c089293fed2db9c8a09d8e55f046169b5f0844.tar.gz kronikarz-42c089293fed2db9c8a09d8e55f046169b5f0844.zip |
Merge pull request #8 from pniedzwiedzinski/feature/generateApi v2.0
Feature/generate api
Diffstat (limited to 'lib/generateApi.ts')
-rw-r--r-- | lib/generateApi.ts | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/lib/generateApi.ts b/lib/generateApi.ts new file mode 100644 index 0000000..0024d9f --- /dev/null +++ b/lib/generateApi.ts @@ -0,0 +1,99 @@ +import fs from 'fs'; +import path from 'path'; +import Post from './Post'; +import { dateToPath } from './utils'; +import { Object as PlainObject } from './interfaces'; + +function mkDirByPathSync(targetDir: string) { + const sep = path.sep; + const initDir = path.isAbsolute(targetDir) ? sep : ''; + const baseDir = '.'; + + return targetDir.split(sep).reduce((parentDir, childDir) => { + const curDir = path.resolve(baseDir, parentDir, childDir); + try { + fs.mkdirSync(curDir); + } catch (err) { + if (err.code === 'EEXIST') { + // curDir already exists! + return curDir; + } + + // To avoid `EISDIR` error on Mac and `EACCES`-->`ENOENT` and `EPERM` on Windows. + if (err.code === 'ENOENT') { + // Throw the original parentDir error on curDir `ENOENT` failure. + throw new Error(`EACCES: permission denied, mkdir '${parentDir}'`); + } + + const caughtErr = ['EACCES', 'EPERM', 'EISDIR'].indexOf(err.code) > -1; + if (!caughtErr || (caughtErr && targetDir === curDir)) { + throw err; // Throw if it's just the last created dir. + } + } + + return curDir; + }, initDir); +} + +export function saveApiEntry(post: Post, path: string) { + const dir = `${path}/api/posts/${dateToPath(post.date)}`; + + mkDirByPathSync(dir); + + const apiEntry = post.toApi(); + + fs.writeFile( + `${dir}/${post.fileTitle}.json`, + JSON.stringify(apiEntry), + (err) => (err ? console.log(err) : null) + ); +} + +/* + * This function generates paged api index on path `/api/posts` + */ +function generateApiIndex(posts: Array<Post>, path: string) { + const parsedPosts = posts.map((post) => { + const apiEntry = post.toApi(); + delete apiEntry.content; + return apiEntry; + }); + const filePath = `${path}/api/posts.json`; + fs.writeFile(filePath, JSON.stringify(parsedPosts), (err) => + err ? console.log(err) : null + ); +} + +function generateCategories(posts: Array<Post>, path: string) { + const categories: PlainObject = {}; + posts.forEach((post) => { + const apiEntry = post.toApi(); + delete apiEntry.content; + if (apiEntry.meta['category']) { + apiEntry.meta['category'].forEach((category: string) => + categories[category] + ? categories[category].push(apiEntry) + : (categories[category] = [apiEntry]) + ); + } + }); + + const dir = `${path}/api/category`; + mkDirByPathSync(dir); + Object.keys(categories).forEach((category) => { + const filePath = `${dir}/${category}.json`; + fs.writeFile(filePath, JSON.stringify(categories[category]), (err) => + err ? console.log(err) : null + ); + }); +} + +export function generateApi(posts: Array<Post>, path: string) { + try { + generateApiIndex(posts, path); + generateCategories(posts, path); + posts.forEach((post) => saveApiEntry(post, path)); + } catch (err) { + console.log(err); + } +} |