Skip to content

Commit e7a14b3

Browse files
committed
Single post version of force rerender tool
1 parent 40c65fe commit e7a14b3

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

force-rerender-single.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Re-renders all posts and pages
3+
*
4+
* Usage:
5+
*
6+
* node force-rerender-single.js https://blah.ghost.io ADMIN_API_KEY slug - dry run
7+
* node force-rerender-single.js https://blah.ghost.io ADMIN_API_KEY slug true - live run
8+
*/
9+
10+
if (process.argv.length < 4) {
11+
console.log('not enough arguments, provide an API url and admin key');
12+
process.exit(1);
13+
}
14+
15+
const Promise = require('bluebird');
16+
const GhostAdminAPI = require('@tryghost/admin-api');
17+
18+
const url = process.argv[2];
19+
const key = process.argv[3];
20+
const slug = process.argv[4];
21+
22+
(async function main() {
23+
const doRerender = process.argv[5] === 'true';
24+
25+
if (doRerender) {
26+
console.log('REAL Run');
27+
} else {
28+
console.log('Dry Run - nothing will be re-rendered');
29+
}
30+
31+
// Give the user time to read...
32+
await Promise.delay(1000);
33+
34+
const api = new GhostAdminAPI({
35+
url,
36+
key,
37+
version: 'canary'
38+
});
39+
40+
try {
41+
const post = await api.posts.read({slug}, {fields: 'id,slug,updated_at'});
42+
43+
console.log(`${post.slug} will be re-rendered`);
44+
45+
if (doRerender) {
46+
console.log(`Re-rendering post ${post.slug} (${post.id})`);
47+
48+
// missing data attributes won't be changed
49+
// updated_at is required to pass collision detection
50+
const postData = {id: post.id, updated_at: post.updated_at};
51+
await api.posts.edit(postData, {force_rerender: true});
52+
53+
console.log(`\nRe-rendered ${post.slug} \n`);
54+
}
55+
} catch (err) {
56+
console.error('There was an error', require('util').inspect(err, false, null));
57+
process.exit(1);
58+
}
59+
})();

0 commit comments

Comments
 (0)