diff options
author | Patryk Niedźwiedziński <pniedzwiedzinski19@gmail.com> | 2020-01-03 00:24:34 +0100 |
---|---|---|
committer | Patryk Niedźwiedziński <pniedzwiedzinski19@gmail.com> | 2020-01-03 00:24:34 +0100 |
commit | c11d7a84aa59e9b1179a9834129b2d776bab8bca (patch) | |
tree | c5c053ace0b8fca3c74fee360036bf1e2efd1c2c /lib | |
parent | 7b8d75cd87e392d9ffb3defa538a041c4f7e0f0c (diff) | |
download | kronikarz-c11d7a84aa59e9b1179a9834129b2d776bab8bca.tar.gz kronikarz-c11d7a84aa59e9b1179a9834129b2d776bab8bca.zip |
Add post endpoints
Diffstat (limited to 'lib')
-rw-r--r-- | lib/generateApi.ts | 60 | ||||
-rw-r--r-- | lib/index.ts | 5 |
2 files changed, 65 insertions, 0 deletions
diff --git a/lib/generateApi.ts b/lib/generateApi.ts new file mode 100644 index 0000000..836fa2a --- /dev/null +++ b/lib/generateApi.ts @@ -0,0 +1,60 @@ +import fs from "fs"; +import path from "path"; +import { Post } 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 { year, month, day } = post.date; + const dir = `${path}/api/posts/${year}/${month}/${day}`; + + mkDirByPathSync(dir); + + const apiEntry = { + title: post.title, + date: `${year}-${month}-${day}`, + content: post.content.html, + meta: post.content.meta + }; + + fs.writeFile(`${dir}/${post.title}.json`, JSON.stringify(apiEntry), err => + err ? console.log(err) : null + ); +} + +export function generateApi(posts: Array<Post>, path: string) { + try { + posts.forEach(post => saveApiEntry(post, path)); + } catch (err) { + console.log(err); + } +} diff --git a/lib/index.ts b/lib/index.ts index ade7180..f9647bc 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,5 +1,6 @@ import { getPost as apiGetPost } from "./getPost"; import { getPosts as apiGetPosts } from "./getPosts"; +import { generateApi as apiGenerateApi } from "./generateApi" import { Post } from "./interfaces"; export default class Kronikarz { @@ -15,4 +16,8 @@ export default class Kronikarz { getPost(year: string, month: string, day: string, title: string): Post { return apiGetPost({ year, month, day, title }, this.postPath); } + generateApi(path: string) { + const posts = this.getPosts(); + apiGenerateApi(posts, path) + } } |