Skip to content

Commit 7f2b9f8

Browse files
committed
feat(migrate): create function for temp files from page payloads
* Set up settings json file * Create remove temp files function
1 parent 73ef655 commit 7f2b9f8

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

utils/migrate/config/settings.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"slugs": [
3+
"about-hero-body",
4+
"about-hero-button",
5+
"about-history-sectionone-body",
6+
"about-history-sectionone-button",
7+
"about-history-sectionthree-body",
8+
"about-history-sectiontwo-body",
9+
"about-position-body",
10+
"about-team-directors-header",
11+
"about-team-header",
12+
"about-team-staff-header",
13+
"footer-socials",
14+
"home-community",
15+
"home-hero-body",
16+
"home-hero-button-left",
17+
"home-hero-button-right",
18+
"home-opensource",
19+
"home-opensource-button",
20+
"home-pillars-button",
21+
"home-pillars-conferences",
22+
"home-support"
23+
]
24+
}

utils/migrate/index.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* eslint-disable no-console */
2+
3+
import { readFileSync, rmSync, writeFileSync } from 'fs';
4+
import { join } from 'path';
5+
import 'dotenv/config';
6+
import GhostAdminAPI from '@tryghost/admin-api';
7+
8+
const STAGING = 'staging';
9+
const PRODUCTION = 'production';
10+
11+
const clientStaging = new GhostAdminAPI({
12+
url: process.env.STAGING_API_URL,
13+
key: process.env.STAGING_API_KEY_ADMIN,
14+
version: 'v5.126.0',
15+
});
16+
17+
const clientProduction = new GhostAdminAPI({
18+
url: process.env.PRODUCTION_API_URL,
19+
key: process.env.PRODUCTION_API_KEY_ADMIN,
20+
version: 'v5.126.0',
21+
});
22+
23+
const getPages = (target) => {
24+
const normalizedTarget = target.toLowerCase();
25+
const client =
26+
normalizedTarget === STAGING ? clientStaging : clientProduction;
27+
const dataPath = join(process.cwd(), `tmp/${normalizedTarget}.json`);
28+
const settingsPath = join(process.cwd(), 'config/settings.json');
29+
const settings = JSON.parse(readFileSync(settingsPath));
30+
const targetSlugs = settings.slugs.join(',');
31+
32+
console.log(`Fetching data from ${normalizedTarget} ...\n`);
33+
34+
return new Promise((resolve, reject) => {
35+
client.pages
36+
.browse({ filter: `slug:[${targetSlugs}]` })
37+
.then((pages) => {
38+
const jsonString = `{ "pages": ${JSON.stringify(pages)} }`;
39+
writeFileSync(dataPath, jsonString, { flag: 'a' });
40+
resolve();
41+
})
42+
.catch((e) => reject(e));
43+
});
44+
};
45+
46+
const cleanTmpFile = (target) => {
47+
const relativePath = `tmp/${target.toLowerCase()}.json`;
48+
49+
console.log(`Removing "${relativePath}"...\n`);
50+
rmSync(join(process.cwd(), relativePath));
51+
};
52+
53+
getPages(STAGING).then(() => {
54+
getPages(PRODUCTION)
55+
.then(() => {
56+
cleanTmpFile(STAGING);
57+
})
58+
.then(() => {
59+
cleanTmpFile(PRODUCTION);
60+
});
61+
});

utils/migrate/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"keywords": [],
1010
"author": "Ruby Central",
1111
"license": "private",
12+
"type": "module",
1213
"dependencies": {
1314
"@tryghost/admin-api": "^1.13.17",
1415
"dotenv": "^16.5.0"

utils/migrate/tmp/.keep

Whitespace-only changes.

0 commit comments

Comments
 (0)