|
| 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 | +}); |
0 commit comments