Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// 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 uploadPostImage(imagePath)
{
let imagePromise = api.images.upload({
ref: imagePath,
file: path.resolve(imagePath)
});
return Promise.resolve(imagePromise).then((url) => {
return url.url;
}).catch((error) => {
console.error(error);
return null
})
}
async function createPosts(deleteOnly) {
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, async (post) => {
//upload de l'image en featured_image
if (post.feature_image) {
post.feature_image = await uploadPostImage(post.feature_image);
}
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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');
}