|
| 1 | +import * as fs from "fs-extra" |
| 2 | +import execa from "execa" |
| 3 | +import _ from "lodash" |
| 4 | +import { |
| 5 | + readConfigFile, |
| 6 | + lock, |
| 7 | + getConfigPath, |
| 8 | + getConfigStore, |
| 9 | +} from "gatsby-core-utils" |
| 10 | +import { transform } from "@babel/core" |
| 11 | +import { BabelPluginAddPluginsToGatsbyConfig } from "./plugin-babel-utils" |
| 12 | + |
| 13 | +const addPluginToConfig = ( |
| 14 | + src: string, |
| 15 | + { |
| 16 | + name, |
| 17 | + options, |
| 18 | + key, |
| 19 | + }: { |
| 20 | + name: string |
| 21 | + options: Record<string, unknown> | undefined |
| 22 | + key: string |
| 23 | + } |
| 24 | +): string => { |
| 25 | + const addPlugins = new BabelPluginAddPluginsToGatsbyConfig({ |
| 26 | + pluginOrThemeName: name, |
| 27 | + options, |
| 28 | + shouldAdd: true, |
| 29 | + key, |
| 30 | + }) |
| 31 | + |
| 32 | + // @ts-ignore - fix me |
| 33 | + const { code } = transform(src, { |
| 34 | + // @ts-ignore - fix me |
| 35 | + plugins: [addPlugins.plugin], |
| 36 | + configFile: false, |
| 37 | + }) |
| 38 | + |
| 39 | + return code |
| 40 | +} |
| 41 | + |
| 42 | +interface IGatsbyPluginCreateInput { |
| 43 | + root: string |
| 44 | + name: string |
| 45 | + options: Record<string, unknown> | undefined |
| 46 | + key: string |
| 47 | +} |
| 48 | + |
| 49 | +export const GatsbyPluginCreate = async ({ |
| 50 | + root, |
| 51 | + name, |
| 52 | + options, |
| 53 | + key, |
| 54 | +}: IGatsbyPluginCreateInput): Promise<void> => { |
| 55 | + const release = await lock(`gatsby-config.js`) |
| 56 | + const configSrc = await readConfigFile(root) |
| 57 | + |
| 58 | + const code = addPluginToConfig(configSrc, { name, options, key }) |
| 59 | + |
| 60 | + await fs.writeFile(getConfigPath(root), code) |
| 61 | + release() |
| 62 | +} |
| 63 | + |
| 64 | +const packageMangerConfigKey = `cli.packageManager` |
| 65 | +const PACKAGE_MANAGER = getConfigStore().get(packageMangerConfigKey) || `yarn` |
| 66 | + |
| 67 | +const getPackageNames = ( |
| 68 | + packages: Array<{ name: string; version: string }> |
| 69 | +): Array<string> => packages.map(n => `${n.name}@${n.version}`) |
| 70 | + |
| 71 | +const generateClientCommands = ({ |
| 72 | + packageManager, |
| 73 | + depType, |
| 74 | + packageNames, |
| 75 | +}: { |
| 76 | + packageManager: string |
| 77 | + depType: string |
| 78 | + packageNames: Array<string> |
| 79 | +}): Array<string> | undefined => { |
| 80 | + const commands: Array<string> = [] |
| 81 | + if (packageManager === `yarn`) { |
| 82 | + commands.push(`add`) |
| 83 | + // Needed for Yarn Workspaces and is a no-opt elsewhere. |
| 84 | + commands.push(`-W`) |
| 85 | + if (depType === `development`) { |
| 86 | + commands.push(`--dev`) |
| 87 | + } |
| 88 | + |
| 89 | + return commands.concat(packageNames) |
| 90 | + } else if (packageManager === `npm`) { |
| 91 | + commands.push(`install`) |
| 92 | + if (depType === `development`) { |
| 93 | + commands.push(`--save-dev`) |
| 94 | + } |
| 95 | + return commands.concat(packageNames) |
| 96 | + } |
| 97 | + |
| 98 | + return undefined |
| 99 | +} |
| 100 | + |
| 101 | +let installs: Array<{ |
| 102 | + outsideResolve: any |
| 103 | + outsideReject: any |
| 104 | + resource: any |
| 105 | +}> = [] |
| 106 | +const executeInstalls = async (root: string): Promise<void> => { |
| 107 | + // @ts-ignore - fix me |
| 108 | + const types = _.groupBy(installs, c => c.resource.dependencyType) |
| 109 | + |
| 110 | + // Grab the key of the first install & delete off installs these packages |
| 111 | + // then run intall |
| 112 | + // when done, check again & call executeInstalls again. |
| 113 | + // @ts-ignore - fix me |
| 114 | + const depType = installs[0].resource.dependencyType |
| 115 | + const packagesToInstall = types[depType] |
| 116 | + installs = installs.filter( |
| 117 | + // @ts-ignore - fix me |
| 118 | + i => !packagesToInstall.some(p => i.resource.name === p.resource.name) |
| 119 | + ) |
| 120 | + |
| 121 | + // @ts-ignore - fix me |
| 122 | + const pkgs = packagesToInstall.map(p => p.resource) |
| 123 | + const packageNames = getPackageNames(pkgs) |
| 124 | + |
| 125 | + const commands = generateClientCommands({ |
| 126 | + packageNames, |
| 127 | + depType, |
| 128 | + packageManager: PACKAGE_MANAGER, |
| 129 | + }) |
| 130 | + |
| 131 | + const release = await lock(`package.json`) |
| 132 | + try { |
| 133 | + await execa(PACKAGE_MANAGER, commands, { |
| 134 | + cwd: root, |
| 135 | + }) |
| 136 | + } catch (e) { |
| 137 | + // A package failed so call the rejects |
| 138 | + return packagesToInstall.forEach(p => { |
| 139 | + // @ts-ignore - fix me |
| 140 | + p.outsideReject( |
| 141 | + JSON.stringify({ |
| 142 | + message: e.shortMessage, |
| 143 | + installationError: `Could not install package`, |
| 144 | + }) |
| 145 | + ) |
| 146 | + }) |
| 147 | + } |
| 148 | + release() |
| 149 | + |
| 150 | + // @ts-ignore - fix me |
| 151 | + packagesToInstall.forEach(p => p.outsideResolve()) |
| 152 | + |
| 153 | + // Run again if there's still more installs. |
| 154 | + if (installs.length > 0) { |
| 155 | + executeInstalls(root) |
| 156 | + } |
| 157 | + |
| 158 | + return undefined |
| 159 | +} |
| 160 | + |
| 161 | +const debouncedExecute = _.debounce(executeInstalls, 25) |
| 162 | + |
| 163 | +interface IPackageCreateInput { |
| 164 | + root: string |
| 165 | + name: string |
| 166 | +} |
| 167 | + |
| 168 | +const createInstall = async ({ |
| 169 | + root, |
| 170 | + name, |
| 171 | +}: IPackageCreateInput): Promise<unknown> => { |
| 172 | + let outsideResolve |
| 173 | + let outsideReject |
| 174 | + const promise = new Promise((resolve, reject) => { |
| 175 | + outsideResolve = resolve |
| 176 | + outsideReject = reject |
| 177 | + }) |
| 178 | + installs.push({ |
| 179 | + outsideResolve, |
| 180 | + outsideReject, |
| 181 | + resource: name, |
| 182 | + }) |
| 183 | + |
| 184 | + debouncedExecute(root) |
| 185 | + return promise |
| 186 | +} |
| 187 | + |
| 188 | +export const NPMPackageCreate = async ({ |
| 189 | + root, |
| 190 | + name, |
| 191 | +}: IPackageCreateInput): Promise<void> => { |
| 192 | + await createInstall({ root, name }) |
| 193 | +} |
0 commit comments