From 4c1c8542140981d69cd49ddb5c06c2ecf2b1c877 Mon Sep 17 00:00:00 2001 From: Paul Walker Date: Thu, 1 Aug 2019 15:05:35 -0400 Subject: [PATCH 1/3] [CMS][m]: Refactor WP CMS backend as plugin --- env.template | 2 +- index.js | 2 -- {lib => plugins/wp}/cms.js | 2 +- routes/cms.js => plugins/wp/index.js | 34 +++++++++++++++++++++------- routes/dms.js | 18 ++------------- tests/lib/index.test.js | 8 +++---- tests/routes/index.test.js | 2 +- 7 files changed, 35 insertions(+), 33 deletions(-) rename {lib => plugins/wp}/cms.js (94%) rename routes/cms.js => plugins/wp/index.js (74%) diff --git a/env.template b/env.template index bde120d3..ad9f1859 100644 --- a/env.template +++ b/env.template @@ -1,4 +1,4 @@ -PLUGINS="example" +PLUGINS="example wp" WP_TOKEN= NODE_ENV=development THEME=example diff --git a/index.js b/index.js index a522279e..d1b66134 100644 --- a/index.js +++ b/index.js @@ -10,7 +10,6 @@ const i18n = require("i18n") const config = require('./config') const dmsRoutes = require('./routes/dms') -const cmsRoutes = require('./routes/cms') const {loadTheme, loadPlugins} = require('./utils') module.exports.makeApp = function () { @@ -67,7 +66,6 @@ module.exports.makeApp = function () { // Controllers app.use([ - cmsRoutes(), dmsRoutes() ]) diff --git a/lib/cms.js b/plugins/wp/cms.js similarity index 94% rename from lib/cms.js rename to plugins/wp/cms.js index cd99f535..be200fce 100644 --- a/lib/cms.js +++ b/plugins/wp/cms.js @@ -1,6 +1,6 @@ 'use strict' -const config = require('../config') +const config = require('../../config') const wpcom = require('wpcom')(config.get('WP_TOKEN')) diff --git a/routes/cms.js b/plugins/wp/index.js similarity index 74% rename from routes/cms.js rename to plugins/wp/index.js index c4fd0e3c..7dcbe94d 100644 --- a/routes/cms.js +++ b/plugins/wp/index.js @@ -3,16 +3,35 @@ const express = require('express') const moment = require('moment') -const cms = require('../lib/cms') +const cms = require('./cms') -module.exports = function () { - const router = express.Router() +module.exports = function (app) { const Model = new cms.CmsModel() - router.get('/news', listStaticPages) - router.get('/news/:page', showPostPage) - router.get(['/:page', '/:parent/:page'], showStaticPage) + app.get('/', async (req, res) => { + // Get latest 3 blog posts and pass it to home template + const size = 3 + let posts = await Model.getListOfPosts(size) + posts = posts.map(post => { + return { + slug: post.slug, + title: post.title, + content: post.content, + published: moment(post.date).format('MMMM Do, YYYY'), + modified: moment(post.modified).format('MMMM Do, YYYY'), + image: post.featured_image + } + }) + res.render('home.html', { + title: 'Home', + posts + }) + }) + + app.get('/news', listStaticPages) + app.get('/news/:page', showPostPage) + app.get(['/:page', '/:parent/:page'], showStaticPage) async function listStaticPages(req, res) { // Get latest 10 blog posts @@ -63,6 +82,7 @@ module.exports = function () { slug += `-${locale}` } try { + console.log('get post') const post = await Model.getPost(slug) res.render('static.html', { slug: post.slug, @@ -82,6 +102,4 @@ module.exports = function () { } } } - - return router } diff --git a/routes/dms.js b/routes/dms.js index 6f3f8c3a..05892a8d 100644 --- a/routes/dms.js +++ b/routes/dms.js @@ -6,14 +6,12 @@ const bytes = require('bytes') const config = require('../config') const dms = require('../lib/dms') -const cms = require('../lib/cms') const utils = require('../utils') module.exports = function () { const router = express.Router() const Model = new dms.DmsModel(config) - const CmsModel = new cms.CmsModel() // ----------------------------------------------- // Redirects @@ -36,22 +34,10 @@ module.exports = function () { // ----------------------------------------------- router.get('/', async (req, res) => { - // Get latest 3 blog posts and pass it to home template - const size = 3 - let posts = await CmsModel.getListOfPosts(size) - posts = posts.map(post => { - return { - slug: post.slug, - title: post.title, - content: post.content, - published: moment(post.date).format('MMMM Do, YYYY'), - modified: moment(post.modified).format('MMMM Do, YYYY'), - image: post.featured_image - } - }) + // If no CMS is enabled, show home page without posts res.render('home.html', { title: 'Home', - posts + posts: [] }) }) diff --git a/tests/lib/index.test.js b/tests/lib/index.test.js index e41db08e..0041c1fd 100644 --- a/tests/lib/index.test.js +++ b/tests/lib/index.test.js @@ -1,13 +1,13 @@ const test = require('ava') -const cms = require('../../lib/cms') +const wp = require('../../plugins/wp/cms') const dms = require('../../lib/dms') const config = require('../../config') const mocks = require('../../fixtures') mocks.initMocks() -const CmsModel = new cms.CmsModel() +const WPModel = new wp.CmsModel() const DmsModel = new dms.DmsModel(config) @@ -15,7 +15,7 @@ test('getPost api works', async t => { t.plan(1) const slug = 'about' - const result = await CmsModel.getPost(slug) + const result = await WPModel.getPost(slug) t.is(result.title, 'Welcome to Data Service!') }) @@ -24,7 +24,7 @@ test('getPost api works', async t => { test('getListOfPosts api works', async t => { t.plan(1) - const result = await CmsModel.getListOfPosts() + const result = await WPModel.getListOfPosts() t.is(result.length, 2) }) diff --git a/tests/routes/index.test.js b/tests/routes/index.test.js index fb3438d7..40abe77b 100644 --- a/tests/routes/index.test.js +++ b/tests/routes/index.test.js @@ -24,7 +24,7 @@ test('Theme defined route does NOT exists when THEME is not set', async t => { config.set('THEME', 'opendk') const app = require('../../index').makeApp() const res = await request(app) - .get('/foo') + .get('/absolutely-not-a-chance') t.is(res.statusCode, 500) }) From 0241d6e0e459a9a74ce1a9f08278f5de4b0f7879 Mon Sep 17 00:00:00 2001 From: Paul Walker Date: Thu, 1 Aug 2019 15:14:48 -0400 Subject: [PATCH 2/3] [tests][s]: Ensure wp plugin enabled if needed --- tests/routes/index.test.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/routes/index.test.js b/tests/routes/index.test.js index 40abe77b..4fe7878b 100644 --- a/tests/routes/index.test.js +++ b/tests/routes/index.test.js @@ -69,6 +69,8 @@ test('Missing plugin load is caught and app still loads', async t => { // CMS test('About page works', async t => { + config.set('PLUGINS', "wp") + const app = require('../../index').makeApp() t.plan(2) const res = await request(app) @@ -80,6 +82,9 @@ test('About page works', async t => { test('News home page works', async t => { + config.set('PLUGINS', "wp") + const app = require('../../index').makeApp() + t.plan(2) const res = await request(app) @@ -91,6 +96,9 @@ test('News home page works', async t => { test('Single post page works', async t => { + config.set('PLUGINS', "wp") + const app = require('../../index').makeApp() + t.plan(2) const res = await request(app) From 9812552fb0204a3af5cffaa07e4ae96cd91f0ac7 Mon Sep 17 00:00:00 2001 From: Paul Walker Date: Thu, 1 Aug 2019 15:20:15 -0400 Subject: [PATCH 3/3] [docs][xs]: Add explanation of CMS plugins --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 52c838ca..cb1cd29a 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,18 @@ export API_URL=https://demo.ckan.org/api/3/action/ ### CMS +To use a CMS plugin, enable the plugin in your environment. + +Example: + +in `.env`: +``` +PLUGINS="wp" +``` + +#### Wordpress +The wordpress plugin (`/plugins/wp`) ships with frontend-v2. + Use `WP_URL` environment variable to point to your WordPress instance. For example, we have test wordpress blog here https://edscms.home.blog/ so it would be: ```