Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
Merge pull request #3 from helpscout/setup-configs
Browse files Browse the repository at this point in the history
Allow for proper config options
  • Loading branch information
ItsJonQ committed Aug 2, 2017
2 parents ffe9b9d + 9f83561 commit b48a536
Show file tree
Hide file tree
Showing 18 changed files with 416 additions and 108 deletions.
12 changes: 12 additions & 0 deletions example/template/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const template =
`---
title: "<%= front_matter.title %>"
description: "<%= front_matter.description %>"
date: "<%= date %>"
slug: "<%= slug %>"
---
<%= content %>
`;

export default template;
27 changes: 21 additions & 6 deletions src/generate.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
import { isString } from 'lodash';
import { isObject, isString } from 'lodash';
import savePost from './savePost';
import template from './template/post.js';

const defaultDir = './posts';
const defaultOptions = {
dest: './posts',
template: template,
};

export const generate = (options = defaultOptions) => (posts = []) => {
if (!isObject(options)) return false;

const config = Object.assign({}, defaultOptions, options);
const dest = config.dest;
const template = config.template;

if (!isString(dest) || !isString(template)) return false;

export const generate = (dir = defaultDir) => (posts = []) => {
if (!isString(dir)) return false;
const saveQueue = [];

posts.forEach(post => {
saveQueue.push(savePost(dir)(post));
saveQueue.push(savePost(options)(post));
});

return Promise.all(saveQueue);
return (
Promise.all(saveQueue)
/* istanbul ignore next */
.catch(err => err)
);
};

export default generate;
12 changes: 8 additions & 4 deletions src/generatePost.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { template } from 'lodash';
import postTemplate from './template/post';
import { isString, template } from 'lodash';
import defaultPostTemplate from './template/post';

const generatePost = data => {
return template(postTemplate)(data);
const generatePost = (postTemplate = defaultPostTemplate) => {
return data => {
if (!isString(postTemplate)) return false;

return template(postTemplate)(data);
};
};

export default generatePost;
46 changes: 29 additions & 17 deletions src/getPosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,39 @@ const endpoints = {
};

const getPosts = params => {
if (!isObject(params)) return false;

const defaultParams = {
archived: false,
state: 'PUBLISHED',
};
const url = getEndpointUrl(
endpoints.hubspot,
Object.assign({}, defaultParams, params)
);

/* istanbul ignore next */
// Test exists for this return. However, it's reporting incomplete since the
// else condition is being skipped.
if (!url || !isString(url)) return false;

/* istanbul ignore next */
// Skipping the coverage test of node-fetch
return new Promise((resolve, reject) => {
if (!isObject(params)) {
reject("Marq requires an object for it's options");
}
if (!params.hapikey || !params.content_group_id) {
reject("Hmm… Something's not right with your Hubspot credentials.");
}

const defaultParams = {
archived: false,
state: 'PUBLISHED',
};
const url = getEndpointUrl(
endpoints.hubspot,
Object.assign({}, defaultParams, params)
);

/* istanbul ignore next */
// Test exists for this return. However, it's reporting incomplete since the
// else condition is being skipped.
if (!url || !isString(url)) {
reject("Hmm… Something's not right with your Hubspot credentials.");
}

fetch(url)
.then(res => res.json())
.then(res => {
if (res.status !== 200) {
reject("Hmm… Something's not right with your Hubspot credentials.");
}
return res.json();
})
.then(results => resolve(results.objects))
.catch(err => reject(err));
});
Expand Down
26 changes: 21 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import generate from './generate';
import getPosts from './getPosts';
import remapOptions from './utils/remapOptions';

const defaultOptions = {};

const marq = (options = defaultOptions) => {
// Todo: Actually parse options into arguments for the functions below
getPosts(options).then(posts => {
generate()(posts).then(r =>
console.log(`marq generated ${r.length} posts into ./posts`)
);
const config = remapOptions(options);
return new Promise((resolve, reject) => {
getPosts(config.query)
.then(posts => {
const o = {
dest: config.dest,
template: config.template,
};
generate(o)(posts)
.then(r => {
console.log(`marq generated ${r.length} posts into ${config.dest}`);
return resolve(r);
})
.catch(err => {
return reject(err);
});
})
.catch(err => {
return reject(err);
});
});
};

Expand Down
23 changes: 16 additions & 7 deletions src/mapDataToProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@ const mapDataToProps = data => {
if (!isValidPost(data)) return false;

const publishDate = getDate(data.publish_date);
const slug = slugify(data.slug);
const slug = slugify(data.slug.replace('blog/', ''));
const title = data.html_title;
const description = data.meta_description;

return {
const featuredImage = data.featured_image
? {
src: data.featured_image,
alt: data.featured_image_alt_text,
height: data.featured_image_height,
width: data.featured_image_width,
}
: null;

const marqData = {
id: data.id,
date: publishDate,
author: {
id: data.author_user_id,
username: data.author_username,
name: data.author_name,
},
content: data.post_body,
fileName: getFileName(publishDate, slug),
featuredImage,
title,
description,
slug,
Expand All @@ -29,6 +34,10 @@ const mapDataToProps = data => {
description: sanitize(description),
},
};

data.marq = marqData;

return data;
};

export default mapDataToProps;
35 changes: 25 additions & 10 deletions src/savePost.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
import fs from 'fs';
import mkdir from 'mkdirp';
import { isString } from 'lodash';
import { isObject, isString } from 'lodash';
import generatePost from './generatePost';
import mapDataToProps from './mapDataToProps';
import { isValidPost } from './utils';
import template from './template/post.js';

const defaultDir = './posts';
const defaultOptions = {
dest: './posts',
template: template,
};

const savePost = (options = defaultOptions) => post => {
return new Promise((resolve, reject) => {
if (!isObject(options)) {
reject('marq: Options needs to be an object');
}
if (!isValidPost(post)) {
reject("marq: Hmm… This post doesn't appear to be correct.");
}

const savePost = (dir = defaultDir) => post => {
if (!isString(dir)) return false;
if (!isValidPost(post)) return false;
const config = Object.assign({}, defaultOptions, options);
const dest = config.dest;
const template = config.template;

const props = mapDataToProps(post);
const markdown = generatePost(props);
const filePath = `${dir}/${props.fileName}`;
if (!isString(dest) || !isString(template)) {
reject("marq: Hmm… Looks like something's up with the configuration.");
}

return new Promise((resolve, reject) => {
mkdir(dir, err => {
const props = mapDataToProps(post);
const markdown = generatePost(template)(props);
const filePath = `${dest}/${props.marq.fileName}`;
mkdir(dest, err => {
/* istanbul ignore next */
// skipping testing for mkdir's promise reject
if (err) return reject(err);
Expand Down
10 changes: 5 additions & 5 deletions src/template/post.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const template = `---
title: "<%= front_matter.title %>"
description: "<%= front_matter.description %>"
date: "<%= date %>"
slug: "<%= slug %>"
title: "<%= marq.front_matter.title %>"
description: "<%= marq.front_matter.description %>"
date: "<%= marq.date %>"
slug: "<%= marq.slug %>"
---
<%= content %>
<%= marq.content %>
`;

export default template;
28 changes: 28 additions & 0 deletions src/utils/remapOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { has, isObject } from 'lodash';
import defaultTemplate from '../template/post';

const remapOptions = (options = {}) => {
let output = {};
if (!isObject(options)) return output;
if (!has(options, 'hubspot')) return output;

const { hubspot, dest, template } = options;

const hapikey = hubspot.key;
const content_group_id = hubspot.blogId;

if (!hapikey || !content_group_id) return output;

output = {
query: {
hapikey,
content_group_id,
},
dest: dest ? dest : './_posts/',
template: template ? template : defaultTemplate,
};

return output;
};

export default remapOptions;
2 changes: 1 addition & 1 deletion test/fixture/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const post = {
author_user: 'Ms. Author',
meta_description: 'This is so meta',
post_body: '<strong>Super meta!</strong>',
slug: 'awesome-post!!!!!!',
slug: '/blog/awesome-post!!!!!!',
};

export default post;

0 comments on commit b48a536

Please sign in to comment.