Skip to content

Commit

Permalink
feat(theme-as-promise): allow theme to render with promise
Browse files Browse the repository at this point in the history
  • Loading branch information
pinage404 committed Aug 18, 2018
1 parent 958973d commit d7528ad
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 47 deletions.
33 changes: 5 additions & 28 deletions lib/builder/index.js
Expand Up @@ -3,7 +3,7 @@ var themeServer = process.env.THEME_SERVER || 'https://themes.jsonresume.org/the
var fs = require('fs');
var request = require('superagent');
var chalk = require('chalk');
var path = require('path');
var renderHtml = require("../render-html");

function sendExportHTML(resumeJson, theme, callback) {

Expand All @@ -29,8 +29,7 @@ function sendExportHTML(resumeJson, theme, callback) {
}

module.exports = function resumeBuilder(theme, dir, resumeFilename, cb) {

fs.readFile(resumeFilename, function(err, resumeJson) {
fs.readFile(resumeFilename, async function(err, resumeJson) {
if (err) {
console.log(chalk.yellow('Could not find:'), resumeFilename);
console.log(chalk.cyan('Using example resume.json from resume-schema instead...'));
Expand All @@ -45,34 +44,12 @@ module.exports = function resumeBuilder(theme, dir, resumeFilename, cb) {
}
}

var packageJson = {};

try {
packageJson = require(path.join(process.cwd(), 'package'));
} catch(e) {
// 'package' module does not exist
}

var render;
try {
render = require(path.join(process.cwd(), packageJson.main || 'index')).render;
} catch(e) {
// The file does not exist.
}

if(render && typeof render === 'function') {
try {
var rendered = render(resumeJson);
return typeof rendered.then === 'function' // check if it's a promise
? rendered.then(cb.bind(null, null), cb)
: cb(null, rendered);
} catch (e) {
return cb(e);
}
} else {
const html = await renderHtml(resumeJson, theme);
cb(null, html);
} catch (err) {
console.log(chalk.yellow('Could not run the render function from local theme.'));
sendExportHTML(resumeJson, theme, cb);
}

});
};
30 changes: 11 additions & 19 deletions lib/export-resume/index.js
Expand Up @@ -8,14 +8,12 @@ var read = require('read');
var spinner = require("char-spinner");
var chalk = require('chalk');
var puppeteer = require('puppeteer');
var renderHtml = require("../render-html");

var SUPPORTED_FILE_FORMATS = ["html", "pdf"];

module.exports = function exportResume(resumeJson, fileName, program, callback) {
var theme = program.theme;
if(!theme.match('jsonresume-theme-.*')){
theme = 'jsonresume-theme-' + theme;
}

if (!fileName) {
console.error("Please enter a export destination.");
Expand Down Expand Up @@ -51,32 +49,26 @@ function extractFileFormat(fileName) {
return fileName.substring(dotPos + 1).toLowerCase();
}

function createHtml(resumeJson, fileName, theme, format, callback) {
var html = renderHtml(resumeJson, theme);
var stream = fs.createWriteStream(path.resolve(process.cwd(), fileName + format));

stream.write(html, function() {
stream.close(callback);
});

}

function renderHtml(resumeJson, theme){
var contents = '';
async function createHtml(resumeJson, fileName, theme, format, callback) {
try {
var themePkg = require(theme);
const html = await renderHtml(resumeJson, theme);
var stream = fs.createWriteStream(
path.resolve(process.cwd(), fileName + format)
);

stream.write(html, function() {
stream.close(callback);
});
} catch (err) {
// Theme not installed
console.log('You have to install this theme globally to use it e.g. `npm install -g ' + theme + '`')
process.exit();
}
contents = themePkg.render(resumeJson);
return contents;
}

function createPdf(resumeJson, fileName, theme, format, callback) {
(async () => {
var html = renderHtml(resumeJson, theme);
const html = await renderHtml(resumeJson, theme);
const browser = await puppeteer.launch();
const page = await browser.newPage();

Expand Down
20 changes: 20 additions & 0 deletions lib/render-html/index.js
@@ -0,0 +1,20 @@
const path = require('path')
const chalk = require('chalk')

module.exports = async function renderHtml(resumeJson, theme) {
if (!theme.match('jsonresume-theme-.*')) {
theme = 'jsonresume-theme-' + theme
}

const themePath = path.resolve(process.cwd(), 'node_modules', theme)
const render = require(themePath).render

if (typeof render !== 'function') {
console.log(
chalk.yellow('Could not run the render function from local theme.'),
)
return Promise.reject(new Error(`Can't render with "${theme}"'s theme`))
}

return render(resumeJson)
}
40 changes: 40 additions & 0 deletions test/renderHtml.js
@@ -0,0 +1,40 @@
const fs = require('fs');
const path = require('path');
const should = require('should');
const renderHtml = require('../lib/render-html');

describe('renderHtml', () => {
const resumeJson = {
basics: {
name: 'test',
label: 'Programmer',
email: 'test4@test.com',
},
};

it('should reject when theme is not availlable', async () => {
const theme = 'unkown';

const result = renderHtml(resumeJson, theme);

return result.should.be.rejected();
});

describe('should render html when theme is availlable', () => {
it('with long theme name', async () => {
const theme = 'jsonresume-theme-flat';

const html = await renderHtml(resumeJson, theme);

return html.should.startWith('<!doctype html>');
});

it('with short theme name', async () => {
const theme = 'flat';

const html = await renderHtml(resumeJson, theme);

return html.should.startWith('<!doctype html>');
});
});
});

0 comments on commit d7528ad

Please sign in to comment.