Something went wrong on our end
init-ghost.js 4.36 KiB
// eslint-disable-next-line @typescript-eslint/no-var-requires
const _ = require('lodash');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const tagsData = require('./ghost/migrations/init/tags.json');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const postsData = require('./ghost/migrations/init/posts.json');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const GhostAdminAPI = require('@tryghost/admin-api');
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('dotenv').config({ path: path.resolve(__dirname, '../.env') });
var api = new GhostAdminAPI({
url: process.env.GHOST_HOST_AND_PORT,
key: process.env.GHOST_ADMIN_API_KEY,
version: 'v3',
});
async function deleteTags(existingTags) {
return await Promise.all(
_.forEach(existingTags, async (tag) => {
api.tags
.delete(_.pick(tag, ['id']))
.then((res) => {
return null;
})
.catch((error) => console.error(error));
})
);
}
async function deletePosts(existingPosts) {
return await Promise.all(
_.forEach(existingPosts, async (tag) => {
api.posts
.delete(_.pick(tag, ['id']))
.then((res) => {
return null;
})
.catch((error) => console.error(error));
})
);
}
async function createTags(deleteOnly) {
// Get existing tags
await api.tags
.browse({ limit: 'all' })
.then(async (existingTags) => {
// remove 'meta' key
delete existingTags['meta'];
if (existingTags.length > 0) {
console.log('-- Dropping ' + existingTags.length + ' tags... --');
// Delete existing tags
await deleteTags(existingTags).then(() => {
console.log('-- Tags dropped --');
});
} else {
console.log('-- No tag to drop --');
}
// Creating new tags
if (!deleteOnly) {
console.log('-- Creating ' + tagsData.tags.length + ' tags --');
_.forEach(tagsData.tags, (tag) => {
api.tags
.add(tag)
.then((res) => {
console.log('-- Tag `' + res.name + '` created --');
})
.catch((error) => console.error(error));
});
}
})
.catch((error) => console.error(error));
}
// Utility function to find and upload any images in an HTML string
function processImagesInHTML(html) {
// Find images that Ghost Upload supports
let imageRegex = /="([^"]*?(?:\.jpg|\.jpeg|\.gif|\.png|\.svg|\.sgvz))"/gim;
let imagePromises = [];
let result;
while ((result = imageRegex.exec(html)) !== null) {
let file = result[1];
// Upload the image, using the original matched filename as a reference
imagePromises.push(
api.images.upload({
ref: file,
file: path.resolve(file),
})
);
}
return Promise.all(imagePromises).then((images) => {
images.forEach((image) => (html = html.replace(image.ref, image.url)));
return html;
});
}
async function createPosts(deleteOnly) {
// Get existing posts
api.posts
.browse({ limit: 'all' })
.then(async (existingPosts) => {
// remove 'meta' key
delete existingPosts['meta'];
if (existingPosts.length > 0) {
console.log('-- Dropping ' + existingPosts.length + ' posts... --');
// Delete existing posts
await deletePosts(existingPosts).then(() => {
console.log('-- Posts dropped --');
});
} else {
console.log('-- No posts to drop --');
}
// Creating new posts
if (!deleteOnly) {
console.log('-- Creating ' + postsData.length + ' posts --');
_.forEach(postsData, (post) => {
api.posts
.add(post, { source: 'html' })
.then((res) => {
console.log('-- Post `' + res.title + '` created --');
})
.catch((error) => console.error(error));
});
}
})
.catch((error) => console.error(error));
}
async function main(deleteOnly) {
createTags(deleteOnly).then(() => {
createPosts(deleteOnly);
});
}
var myArgs = process.argv.slice(2);
switch (myArgs[0]) {
case 'drop':
console.log('-- Droping data --');
main(true);
break;
case 'up':
console.log('-- Init db --');
main(false);
break;
default:
console.error('Unknown cmd');
}