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!)
This commit is contained in:
Hippo 2021-03-30 17:41:13 +05:30
parent 1d80daba64
commit a41b45c58f
2 changed files with 76 additions and 39 deletions

11
cli.js
View File

@ -140,8 +140,15 @@ program.command('fetch-medium <post_url>')
program.command('push-ghost <file>')
.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 <mediumUrl>')

104
seance.js
View File

@ -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,
}
};
}
/**