diff --git a/lib/builder.js b/lib/builder.js index efca364e..4f4e0dd8 100644 --- a/lib/builder.js +++ b/lib/builder.js @@ -3,7 +3,8 @@ const themeServer = const fs = require('fs'); const request = require('superagent'); const chalk = require('chalk'); -const renderHtml = require('./render-html'); +const renderHtml = require('./render-html').default; +const getResume = require('./get-resume.js').default; const denormalizeTheme = (value) => { return value.match(/jsonresume-theme-(.*)/)[1]; @@ -34,33 +35,29 @@ const sendExportHTML = (resumeJson, theme, callback) => { } }); }; -module.exports = function resumeBuilder(theme, dir, resumeFilename, cb) { - fs.readFile(resumeFilename, async (err, resumeJson) => { - if (err) { - console.log(chalk.yellow('Could not find:'), resumeFilename); - console.log( - chalk.cyan('Using example resume.json from resume-schema instead...'), - ); - resumeJson = require('resume-schema').resumeJson; - } else { - try { - // todo: test resume schema - resumeJson = JSON.parse(resumeJson); - } catch (e) { - err = 'Parse error: ' + resumeFilename; - return cb(err); - } - } +module.exports = async function resumeBuilder(theme, dir, resumeFilename, mime, cb) { + let resume; + try { + resume = await getResume({ + path: resumeFilename, + mime + }) + } catch (e) { + console.warn(e.message); + console.log( + chalk.cyan('Using example resume.json from resume-schema instead...'), + ); + resume = require('resume-schema').resumeJson; + } - try { - const html = await renderHtml({ resume: resumeJson, themePath: theme }); - cb(null, html); - } catch (err) { - console.log(err); - console.log( - chalk.yellow('Could not run the render function from local theme.'), - ); - sendExportHTML(resumeJson, theme, cb); - } - }); + try { + const html = await renderHtml({ resume: resume, themePath: theme }); + cb(null, html); + } catch (err) { + console.log(err); + console.log( + chalk.yellow('Could not run the render function from local theme.'), + ); + sendExportHTML(resume, theme, cb); + } }; diff --git a/lib/get-resume.js b/lib/get-resume.js index fd4f2a2f..c0e22dae 100644 --- a/lib/get-resume.js +++ b/lib/get-resume.js @@ -13,17 +13,16 @@ const parsers = { 'text/yaml': (string) => yaml.load(string), 'application/json': (string) => JSON.parse(string), }; -export default async ({ path, mime: inputMime }) => { +export default async ({ path, mime }) => { let input; - let mime; if ('-' === path) { - mime = inputMime || lookup('.json'); + mime = mime || lookup('.json'); input = process.stdin; } else if (path && (await stat(path)).isDirectory()) { return quaff(path); } if (!input) { - mime = inputMime || lookup(path); + mime = mime || lookup(path); input = createReadStream(resolvePath(process.cwd(), path)); } if (!input) { diff --git a/lib/main.js b/lib/main.js index e72db6fa..693847b3 100755 --- a/lib/main.js +++ b/lib/main.js @@ -57,6 +57,10 @@ const normalizeTheme = (value, defaultValue) => { 'Used by `serve` to indicate a public directory path.', 'public', ) + .option( + '-T, --type ', + 'Specify the mime type of the resume file.', + ) .option( '--schema ', 'Used by `validate` to validate against a custom schema.', @@ -73,7 +77,7 @@ const normalizeTheme = (value, defaultValue) => { .command('validate') .description("Validate your resume's schema") .action(async () => { - const resume = await getResume({ path: program.resume }); + const resume = await getResume({ path: program.resume, mime: program.mime }); const schema = await getSchema({ path: program.schema }); try { await validate({ @@ -92,7 +96,7 @@ const normalizeTheme = (value, defaultValue) => { 'Export locally to .html or .pdf. Supply a --format flag and argument to specify export format.', ) .action(async (fileName) => { - const resume = await getResume({ path: program.resume }); + const resume = await getResume({ path: program.resume, mime: program.mime }); exportResume( { ...program, resume, fileName }, (err, fileName, format) => { @@ -112,7 +116,7 @@ const normalizeTheme = (value, defaultValue) => { .command('serve') .description('Serve resume at http://localhost:4000/') .action(async () => { - serve({ + await serve({ ...program, resumeFilename: program.resume, }); diff --git a/lib/main.test.js b/lib/main.test.js index b932b93d..ffcd5c87 100644 --- a/lib/main.test.js +++ b/lib/main.test.js @@ -70,6 +70,7 @@ describe('cli configuration', () => { browser auto or not. (default: false) -d, --dir Used by \`serve\` to indicate a public directory path. (default: \\"public\\") + -T, --type Specify the mime type of the resume file. --schema Used by \`validate\` to validate against a custom schema. -h, --help display help for command diff --git a/lib/serve.js b/lib/serve.js index b98f2873..60ae477d 100644 --- a/lib/serve.js +++ b/lib/serve.js @@ -5,8 +5,8 @@ const bs = require('browser-sync').create(); const builder = require('./builder'); -const reBuildResume = (theme, dir, resumeFilename, cb) => { - builder(theme, dir, resumeFilename, (err, html) => { +const reBuildResume = async (theme, dir, resumeFilename, mime, cb) => { + await builder(theme, dir, resumeFilename, mime, (err, html) => { if (err) { readline.cursorTo(process.stdout, 0); console.log(err); @@ -22,17 +22,17 @@ const reBuildResume = (theme, dir, resumeFilename, cb) => { }); }; -module.exports = function ({ port, theme, silent, dir, resumeFilename }) { +module.exports = async function ({ port, theme, silent, dir, resumeFilename, type }) { if (!fs.existsSync(dir)) { fs.mkdirSync(dir); } - bs.watch(resumeFilename).on('change', () => { - reBuildResume(theme, dir, resumeFilename, () => { + bs.watch(resumeFilename).on('change', async () => { + await reBuildResume(theme, dir, resumeFilename, type, () => { bs.reload(); }); }); - reBuildResume(theme, dir, resumeFilename, () => { + await reBuildResume(theme, dir, resumeFilename, type, () => { bs.init({ server: dir, port: port, diff --git a/package.json b/package.json index adf43f1d..c6992698 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "resume-cli", "version": "0.0.0-development", "description": "The JSON Resume command line interface", - "main": "index.js", + "main": "build/main.js", "engines": { "node": ">=10.18.1" },