|
1 | 1 | import { reporter } from "./reporter"
|
2 | 2 | import path from "path"
|
3 | 3 | import { PluginConfigMap } from "."
|
4 |
| -export async function installPlugins( |
5 |
| - plugins: Array<string>, |
6 |
| - pluginOptions: PluginConfigMap = {}, |
7 |
| - rootPath: string, |
8 |
| - packages: Array<string> |
9 |
| -): Promise<void> { |
10 |
| - let installPluginCommand |
11 |
| - let gatsbyPath |
| 4 | +import { requireResolve } from "./require-utils" |
12 | 5 |
|
| 6 | +const resolveGatsbyPath = (rootPath: string): string | never => { |
13 | 7 | try {
|
14 |
| - gatsbyPath = require.resolve(`gatsby/package.json`, { |
| 8 | + const gatsbyPath = requireResolve(`gatsby/package.json`, { |
15 | 9 | paths: [rootPath],
|
16 | 10 | })
|
17 |
| - } catch (e) { |
18 |
| - // Not found |
19 |
| - console.warn(e) |
20 |
| - } |
21 | 11 |
|
22 |
| - if (!gatsbyPath) { |
23 |
| - reporter.error( |
| 12 | + if (!gatsbyPath) throw new Error() |
| 13 | + |
| 14 | + return gatsbyPath |
| 15 | + } catch (e) { |
| 16 | + throw new Error( |
24 | 17 | `Could not find "gatsby" in ${rootPath}. Perhaps it wasn't installed properly?`
|
25 | 18 | )
|
26 |
| - return |
27 | 19 | }
|
| 20 | +} |
28 | 21 |
|
| 22 | +const resolveGatsbyCliPath = ( |
| 23 | + rootPath: string, |
| 24 | + gatsbyPath: string |
| 25 | +): string | never => { |
29 | 26 | try {
|
30 |
| - installPluginCommand = require.resolve( |
| 27 | + const installPluginCommand = requireResolve( |
31 | 28 | `gatsby-cli/lib/handlers/plugin-add`,
|
32 | 29 | {
|
33 | 30 | // Try to find gatsby-cli in the site root, or in the site's gatsby dir
|
34 | 31 | paths: [rootPath, path.dirname(gatsbyPath)],
|
35 | 32 | }
|
36 | 33 | )
|
| 34 | + |
| 35 | + if (!installPluginCommand) throw new Error() |
| 36 | + |
| 37 | + return installPluginCommand |
37 | 38 | } catch (e) {
|
38 |
| - // The file is missing |
| 39 | + throw new Error(`gatsby-cli not installed, or is too old`) |
39 | 40 | }
|
| 41 | +} |
40 | 42 |
|
41 |
| - if (!installPluginCommand) { |
42 |
| - reporter.error(`gatsby-cli not installed, or is too old`) |
43 |
| - return |
| 43 | +const addPluginsToProject = async ( |
| 44 | + installPluginCommand: string, |
| 45 | + plugins: Array<string>, |
| 46 | + pluginOptions: PluginConfigMap = {}, |
| 47 | + rootPath: string, |
| 48 | + packages: Array<string> |
| 49 | +): Promise<void> => { |
| 50 | + try { |
| 51 | + const { addPlugins } = require(installPluginCommand) |
| 52 | + await addPlugins(plugins, pluginOptions, rootPath, packages) |
| 53 | + } catch (e) { |
| 54 | + throw new Error( |
| 55 | + `Something went wrong when trying to add the plugins to the project: ${e.message}` |
| 56 | + ) |
44 | 57 | }
|
| 58 | +} |
45 | 59 |
|
46 |
| - const { addPlugins } = require(installPluginCommand) |
| 60 | +export async function installPlugins( |
| 61 | + plugins: Array<string>, |
| 62 | + pluginOptions: PluginConfigMap = {}, |
| 63 | + rootPath: string, |
| 64 | + packages: Array<string> |
| 65 | +): Promise<void> { |
| 66 | + try { |
| 67 | + const gatsbyPath = resolveGatsbyPath(rootPath) |
| 68 | + const installPluginCommand = resolveGatsbyCliPath(rootPath, gatsbyPath) |
47 | 69 |
|
48 |
| - await addPlugins(plugins, pluginOptions, rootPath, packages) |
| 70 | + await addPluginsToProject( |
| 71 | + installPluginCommand, |
| 72 | + plugins, |
| 73 | + pluginOptions, |
| 74 | + rootPath, |
| 75 | + packages |
| 76 | + ) |
| 77 | + } catch (e) { |
| 78 | + reporter.error(e.message) |
| 79 | + return |
| 80 | + } |
49 | 81 | }
|
0 commit comments