about summary refs log tree commit diff
path: root/scripts
diff options
context:
space:
mode:
authorPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2020-01-02 19:55:58 +0100
committerPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2020-01-02 19:55:58 +0100
commit53e2f45c6a62bc66185213252378944b19a6c7f9 (patch)
treec6cfb1f70993e760bd62b79773a9f14f57c8291a /scripts
parent6b3b28848f06667f36b4c20bf83ada0e5f578a99 (diff)
downloadpuszcza-53e2f45c6a62bc66185213252378944b19a6c7f9.tar.gz
puszcza-53e2f45c6a62bc66185213252378944b19a6c7f9.zip
Add basic api
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/generateApi.js56
-rwxr-xr-xscripts/postbuild.sh2
2 files changed, 57 insertions, 1 deletions
diff --git a/scripts/generateApi.js b/scripts/generateApi.js
index 4b7426a..ad266cc 100755
--- a/scripts/generateApi.js
+++ b/scripts/generateApi.js
@@ -1,8 +1,64 @@
 import fs from 'fs'
 import k from '../api'
+import path from 'path'
+
+function mkDirByPathSync(targetDir, { isRelativeToScript = false } = {}) {
+  const sep = path.sep
+  const initDir = path.isAbsolute(targetDir) ? sep : ''
+  const baseDir = isRelativeToScript ? __dirname : '.'
+
+  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)
+}
 
 let posts = k.getPosts()
 
+posts.forEach(post => {
+  const { year, month, day } = post.date
+  const dir = `api/posts/${year}/${month}/${day}`
+  mkDirByPathSync(`./dist/${dir}`)
+  mkDirByPathSync(`./static/${dir}`)
+  console.log(`${dir}/${post.title}.json`)
+  const apiEntry = {
+    title: post.title,
+    date: `${year}-${month}-${day}`,
+    content: post.content.html,
+    meta: post.content.meta
+  }
+  fs.writeFile(
+    `./dist/${dir}/${post.title}.json`,
+    JSON.stringify(apiEntry),
+    err => (err ? console.log(err) : null)
+  )
+  fs.writeFile(
+    `./static/${dir}/${post.title}.json`,
+    JSON.stringify(apiEntry),
+    err => (err ? console.log(err) : null)
+  )
+})
+
 posts = posts.map(({ date, title, content, route }) => {
   const { year, month, day } = date
   const { description, meta } = content
diff --git a/scripts/postbuild.sh b/scripts/postbuild.sh
index 6da3659..7b7b1e5 100755
--- a/scripts/postbuild.sh
+++ b/scripts/postbuild.sh
@@ -1,6 +1,6 @@
 #!/usr/bin/env bash
 
-mkdir ./dist/api
+mkdir -p ./dist/api
 mkdir ./static/api
 
 node -r esm ./scripts/generateApi.js