-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(gatsby-cli): Re-Add plugin-add functionality #34482
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
import * as fs from "fs-extra" | ||
import execa from "execa" | ||
import _ from "lodash" | ||
import { | ||
readConfigFile, | ||
lock, | ||
getConfigPath, | ||
getConfigStore, | ||
} from "gatsby-core-utils" | ||
import { transform } from "@babel/core" | ||
import { BabelPluginAddPluginsToGatsbyConfig } from "./plugin-babel-utils" | ||
|
||
const addPluginToConfig = ( | ||
src: string, | ||
{ | ||
name, | ||
options, | ||
key, | ||
}: { | ||
name: string | ||
options: Record<string, unknown> | undefined | ||
key: string | ||
} | ||
): string => { | ||
const addPlugins = new BabelPluginAddPluginsToGatsbyConfig({ | ||
pluginOrThemeName: name, | ||
options, | ||
shouldAdd: true, | ||
key, | ||
}) | ||
|
||
// @ts-ignore - fix me | ||
const { code } = transform(src, { | ||
// @ts-ignore - fix me | ||
plugins: [addPlugins.plugin], | ||
configFile: false, | ||
}) | ||
|
||
return code | ||
} | ||
|
||
interface IGatsbyPluginCreateInput { | ||
root: string | ||
name: string | ||
options: Record<string, unknown> | undefined | ||
key: string | ||
} | ||
|
||
export const GatsbyPluginCreate = async ({ | ||
root, | ||
name, | ||
options, | ||
key, | ||
}: IGatsbyPluginCreateInput): Promise<void> => { | ||
const release = await lock(`gatsby-config.js`) | ||
const configSrc = await readConfigFile(root) | ||
|
||
const code = addPluginToConfig(configSrc, { name, options, key }) | ||
|
||
await fs.writeFile(getConfigPath(root), code) | ||
release() | ||
} | ||
|
||
const packageMangerConfigKey = `cli.packageManager` | ||
const PACKAGE_MANAGER = getConfigStore().get(packageMangerConfigKey) || `yarn` | ||
|
||
const getPackageNames = ( | ||
packages: Array<{ name: string; version: string }> | ||
): Array<string> => packages.map(n => `${n.name}@${n.version}`) | ||
|
||
const generateClientCommands = ({ | ||
packageManager, | ||
depType, | ||
packageNames, | ||
}: { | ||
packageManager: string | ||
depType: string | ||
packageNames: Array<string> | ||
}): Array<string> | undefined => { | ||
const commands: Array<string> = [] | ||
if (packageManager === `yarn`) { | ||
commands.push(`add`) | ||
// Needed for Yarn Workspaces and is a no-opt elsewhere. | ||
commands.push(`-W`) | ||
if (depType === `development`) { | ||
commands.push(`--dev`) | ||
} | ||
|
||
return commands.concat(packageNames) | ||
} else if (packageManager === `npm`) { | ||
commands.push(`install`) | ||
if (depType === `development`) { | ||
commands.push(`--save-dev`) | ||
} | ||
return commands.concat(packageNames) | ||
} | ||
|
||
return undefined | ||
} | ||
|
||
let installs: Array<{ | ||
outsideResolve: any | ||
outsideReject: any | ||
resource: any | ||
}> = [] | ||
const executeInstalls = async (root: string): Promise<void> => { | ||
// @ts-ignore - fix me | ||
const types = _.groupBy(installs, c => c.resource.dependencyType) | ||
|
||
// Grab the key of the first install & delete off installs these packages | ||
// then run intall | ||
// when done, check again & call executeInstalls again. | ||
// @ts-ignore - fix me | ||
const depType = installs[0].resource.dependencyType | ||
const packagesToInstall = types[depType] | ||
installs = installs.filter( | ||
// @ts-ignore - fix me | ||
i => !packagesToInstall.some(p => i.resource.name === p.resource.name) | ||
) | ||
|
||
// @ts-ignore - fix me | ||
const pkgs = packagesToInstall.map(p => p.resource) | ||
const packageNames = getPackageNames(pkgs) | ||
|
||
const commands = generateClientCommands({ | ||
packageNames, | ||
depType, | ||
packageManager: PACKAGE_MANAGER, | ||
}) | ||
|
||
const release = await lock(`package.json`) | ||
try { | ||
await execa(PACKAGE_MANAGER, commands, { | ||
cwd: root, | ||
}) | ||
} catch (e) { | ||
// A package failed so call the rejects | ||
return packagesToInstall.forEach(p => { | ||
// @ts-ignore - fix me | ||
p.outsideReject( | ||
JSON.stringify({ | ||
message: e.shortMessage, | ||
installationError: `Could not install package`, | ||
}) | ||
) | ||
}) | ||
} | ||
release() | ||
|
||
// @ts-ignore - fix me | ||
packagesToInstall.forEach(p => p.outsideResolve()) | ||
|
||
// Run again if there's still more installs. | ||
if (installs.length > 0) { | ||
executeInstalls(root) | ||
} | ||
|
||
return undefined | ||
} | ||
|
||
const debouncedExecute = _.debounce(executeInstalls, 25) | ||
|
||
interface IPackageCreateInput { | ||
root: string | ||
name: string | ||
} | ||
|
||
const createInstall = async ({ | ||
root, | ||
name, | ||
}: IPackageCreateInput): Promise<unknown> => { | ||
let outsideResolve | ||
let outsideReject | ||
const promise = new Promise((resolve, reject) => { | ||
outsideResolve = resolve | ||
outsideReject = reject | ||
}) | ||
installs.push({ | ||
outsideResolve, | ||
outsideReject, | ||
resource: name, | ||
}) | ||
|
||
debouncedExecute(root) | ||
return promise | ||
} | ||
|
||
export const NPMPackageCreate = async ({ | ||
root, | ||
name, | ||
}: IPackageCreateInput): Promise<void> => { | ||
await createInstall({ root, name }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import reporter from "../reporter" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copied from https://github.com/gatsbyjs/gatsby/blob/fb530ecccd45037b664525c5a7d18ea13f64a540/packages/gatsby-cli/src/handlers/plugin-add.ts which was deleted in "Remove recipes" PR |
||
import { GatsbyPluginCreate, NPMPackageCreate } from "./plugin-add-utils" | ||
|
||
const normalizePluginName = (plugin: string): string => { | ||
if (plugin.startsWith(`gatsby-`)) { | ||
return plugin | ||
} | ||
if ( | ||
plugin.startsWith(`source-`) || | ||
plugin.startsWith(`transformer-`) || | ||
plugin.startsWith(`plugin-`) | ||
) { | ||
return `gatsby-${plugin}` | ||
} | ||
return `gatsby-plugin-${plugin}` | ||
} | ||
|
||
async function installPluginPackage( | ||
plugin: string, | ||
root: string | ||
): Promise<void> { | ||
const installTimer = reporter.activityTimer(`Installing ${plugin}`) | ||
|
||
installTimer.start() | ||
reporter.info(`Installing ${plugin}`) | ||
try { | ||
await NPMPackageCreate({ root, name: plugin }) | ||
reporter.info(`Installed NPM package ${plugin}`) | ||
} catch (err) { | ||
reporter.error(JSON.parse(err)?.message) | ||
installTimer.setStatus(`FAILED`) | ||
} | ||
installTimer.end() | ||
} | ||
|
||
async function installPluginConfig( | ||
plugin: string, | ||
options: Record<string, unknown> | undefined, | ||
root: string | ||
): Promise<void> { | ||
// Plugins can optionally include a key, to allow duplicates | ||
const [pluginName, pluginKey] = plugin.split(`:`) | ||
|
||
const installTimer = reporter.activityTimer( | ||
`Adding ${pluginName} ${pluginKey ? `(${pluginKey}) ` : ``}to gatsby-config` | ||
) | ||
|
||
installTimer.start() | ||
reporter.info(`Adding ${pluginName}`) | ||
try { | ||
await GatsbyPluginCreate({ | ||
root, | ||
name: pluginName, | ||
options, | ||
key: pluginKey, | ||
}) | ||
reporter.info(`Installed ${pluginName || pluginKey} in gatsby-config.js`) | ||
} catch (err) { | ||
reporter.error(JSON.parse(err)?.message) | ||
installTimer.setStatus(`FAILED`) | ||
} | ||
installTimer.end() | ||
} | ||
|
||
export async function addPlugins( | ||
plugins: Array<string>, | ||
pluginOptions: Record<string, Record<string, unknown>>, | ||
directory: string, | ||
packages: Array<string> = [] | ||
): Promise<void> { | ||
if (!plugins?.length) { | ||
reporter.error(`Please specify a plugin to install`) | ||
return | ||
} | ||
|
||
const pluginList = plugins.map(normalizePluginName) | ||
|
||
await Promise.all( | ||
packages.map(plugin => installPluginPackage(plugin, directory)) | ||
) | ||
await Promise.all( | ||
pluginList.map(plugin => | ||
installPluginConfig(plugin, pluginOptions[plugin], directory) | ||
) | ||
) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Partially copied from https://github.com/gatsbyjs/gatsby/blob/fb530ecccd45037b664525c5a7d18ea13f64a540/packages/gatsby-recipes/src/providers/gatsby/plugin.js & https://github.com/gatsbyjs/gatsby/blob/fb530ecccd45037b664525c5a7d18ea13f64a540/packages/gatsby-recipes/src/providers/npm/package.js