Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] External Data Sources - Headless Wordpress PoC #211

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: small cleanup
hutchgrant committed Sep 25, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit e0abc9283ecf7059a06ea73fd3c21bf4afb01bf5
27 changes: 9 additions & 18 deletions packages/cli/lifecycles/sources.js
Original file line number Diff line number Diff line change
@@ -3,21 +3,6 @@ const fs = require('fs');
const path = require('path');
const rp = require('request-promise');

// insert home page
// const fileName = 'index';

// const indexGraph = {
// type: 'wp',
// meta: compilation.config.meta,
// template: 'page',
// title: 'Home Page',
// content: '<h1>Home Page</h1>',
// route: '/',
// relativeExpectedPath: `'../${fileName}/${fileName}.js'`,
// label: generateLabel(fileName),
// fileName
// };

let optionsList = {
method: 'GET',
json: true
@@ -34,6 +19,7 @@ const fetchWPPosts = async (compilation) => {
return await rp.get(optionsList);
};

// Future amendments for more of wordpress API
// const fetchWPPages = async (compilation) => {

// optionsList.uri = compilation.config.wpSource + '/wp-json/wp/v2/pages';
@@ -48,6 +34,7 @@ const fetchWPPosts = async (compilation) => {
// return await rp.get(optionsList);
// };

// Generate a random hash based on slug
const generateLabel = (slug) => {
const labelHash = {
algo: 'sha256',
@@ -69,7 +56,7 @@ const generateWPGraph = async(wpJSON, config) => {
return new Promise((resolve, reject) => {
try {
let { title, content, slug } = post;
let fileName = slug.substring(0, 100);
let fileName = slug.substring(0, 100); // needs to be trimmed better this is hacky
let graphPost = {
type: 'wp',
meta: config.meta,
@@ -90,6 +77,9 @@ const generateWPGraph = async(wpJSON, config) => {
}));
};

/*
* Note: This is the same function from our scaffold.js, with small modifications
*/
const writePageComponentsFromTemplate = async (compilation) => {
const createPageComponent = async (file, context) => {
return new Promise(async (resolve, reject) => {
@@ -100,7 +90,7 @@ const writePageComponentsFromTemplate = async (compilation) => {

const templateData = await fs.readFileSync(pageTemplatePath);

let result = templateData.toString().replace(/<entry><\/entry>/g, file.content);
let result = templateData.toString().replace(/<entry><\/entry>/g, file.content); // modified from scaffold.js

result = result.replace(/page-template/g, `eve-${file.label}`);
result = result.replace(/MDIMPORT;/, '');
@@ -139,12 +129,13 @@ const writePageComponentsFromTemplate = async (compilation) => {

return new Promise(async(resolve, reject) => {
try {
// modified from scaffold.js
if (file.type === 'wp') {
let result = await createPageComponent(file, context);

result = await loadPageMeta(file, result, context);

target = path.join(context.scratchDir, file.fileName);
target = path.join(context.scratchDir, file.fileName); // modified from scaffold.js

if (!fs.existsSync(target)) {
fs.mkdirSync(target, { recursive: true });