|
| 1 | +import { GluegunCommand, GluegunToolbox } from "gluegun" |
| 2 | + |
| 3 | +import * as fs from "fs-extra" |
| 4 | + |
| 5 | +import { runPreviewSwarm } from "../../utils/wordpress-puppeteer/run-preview-swarm" |
| 6 | + |
| 7 | +const command: GluegunCommand = { |
| 8 | + name: `swarm`, |
| 9 | + commandPath: [`preview`, `swarm`], |
| 10 | + dashed: true, |
| 11 | + description: `Swarm a Gatsby Preview instance with bot WP users`, |
| 12 | + run: async (toolbox: GluegunToolbox) => { |
| 13 | + const { print, prompt, parameters } = toolbox |
| 14 | + |
| 15 | + const { |
| 16 | + wpUrl: optionWpUrl, |
| 17 | + users: optionUsersJsonPath, |
| 18 | + previewRefreshUrl, |
| 19 | + previewFrontendUrl, |
| 20 | + timeout, |
| 21 | + previewsEach, |
| 22 | + debugMode, |
| 23 | + } = parameters.options |
| 24 | + |
| 25 | + print.info( |
| 26 | + `We're going to toast your WP and Gatsby and then smatter them with delicious jam` |
| 27 | + ) |
| 28 | + |
| 29 | + let { wpUrl, wpUsersJsonPath } = await prompt.ask([ |
| 30 | + !optionWpUrl && { |
| 31 | + type: `input`, |
| 32 | + name: `wpUrl`, |
| 33 | + message: `What's the full URL for your WordPress instance?`, |
| 34 | + }, |
| 35 | + !optionUsersJsonPath && { |
| 36 | + type: `input`, |
| 37 | + name: `wpUsersJsonPath`, |
| 38 | + message: `What's the path to your users.json file?`, |
| 39 | + }, |
| 40 | + ]) |
| 41 | + |
| 42 | + if (optionWpUrl) { |
| 43 | + wpUrl = optionWpUrl |
| 44 | + } |
| 45 | + |
| 46 | + if (optionUsersJsonPath) { |
| 47 | + wpUsersJsonPath = optionUsersJsonPath |
| 48 | + } |
| 49 | + |
| 50 | + if (!wpUsersJsonPath) { |
| 51 | + print.error(`You must provide a path to your users.json file.`) |
| 52 | + } |
| 53 | + |
| 54 | + if (!wpUrl) { |
| 55 | + print.error(`You must provide the home url for your WordPress instance.`) |
| 56 | + } |
| 57 | + |
| 58 | + if (!wpUsersJsonPath || !wpUrl) { |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + const users = await fs.readJson(wpUsersJsonPath) |
| 63 | + |
| 64 | + await runPreviewSwarm({ |
| 65 | + headless: false, |
| 66 | + maxPreviewsEach: previewsEach || 10, |
| 67 | + previewTimeout: timeout, |
| 68 | + users, |
| 69 | + wpUrl, |
| 70 | + gatsbyPreviewFrontendUrl: previewFrontendUrl, |
| 71 | + gatsbyPreviewRefreshEndpoint: previewRefreshUrl, |
| 72 | + debugMode, |
| 73 | + }) |
| 74 | + }, |
| 75 | +} |
| 76 | + |
| 77 | +module.exports = command |
0 commit comments