|
| 1 | +import Promise from 'bluebird'; |
| 2 | +import axios from 'axios'; |
| 3 | +import GhostAdminAPI from '@tryghost/admin-api'; |
| 4 | +import {makeTaskRunner} from '@tryghost/listr-smart-renderer'; |
| 5 | +import _ from 'lodash'; |
| 6 | +import {apiAuthTokenHeaders} from '../lib/admin-api-call.js'; |
| 7 | +import {getAPIMemberLabels} from '../lib/ghost-api-choices.js'; |
| 8 | + |
| 9 | +const initialise = (options) => { |
| 10 | + return { |
| 11 | + title: 'Initialising API connection', |
| 12 | + task: (ctx, task) => { |
| 13 | + let defaults = { |
| 14 | + verbose: false, |
| 15 | + labels: false, |
| 16 | + delayBetweenCalls: 50 |
| 17 | + }; |
| 18 | + |
| 19 | + const url = options.apiURL.replace(/\/$/, ''); |
| 20 | + const key = options.adminAPIKey; |
| 21 | + const api = new GhostAdminAPI({ |
| 22 | + url: url.replace('localhost', '127.0.0.1'), |
| 23 | + key, |
| 24 | + version: 'v5.0' |
| 25 | + }); |
| 26 | + |
| 27 | + ctx.args = _.mergeWith(defaults, options); |
| 28 | + ctx.api = api; |
| 29 | + ctx.labels = []; |
| 30 | + ctx.deleted = []; |
| 31 | + |
| 32 | + task.output = `Initialised API connection for ${options.apiURL}`; |
| 33 | + } |
| 34 | + }; |
| 35 | +}; |
| 36 | + |
| 37 | +const getFullTaskList = (options) => { |
| 38 | + return [ |
| 39 | + initialise(options), |
| 40 | + { |
| 41 | + title: 'Fetch labels from Ghost API', |
| 42 | + task: async (ctx) => { |
| 43 | + if (ctx.args.labels[0].id) { |
| 44 | + ctx.labels = ctx.args.labels; |
| 45 | + } else { |
| 46 | + const allLabels = await getAPIMemberLabels({returnKey: false, options}); |
| 47 | + |
| 48 | + ctx.args.labels.forEach((label) => { |
| 49 | + let labelsObject = allLabels.find((labelObj) => { |
| 50 | + return labelObj.value.name === label; |
| 51 | + }); |
| 52 | + |
| 53 | + if (labelsObject) { |
| 54 | + ctx.labels.push(labelsObject.value); |
| 55 | + } else { |
| 56 | + ctx.errors.push(`No label found for "${label}"`); |
| 57 | + } |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | + }, |
| 62 | + { |
| 63 | + title: 'Deleting labels from Ghost', |
| 64 | + skip: (ctx) => { |
| 65 | + return ctx.labels.length === 0; |
| 66 | + }, |
| 67 | + task: async (ctx) => { |
| 68 | + let tasks = []; |
| 69 | + |
| 70 | + const headers = apiAuthTokenHeaders(options); |
| 71 | + |
| 72 | + await Promise.mapSeries(ctx.labels, async (label) => { |
| 73 | + tasks.push({ |
| 74 | + title: `Delete label ${label.name}`, |
| 75 | + task: async () => { |
| 76 | + let urlString = `${options.apiURL}/ghost/api/admin/labels/${label.id}/`; |
| 77 | + |
| 78 | + try { |
| 79 | + let result = await axios.delete(urlString, {headers}); |
| 80 | + ctx.deleted.push(label.name); |
| 81 | + return Promise.delay(options.delayBetweenCalls).return(result); |
| 82 | + } catch (error) { |
| 83 | + error.resource = { |
| 84 | + name: label.name |
| 85 | + }; |
| 86 | + error.object = label; |
| 87 | + ctx.errors.push(error); |
| 88 | + throw error; |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + let taskOptions = options; |
| 95 | + taskOptions.concurrent = 3; |
| 96 | + return makeTaskRunner(tasks, taskOptions); |
| 97 | + } |
| 98 | + } |
| 99 | + ]; |
| 100 | +}; |
| 101 | + |
| 102 | +const getTaskRunner = (options) => { |
| 103 | + let tasks = []; |
| 104 | + |
| 105 | + tasks = getFullTaskList(options); |
| 106 | + |
| 107 | + return makeTaskRunner(tasks, Object.assign({topLevel: true}, {...options, verbose: true})); |
| 108 | +}; |
| 109 | + |
| 110 | +export default { |
| 111 | + initialise, |
| 112 | + getFullTaskList, |
| 113 | + getTaskRunner |
| 114 | +}; |
0 commit comments