-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathforce-rerender.js
76 lines (59 loc) · 2.48 KB
/
force-rerender.js
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
/**
* Re-renders all posts and pages
*
* Usage:
*
* node force-rerender.js https://blah.ghost.io ADMIN_API_KEY - dry run
* node force-rerender.js https://blah.ghost.io ADMIN_API_KEY true - live run
*/
if (process.argv.length < 4) {
console.log('not enough arguments, provide an API url and admin key');
process.exit(1);
}
const Promise = require('bluebird');
const GhostAdminAPI = require('@tryghost/admin-api');
const url = process.argv[2];
const key = process.argv[3];
(async function main() {
const doRerender = process.argv[4] === 'true';
if (doRerender) {
console.log('REAL Run');
} else {
console.log('Dry Run - nothing will be re-rendered');
}
// Give the user time to read...
await Promise.delay(1000);
const api = new GhostAdminAPI({
url,
key,
version: 'canary'
});
try {
const allPosts = await api.posts.browse({fields: 'id,slug,updated_at', limit: 'all'});
const allPages = await api.pages.browse({fields: 'id,slug,updated_at', limit: 'all'});
console.log(`${allPosts.length} Posts and ${allPages.length} Pages will be re-rendered`);
if (doRerender) {
const postsResult = await Promise.mapSeries(allPosts, async (post) => {
console.log(`Re-rendering post ${post.slug} (${post.id})`);
// missing data attributes won't be changed
// updated_at is required to pass collision detection
const postData = {id: post.id, updated_at: post.updated_at};
await api.posts.edit(postData, {force_rerender: true});
// return `true` rather than the api response to avoid putting all post contents into memory
return Promise.delay(50).return(true);
});
console.log(`\nRe-rendered ${postsResult.length} posts\n`);
await Promise.delay(1000);
const pagesResult = await Promise.mapSeries(allPages, async (page) => {
console.log(`Re-rendering page ${page.slug} (${page.id})`);
const pageData = {id: page.id, updated_at: page.updated_at};
await api.pages.edit(pageData, {force_rerender: true});
return Promise.delay(50).return(true);
});
console.log(`\nRe-rendered ${pagesResult.length} pages`);
}
} catch (err) {
console.error('There was an error', require('util').inspect(err, false, null));
process.exit(1);
}
})();