-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
76 lines (63 loc) · 2.36 KB
/
index.js
File metadata and controls
76 lines (63 loc) · 2.36 KB
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
/* eslint-disable no-console */
import { readFileSync, rmSync, writeFileSync } from 'fs';
import { join } from 'path';
import 'dotenv/config';
import GhostAdminAPI from '@tryghost/admin-api';
const STAGING = 'staging';
const PRODUCTION = 'production';
const clientStaging = new GhostAdminAPI({
url: process.env.STAGING_API_URL,
key: process.env.STAGING_API_KEY_ADMIN,
version: 'v5.126.0',
});
const clientProduction = new GhostAdminAPI({
url: process.env.PRODUCTION_API_URL,
key: process.env.PRODUCTION_API_KEY_ADMIN,
version: 'v5.126.0',
});
const getPages = (target) => {
const normalizedTarget = target.toLowerCase();
const client =
normalizedTarget === STAGING ? clientStaging : clientProduction;
const dataPath = join(process.cwd(), `tmp/${normalizedTarget}.json`);
const settingsPath = join(process.cwd(), 'config/settings.json');
const settings = JSON.parse(readFileSync(settingsPath));
const targetSlugs = settings.slugs.join(',');
console.log(`Fetching data from ${normalizedTarget} ...\n\n`);
return new Promise((resolve, reject) => {
client.pages
.browse({ filter: `slug:[${targetSlugs}]`, limit: 1000 })
.then((pages) => {
const jsonString = `{ "pages": ${JSON.stringify(pages)} }`;
writeFileSync(dataPath, jsonString, { flag: 'a' });
resolve();
})
.catch((e) => reject(e));
});
};
const cleanTmpFile = (target) => {
const relativePath = `tmp/${target.toLowerCase()}.json`;
console.log(`Removing "${relativePath}"...\n\n`);
rmSync(join(process.cwd(), relativePath));
};
const moveToProduction = () => {
const stagingDataPath = join(process.cwd(), 'tmp/staging.json');
const stagingData = JSON.parse(readFileSync(stagingDataPath)).pages;
console.log(`Adding page copies from ${STAGING} to ${PRODUCTION}...\n\n`);
return new Promise((resolve) => {
stagingData.forEach((stagingPage) => {
clientProduction.pages.add({
lexical: stagingPage.lexical,
status: 'published',
title: stagingPage.title,
published_at: stagingPage.published_at,
});
resolve();
});
});
};
getPages(STAGING).then(() => {
moveToProduction().then(() => {
cleanTmpFile(STAGING);
});
});