From 53e2f45c6a62bc66185213252378944b19a6c7f9 Mon Sep 17 00:00:00 2001 From: Patryk Niedźwiedziński Date: Thu, 2 Jan 2020 19:55:58 +0100 Subject: Add basic api --- scripts/generateApi.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ scripts/postbuild.sh | 2 +- 2 files changed, 57 insertions(+), 1 deletion(-) (limited to 'scripts') 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 -- cgit 1.4.1 From 6f5413879fc6c4afe04cde9bb930aa9182c57974 Mon Sep 17 00:00:00 2001 From: Patryk Niedźwiedziński Date: Wed, 29 Jan 2020 11:21:51 +0100 Subject: Fix path list --- api/kronikarz.js | 7 +++++ nuxt.config.js | 40 ++++++++++++------------ package.json | 1 - scripts/generateApi.js | 82 -------------------------------------------------- scripts/postbuild.sh | 7 ----- 5 files changed, 27 insertions(+), 110 deletions(-) create mode 100644 api/kronikarz.js delete mode 100755 scripts/generateApi.js delete mode 100755 scripts/postbuild.sh (limited to 'scripts') diff --git a/api/kronikarz.js b/api/kronikarz.js new file mode 100644 index 0000000..5658365 --- /dev/null +++ b/api/kronikarz.js @@ -0,0 +1,7 @@ +import Kronikarz from 'kronikarz' + +const POSTS_PATH = './content/wpisy' + +const k = new Kronikarz(POSTS_PATH) + +export default k diff --git a/nuxt.config.js b/nuxt.config.js index f454cac..7c63f0c 100644 --- a/nuxt.config.js +++ b/nuxt.config.js @@ -1,11 +1,11 @@ -import k from './api' +import k from './api/kronikarz' const BASE_URL = process.env.DEPLOY_PRIME_URL || 'http://localhost:3000' export default { mode: 'universal', server: { - host: '0.0.0.0' + host: '0.0.0.0', }, /* ** Headers of the page @@ -13,7 +13,7 @@ export default { head: { htmlAttrs: { lang: 'pl', - prefix: 'og: http://ogp.me/ns#' + prefix: 'og: http://ogp.me/ns#', }, title: '19 PDH Puszcza', meta: [ @@ -23,32 +23,32 @@ export default { { hid: 'description', name: 'description', - content: process.env.npm_package_description || '' + content: process.env.npm_package_description || '', }, { name: 'google-site-verification', - content: 'V9-RG4-ertDzO9hqxuG29PkDUM8PGqaSOYUMiMaGUOY' + content: 'V9-RG4-ertDzO9hqxuG29PkDUM8PGqaSOYUMiMaGUOY', }, { hid: 'og:image', name: 'og:image', - content: `${BASE_URL}/assets/og/default.jpg` - } + content: `${BASE_URL}/assets/og/default.jpg`, + }, ], link: [ { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }, { href: 'https://fonts.googleapis.com/css?family=Roboto+Slab&display=swap', - rel: 'stylesheet' - } - ] + rel: 'stylesheet', + }, + ], }, env: { - baseUrl: process.env.DEPLOY_URL || 'http://localhost:3000' + baseUrl: process.env.DEPLOY_URL || 'http://localhost:3000', }, generate: { - routes: k.getPosts().map(post => post.route) + routes: k.getPosts().map((post) => post.route), }, /* ** Customize the progress-bar color @@ -72,7 +72,7 @@ export default { modules: [ // Doc: https://axios.nuxtjs.org/usage '@nuxtjs/axios', - '@nuxtjs/pwa' + '@nuxtjs/pwa', ], /* ** Axios module configuration @@ -92,14 +92,14 @@ export default { fs: 'empty', child_process: 'empty', tls: 'empty', - net: 'empty' + net: 'empty', } } config.module.rules.push({ test: /\.md$/, - use: ['raw-loader'] + use: ['raw-loader'], }) - } + }, }, hooks: { generate: { @@ -107,11 +107,11 @@ export default { console.log(route) console.log(path) console.log(errors) - } - } + }, + }, }, loading: { color: '#507b34', - height: '5px' - } + height: '5px', + }, } diff --git a/package.json b/package.json index b8921cd..1970bd0 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,6 @@ "dev": "nuxt", "prebuild": "./scripts/prebuild.sh", "build": "nuxt generate", - "postbuild": "./scripts/postbuild.sh", "start": "nuxt start", "test": "jest", "storybook": "start-storybook -p 6006 -s static", diff --git a/scripts/generateApi.js b/scripts/generateApi.js deleted file mode 100755 index ad266cc..0000000 --- a/scripts/generateApi.js +++ /dev/null @@ -1,82 +0,0 @@ -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 - - return { - date: `${year}-${month}-${day}`, - title, - content: { description, meta }, - route - } -}) - -fs.writeFile('./dist/api/posts.json', JSON.stringify(posts), err => - err ? console.log(err) : null -) - -fs.writeFile('./static/api/posts.json', JSON.stringify(posts), err => - err ? console.log(err) : null -) - -console.log(posts) diff --git a/scripts/postbuild.sh b/scripts/postbuild.sh deleted file mode 100755 index 7b7b1e5..0000000 --- a/scripts/postbuild.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env bash - -mkdir -p ./dist/api -mkdir ./static/api - -node -r esm ./scripts/generateApi.js - -- cgit 1.4.1