From a41b45c58fb087a9b1dcce6b2a818084f69a7192 Mon Sep 17 00:00:00 2001 From: Hippo Date: Tue, 30 Mar 2021 17:41:13 +0530 Subject: [PATCH] Add "dry run" options for push-to-ghost command These options make Seance go through the motions of processing a post without actually uploading images and/or pushing them to Ghost. This makes it easier to test out stuff for new features (or, more accurately, it reduces the need to clean up afterwards!) --- cli.js | 11 ++++-- seance.js | 104 +++++++++++++++++++++++++++++++++++------------------- 2 files changed, 76 insertions(+), 39 deletions(-) diff --git a/cli.js b/cli.js index 4f8ab25..06c2ee5 100755 --- a/cli.js +++ b/cli.js @@ -140,8 +140,15 @@ program.command('fetch-medium ') program.command('push-ghost ') .alias('push') .description('push a downloaded Medium post to Ghost') - .action((file) => { - seance.pushToGhost(file) + .option('-d, --dry-run', "full dry run: doesn't upload anything (same as --no-upload --no-push)") + .option('--no-upload', "partial dry run: don't upload images") + .option('--no-push', "partial dry run: don't push to ghost") + .action((file, o) => { + seance.pushToGhost(file, { + dryRun: o.dryRun, + noUpload: !o.upload, + noPush: !o.push, + }) }); program.command('medium-to-ghost ') diff --git a/seance.js b/seance.js index 6649c4a..0161ee9 100644 --- a/seance.js +++ b/seance.js @@ -110,15 +110,28 @@ class Seance { /** * function [pushToGhost] + * @description + * Pre-processes and uploads the given article to Ghost + * + * @param {Boolean} options.noUpload Skip uploading of images + * @param {Boolean} options.noPush Skip pushing to Ghost; just generate the file + * @param {Boolean} options.dryRun Combination of noUpload and noPush * @returns [string] status */ - async pushToGhost (postSlug) { + async pushToGhost (postSlug, options={}) { this.emit('update', { status: 'starting', message: 'Starting upload: ' + postSlug, loglevel: 'info' }) + if (!!options.dryRun) { + options.noUpload = true + options.noPush = true + } + + console.log('noUpload', options.noUpload) + // Decide working path var postFolder = path.resolve('content/' + postSlug) @@ -256,7 +269,9 @@ class Seance { uploadedImages.push(imageName) // Let's wait for the upload, just to avoid conflicts - await this.uploadDav(davPath, imagePath) + if (!options.noUpload) { + await this.uploadDav(davPath, imagePath) + } newLine = '![' + imageAlt + '](' + uploadedPath + '/' + imageName + ')' } @@ -304,7 +319,10 @@ class Seance { loglevel: 'info' }) - this.uploadDav(davPath, imagePath) + if (!options.noUpload) { + this.uploadDav(davPath, imagePath) + } + featuredImagePath = uploadedPath + '/' + imageName } } @@ -325,42 +343,54 @@ class Seance { loglevel: 'info' }) - let res = await this.ghostAdmin.posts.add({ - title: postMeta.title, - custom_excerpt: postMeta.subtitle || null, - tags: postMeta.tags, - authors: users, - html: markdown.toHTML(await fs.promises.readFile(postOutput, 'utf-8')), - feature_image: featuredImagePath - }, {source: 'html'}) - - // Check if user was added - if (res.primary_author.id == 1) { - this.emit('notification', { - message: `WARNING: The admin editor, "${res.primary_author.name}", is set as author for this post. If this is incorrect, there was some problem matching usernames. Please check and set it manually.`, + if (!options.noPush) { + let res = await this.ghostAdmin.posts.add({ + title: postMeta.title, + custom_excerpt: postMeta.subtitle || null, + tags: postMeta.tags, + authors: users, + html: markdown.toHTML(await fs.promises.readFile(postOutput, 'utf-8')), + feature_image: featuredImagePath + }, {source: 'html'}) + + // Check if user was added + if (res.primary_author.id == 1) { + this.emit('notification', { + message: `WARNING: The admin editor, "${res.primary_author.name}", is set as author for this post. If this is incorrect, there was some problem matching usernames. Please check and set it manually.`, + }) + } + + this.emit('update', { + status: 'progress', + progress: 100, // we don't know the percentage + message: 'Post conveyed successfully', + loglevel: 'info' }) + + return { + slug: res.slug, + id: res.id, + uuid: res.uuid, + preview_url: res.url, + primary_author: res.primary_author, + title: res.title, + subtitle: res.custom_excerpt, + status: res.status, + } + } else { + // just return without pushing to Ghost + return { + slug: postSlug, + id: 0, + uuid: 0, + preview_url: null, + primary_author: {}, + title: postMeta.title, + subtitle: postMeta.subtitle, + status: 'none', + } } - - this.emit('update', { - status: 'progress', - progress: 100, // we don't know the percentage - message: 'Post conveyed successfully', - loglevel: 'info' - }) - - console.log(res) - - return { - slug: res.slug, - id: res.id, - uuid: res.uuid, - preview_url: res.url, - primary_author: res.primary_author, - title: res.title, - subtitle: res.custom_excerpt, - status: res.status, - } - }; + } /**