From c428cd574cfa5d5ddec70c3632d4182a9b661e36 Mon Sep 17 00:00:00 2001 From: Will Boyd Date: Sun, 12 Jan 2020 13:50:50 -0500 Subject: [PATCH] Display command to rerun --- index.js | 1 + src/parser.js | 13 ++++++++----- src/wizard.js | 50 ++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index e010138..deec646 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ const writer = require('./src/writer'); let config = await wizard.getConfig(); let posts = await parser.parseFilePromise(config) await writer.writeFilesPromise(posts, config); + wizard.displayCommand(config); })().catch(ex => { console.error(ex); }); diff --git a/src/parser.js b/src/parser.js index e5eb0ab..52b4f8b 100644 --- a/src/parser.js +++ b/src/parser.js @@ -60,17 +60,20 @@ function getPostId(post) { return post.post_id[0]; } +function getPostSlug(post) { + return post.post_name[0]; +} + function getPostCoverImageId(post) { - if (post.postmeta === undefined) return; + if (post.postmeta === undefined) { + return undefined; + } + let postmeta = post.postmeta.find(postmeta => postmeta.meta_key[0] === '_thumbnail_id'); let id = postmeta ? postmeta.meta_value[0] : undefined; return id; } -function getPostSlug(post) { - return post.post_name[0]; -} - function getPostTitle(post) { return post.title[0]; } diff --git a/src/wizard.js b/src/wizard.js index 1de484b..867ec97 100644 --- a/src/wizard.js +++ b/src/wizard.js @@ -68,18 +68,25 @@ async function getConfig() { extendOptionsData(); const program = parseCommandLine(process.argv); - let questions = options.map(option => ({ - when: option.name !== 'wizard' && program.wizard && !option.isProvided, - name: camelcase(option.name), - type: option.prompt, - message: option.description + '?', - default: option.default, - - // these are not used for all option types and that's fine - filter: option.coerce, - validate: option.validate - })); - let answers = await inquirer.prompt(questions); + let answers; + if (program.wizard) { + console.log('\nStarting wizard...'); + let questions = options.map(option => ({ + when: option.name !== 'wizard' && !option.isProvided, + name: camelcase(option.name), + type: option.prompt, + message: option.description + '?', + default: option.default, + + // these are not used for all option types and that's fine + filter: option.coerce, + validate: option.validate + })); + answers = await inquirer.prompt(questions); + } else { + console.log('\nSkipping wizard...'); + answers = {}; + } const config = { ...program.opts(), ...answers }; return config; @@ -114,8 +121,8 @@ function parseCommandLine(argv) { .name('node index.js') .helpOption('-h, --help', 'See the thing you\'re looking at right now') .on('--help', () => { - console.log('\nMore documentation at https://github.com/lonekorean/wordpress-export-to-markdown'); - }) + console.log('\nMore documentation is at https://github.com/lonekorean/wordpress-export-to-markdown'); + }); options.forEach(input => { const flag = '--' + input.name + ' <' + input.type + '>'; @@ -150,4 +157,19 @@ function validateFile(value) { return isValid ? true : 'Unable to find file: ' + path.resolve(value); } +function displayCommand(config) { + let command = 'node index.js --wizard=false'; + options.forEach(option => { + if (option.name !== 'wizard') { + let configKey = camelcase(option.name); + let configValue = config[configKey]; + if (configValue !== option.default) { + command += ' --' + option.name + '=' + configValue; + } + } + }); + console.log('\nTo skip the wizard and rerun with the same options, run this:\n' + command); +} + exports.getConfig = getConfig; +exports.displayCommand = displayCommand;