|
| 1 | +const fs = require('fs-extra'); |
| 2 | +const got = require('got'); |
| 3 | +const get = require('lodash/get'); |
| 4 | +const semver = require('semver'); |
| 5 | +const FormData = require('form-data'); |
| 6 | +const {Cookie} = require('tough-cookie'); |
| 7 | + |
| 8 | +const {SystemError} = require('../../errors'); |
| 9 | + |
| 10 | +const bases = { |
| 11 | + 1: '/ghost/api/v0.1', |
| 12 | + 2: '/ghost/api/v2/admin', |
| 13 | + 3: '/ghost/api/v3/admin' |
| 14 | +}; |
| 15 | + |
| 16 | +function getBaseUrl(version, url) { |
| 17 | + const basePath = bases[semver.major(version)]; |
| 18 | + |
| 19 | + if (!basePath) { |
| 20 | + throw new SystemError(`Unsupported version: ${version}`); |
| 21 | + } |
| 22 | + |
| 23 | + return `${url.replace(/\/?$/, '')}${basePath}`; |
| 24 | +} |
| 25 | + |
| 26 | +async function isSetup(version, url) { |
| 27 | + const baseUrl = getBaseUrl(version, url); |
| 28 | + const {body} = await got('/authentication/setup/', {baseUrl, json: true}); |
| 29 | + return get(body, 'setup[0].status', false); |
| 30 | +} |
| 31 | + |
| 32 | +async function setup(version, url, data) { |
| 33 | + const baseUrl = getBaseUrl(version, url); |
| 34 | + const {name, email, password, blogTitle} = data; |
| 35 | + const body = { |
| 36 | + setup: [{name, email, password, blogTitle}] |
| 37 | + }; |
| 38 | + |
| 39 | + await got.post('/authentication/setup/', {baseUrl, body, json: true}); |
| 40 | +} |
| 41 | + |
| 42 | +async function getAuthOpts(version, url, {username, password}) { |
| 43 | + const baseUrl = getBaseUrl(version, url); |
| 44 | + |
| 45 | + if (semver.major(version) === 1) { |
| 46 | + const {body: configBody} = await got('/configuration/', {baseUrl, json: true}); |
| 47 | + const {clientId, clientSecret} = get(configBody, 'configuration[0]', {}); |
| 48 | + const {body: authBody} = await got.post('/authentication/token/', { |
| 49 | + baseUrl, |
| 50 | + json: true, |
| 51 | + form: true, |
| 52 | + body: { |
| 53 | + grant_type: 'password', |
| 54 | + client_id: clientId, |
| 55 | + client_secret: clientSecret, |
| 56 | + username, |
| 57 | + password |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + return { |
| 62 | + baseUrl, |
| 63 | + headers: { |
| 64 | + Authorization: `Bearer ${authBody.access_token}` |
| 65 | + } |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + const {headers} = await got.post('/session/', { |
| 70 | + baseUrl, |
| 71 | + headers: { |
| 72 | + Origin: url, |
| 73 | + 'Content-Type': 'application/json' |
| 74 | + }, |
| 75 | + body: JSON.stringify({username, password}) |
| 76 | + }); |
| 77 | + |
| 78 | + /* istanbul ignore next */ |
| 79 | + const cookies = headers['set-cookie'] || []; |
| 80 | + const filteredCookies = cookies.map(Cookie.parse).filter(Boolean).map(c => c.cookieString()); |
| 81 | + |
| 82 | + return { |
| 83 | + baseUrl, |
| 84 | + headers: { |
| 85 | + Origin: url, |
| 86 | + Cookie: filteredCookies |
| 87 | + } |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +async function runImport(version, url, auth, exportFile) { |
| 92 | + const authOpts = await getAuthOpts(version, url, auth); |
| 93 | + const body = new FormData(); |
| 94 | + |
| 95 | + body.append('importfile', fs.createReadStream(exportFile)); |
| 96 | + await got.post('/db/', {...authOpts, body}); |
| 97 | +} |
| 98 | + |
| 99 | +module.exports = { |
| 100 | + getBaseUrl, |
| 101 | + isSetup, |
| 102 | + setup, |
| 103 | + runImport |
| 104 | +}; |
0 commit comments