|
| 1 | +import { spawn } from "child_process"; |
| 2 | +import fs from "fs"; |
| 3 | +import inquirer from "inquirer"; |
| 4 | +import isProjectOfType from "../utils/isProjectOfType.js"; |
| 5 | +import Logger from "../utils/logger.js"; |
| 6 | +import wget from "../utils/wget.js"; |
| 7 | + |
| 8 | +/** |
| 9 | + * @summary Get and parse local plugins.json file |
| 10 | + * @returns {Object} - return the local plugins.json as object |
| 11 | + */ |
| 12 | +async function getRemotePluginsJson() { |
| 13 | + const remotePackageJsonUrl = "https://raw.githubusercontent.com/reactioncommerce/reaction/trunk/plugins.json"; |
| 14 | + try { |
| 15 | + const pluginsJson = await wget(remotePackageJsonUrl); |
| 16 | + |
| 17 | + return JSON.parse(pluginsJson); |
| 18 | + } catch (error) { |
| 19 | + Logger.error("Unable to get local plugins.json"); |
| 20 | + Logger.error(error); |
| 21 | + throw error; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * @summary Get the list of all upstream plugins defined in the plugins.json file of the official repo |
| 27 | + * @return {Promise<String[]>} - list of all plugins |
| 28 | + */ |
| 29 | +async function getAllPlugins() { |
| 30 | + const plugins = await getRemotePluginsJson(); |
| 31 | + return Object.values(plugins) |
| 32 | + .map((plugin) => plugin.replace("@reactioncommerce/", "")); |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * @summary Creates api-plugins directory in it doesn't exist |
| 37 | + * @return {void} |
| 38 | + */ |
| 39 | +function ensureApiPluginsDirectoryExists() { |
| 40 | + const apiPluginsDir = `${process.cwd()}/api-plugins`; |
| 41 | + if (!fs.existsSync(apiPluginsDir)) { |
| 42 | + Logger.info("Creating api-plugins directory"); |
| 43 | + fs.mkdirSync(apiPluginsDir); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * @summary Checks if plugin is already cloned |
| 49 | + * @param {String} plugin - key of the plugin |
| 50 | + * @return {boolean} - true if plugin is already cloned |
| 51 | + */ |
| 52 | +function isPluginAlreadyCloned(plugin) { |
| 53 | + const apiPluginsDir = `${process.cwd()}/api-plugins`; |
| 54 | + const pluginDir = `${apiPluginsDir}/${plugin}`; |
| 55 | + const pluginAlreadyCloned = fs.existsSync(pluginDir); |
| 56 | + if (pluginAlreadyCloned) { |
| 57 | + Logger.info(`Plugin ${plugin} already cloned`); |
| 58 | + } |
| 59 | + return pluginAlreadyCloned; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * @summary Generate the repo url for a plugin |
| 64 | + * @param {String} plugin - key of the plugin |
| 65 | + * @return {String} - the repo url |
| 66 | + */ |
| 67 | +function buildRepoUrlForPlugin(plugin) { |
| 68 | + return `https://github.com/reactioncommerce/${plugin}.git`; |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * @summary Clones a plugin in the api-plugins directory |
| 73 | + * @param {String} plugin - key of the plugin |
| 74 | + * @return {Promise<void>} - promise that resolves when the plugin is cloned |
| 75 | + */ |
| 76 | +async function clonePlugin(plugin) { |
| 77 | + const apiPluginRepoUrl = buildRepoUrlForPlugin(plugin); |
| 78 | + |
| 79 | + return new Promise((resolve, reject) => { |
| 80 | + spawn("git", ["clone", apiPluginRepoUrl], { |
| 81 | + stdio: [process.stdout, process.stderr, process.stdin], |
| 82 | + cwd: `${process.cwd()}/api-plugins` |
| 83 | + }) |
| 84 | + .on("exit", (errorCode) => { |
| 85 | + if (errorCode === 0) { |
| 86 | + resolve(); |
| 87 | + } |
| 88 | + reject("Error cloning plugin"); |
| 89 | + }); |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * @summary Opens a prompt for manually selecting plugins to clone |
| 95 | + * @param {String[]} allPlugins - list of all plugins |
| 96 | + * @return {Promise<String[]>} - list of the selected plugins |
| 97 | + */ |
| 98 | +async function getManuallySelectedPlugins(allPlugins) { |
| 99 | + const { plugins } = await inquirer.prompt([{ |
| 100 | + type: "checkbox", |
| 101 | + message: "Select the plugins you want to clone:", |
| 102 | + name: "plugins", |
| 103 | + choices: allPlugins, |
| 104 | + validate: (ans) => { |
| 105 | + if (ans.length === 0) { |
| 106 | + return "You must choose at least one plugin."; |
| 107 | + } |
| 108 | + return true; |
| 109 | + } |
| 110 | + }]); |
| 111 | + return plugins; |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * @summary Get the plugins.json file |
| 116 | + * @return {Object} - the plugins.json as an object |
| 117 | + */ |
| 118 | +function getPluginsJson() { |
| 119 | + const pluginsJson = fs.readFileSync(`${process.cwd()}/plugins.json`, "utf8"); |
| 120 | + return JSON.parse(pluginsJson); |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * @summary Link a local plugin in the plugins.json file |
| 125 | + * @param {String} plugin - name of the plugin |
| 126 | + * @return {void} |
| 127 | + */ |
| 128 | +function linkLocalPlugin(plugin) { |
| 129 | + Logger.info(`Linking local plugin ${plugin} in plugins.json`); |
| 130 | + const pluginsJson = getPluginsJson(); |
| 131 | + for (const key in pluginsJson) { |
| 132 | + if (pluginsJson[key] === `@reactioncommerce/${plugin}`) { |
| 133 | + pluginsJson[key] = `./api-plugins/${plugin}/index.js`; |
| 134 | + break; |
| 135 | + } |
| 136 | + } |
| 137 | + fs.writeFileSync(`${process.cwd()}/plugins.json`, JSON.stringify(pluginsJson, null, 2)); |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * @summary Clone all official Open Commerce API plugins in the api project |
| 142 | + * @param {Object} options - Options for cloning api plugins command |
| 143 | + * @returns {Promise<boolean>} - return true if successful |
| 144 | + */ |
| 145 | +export default async function cloneApiPlugins({ |
| 146 | + manualSelect, |
| 147 | + link |
| 148 | +}) { |
| 149 | + const isApiProject = await isProjectOfType("api"); |
| 150 | + if (!isApiProject) { |
| 151 | + return false; |
| 152 | + } |
| 153 | + |
| 154 | + const allPlugins = await getAllPlugins(); |
| 155 | + |
| 156 | + ensureApiPluginsDirectoryExists(); |
| 157 | + |
| 158 | + const manuallySelectedPlugins = manualSelect && await getManuallySelectedPlugins(allPlugins); |
| 159 | + |
| 160 | + const pluginsToClone = manuallySelectedPlugins || allPlugins; |
| 161 | + |
| 162 | + const cloneAndLinkPromises = pluginsToClone.map(async (plugin) => { |
| 163 | + if (!isPluginAlreadyCloned(plugin)) { |
| 164 | + await clonePlugin(plugin); |
| 165 | + } |
| 166 | + if (link) { |
| 167 | + linkLocalPlugin(plugin); |
| 168 | + } |
| 169 | + }); |
| 170 | + |
| 171 | + try { |
| 172 | + await Promise.all(cloneAndLinkPromises); |
| 173 | + } catch (error) { |
| 174 | + Logger.error(error); |
| 175 | + return false; |
| 176 | + } |
| 177 | + return true; |
| 178 | +} |
0 commit comments