Skip to content

Commit

Permalink
Refactor to allow library to be used externally
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcL committed Nov 6, 2019
1 parent 54a4330 commit bdec6f3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 27 deletions.
38 changes: 13 additions & 25 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env node
const yargs = require('yargs');
const chalk = require('chalk');
const markdownToHtmlEmail = require('../src/markdownToHtmlEmail');
const createMailchimpCampaign = require('../src/createMailchimpCampaign');
const convertAndCreateCampaign = require('../src');

const logError = error => console.error(chalk.bold.red(`❌ ${error}`));
const logSuccess = message => console.log(chalk.green(`✅ ${message}`));
Expand Down Expand Up @@ -49,31 +48,20 @@ const { argv } = yargs
.help('h')
.alias('h', 'help');

const convertAndCreateCampaign = async args => {
try {
const emailData = await markdownToHtmlEmail(args);

const { apikey: apiKey, listid: listId } = args;
const options = {
apiKey,
listId,
...emailData,
};
convertAndCreateCampaign(argv)
.then(data => {
const { email, campaign } = data;
if (email.errors.length > 0) {
throw new Error(email.errors.toString());
}

logSuccess('Created email data');
logSuccess('Email created');

const campaignData = await createMailchimpCampaign(options);
if (!campaignData) {
logError('No Mailchimp campaign created');
} else {
const { web_id: id } = campaignData;
if (campaign) {
const { web_id: id } = campaign;
const editUrl = 'https://admin.mailchimp.com/campaigns/edit';

logSuccess(`Mailchimp campaign created - ${editUrl}?id=${id}`);
logSuccess(`Mailchimp campaign: ${editUrl}?id=${id}`);
}
} catch (error) {
console.error(error.toString());
}
};

convertAndCreateCampaign(argv);
})
.catch(error => logError(error));
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "markdown-to-mailchimp",
"version": "0.1.0",
"main": "index.js",
"main": "./src/index.js",
"bin": {
"md2mc": "./bin/cli.js"
},
Expand Down
2 changes: 1 addition & 1 deletion src/createMailchimpCampaign.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const createMailchimpCampaign = async options => {
const { apiKey, listId, frontmatter, html } = options;

if (!apiKey || !listId) {
return false;
return null;
}

const campaignOptions = {
Expand Down
28 changes: 28 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const markdownToHtmlEmail = require('./markdownToHtmlEmail');
const createMailchimpCampaign = require('./createMailchimpCampaign');

const convertAndCreateCampaign = async args => {
try {
const emailData = await markdownToHtmlEmail(args);

const { apikey: apiKey, listid: listId } = args;
const options = {
apiKey,
listId,
...emailData,
};

const campaignData = await createMailchimpCampaign(options);

return {
email: emailData,
campaign: campaignData,
};
} catch (error) {
return {
error: error.toString(),
};
}
};

module.exports = convertAndCreateCampaign;

0 comments on commit bdec6f3

Please sign in to comment.