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

feat: Adding support for yaml as documented in the README #503

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
55 changes: 26 additions & 29 deletions lib/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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);
}
};
7 changes: 3 additions & 4 deletions lib/get-resume.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 7 additions & 3 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ const normalizeTheme = (value, defaultValue) => {
'Used by `serve` to indicate a public directory path.',
'public',
)
.option(
'-T, --type <mime type>',
'Specify the mime type of the resume file.',
)
.option(
'--schema <relativePath>',
'Used by `validate` to validate against a custom schema.',
Expand All @@ -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({
Expand All @@ -92,7 +96,7 @@ const normalizeTheme = (value, defaultValue) => {
'Export locally to .html or .pdf. Supply a --format <file 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) => {
Expand All @@ -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,
});
Expand Down
1 change: 1 addition & 0 deletions lib/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ describe('cli configuration', () => {
browser auto or not. (default: false)
-d, --dir <path> Used by \`serve\` to indicate a public
directory path. (default: \\"public\\")
-T, --type <mime type> Specify the mime type of the resume file.
--schema <relativePath> Used by \`validate\` to validate against a
custom schema.
-h, --help display help for command
Expand Down
12 changes: 6 additions & 6 deletions lib/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down