summary refs log tree commit diff
path: root/lib/generateApi.ts
diff options
context:
space:
mode:
authorPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2020-01-28 11:55:03 +0100
committerPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2020-01-28 11:55:03 +0100
commit9f75ff1d2d5ebf34c040474e836e125f80253365 (patch)
tree523c52b9dd158e21d9247b58d81c655102323a26 /lib/generateApi.ts
parentc11d7a84aa59e9b1179a9834129b2d776bab8bca (diff)
downloadkronikarz-9f75ff1d2d5ebf34c040474e836e125f80253365.tar.gz
kronikarz-9f75ff1d2d5ebf34c040474e836e125f80253365.zip
Add Post class
Diffstat (limited to 'lib/generateApi.ts')
-rw-r--r--lib/generateApi.ts49
1 files changed, 31 insertions, 18 deletions
diff --git a/lib/generateApi.ts b/lib/generateApi.ts
index 836fa2a..cade7e4 100644
--- a/lib/generateApi.ts
+++ b/lib/generateApi.ts
@@ -1,29 +1,30 @@
-import fs from "fs";
-import path from "path";
-import { Post } from "./interfaces";
+import fs from 'fs';
+import path from 'path';
+import Post from './Post';
+import { dateToPath } from './utils';
 
 function mkDirByPathSync(targetDir: string) {
   const sep = path.sep;
-  const initDir = path.isAbsolute(targetDir) ? sep : "";
-  const baseDir = ".";
+  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") {
+      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") {
+      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;
+      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.
       }
@@ -34,26 +35,38 @@ function mkDirByPathSync(targetDir: string) {
 }
 
 export function saveApiEntry(post: Post, path: string) {
-  const { year, month, day } = post.date;
-  const dir = `${path}/api/posts/${year}/${month}/${day}`;
+  const dir = `${path}/api/posts/${dateToPath(post.date)}`;
 
   mkDirByPathSync(dir);
 
-  const apiEntry = {
-    title: post.title,
-    date: `${year}-${month}-${day}`,
-    content: post.content.html,
-    meta: post.content.meta
-  };
+  const apiEntry = post.toApi();
 
-  fs.writeFile(`${dir}/${post.title}.json`, JSON.stringify(apiEntry), err =>
+  fs.writeFile(
+    `${dir}/${post.fileTitle}.json`,
+    JSON.stringify(apiEntry),
+    (err) => (err ? console.log(err) : null)
+  );
+}
+
+/*
+ * This function generates paged api index on path `/api/paged-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
   );
 }
 
 export function generateApi(posts: Array<Post>, path: string) {
   try {
-    posts.forEach(post => saveApiEntry(post, path));
+    generateApiIndex(posts, path);
+    posts.forEach((post) => saveApiEntry(post, path));
   } catch (err) {
     console.log(err);
   }