-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
59 additions
and
23 deletions.
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
41 changes: 41 additions & 0 deletions
41
packages/gatsby/src/bootstrap/load-plugins/utils/check-local-plugin.ts
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,41 @@ | ||
import { PluginRef } from "../types" | ||
import { sync as existsSync } from "fs-exists-cached" | ||
import { slash } from "gatsby-core-utils" | ||
import path from "path" | ||
|
||
/** | ||
* Checks if a plugin is a valid local plugin and returns the resolved path if it is. | ||
*/ | ||
export function checkLocalPlugin( | ||
plugin: PluginRef, | ||
rootDir: string | ||
): { validLocalPlugin: boolean; localPluginPath?: string } { | ||
const pluginName = typeof plugin === `string` ? plugin : plugin.resolve | ||
|
||
// Make sure the plugin exists relatively | ||
if (existsSync(pluginName) || !rootDir) { | ||
return { | ||
validLocalPlugin: false, | ||
} | ||
} | ||
|
||
const resolvedPath = slash(path.join(rootDir, `plugins/${pluginName}`)) | ||
|
||
if (!existsSync(resolvedPath)) { | ||
return { | ||
validLocalPlugin: false, | ||
} | ||
} | ||
|
||
const resolvedPackageJson = existsSync(`${resolvedPath}/package.json`) | ||
|
||
// package.json is a requirement for local plugins | ||
if (!resolvedPackageJson) { | ||
throw new Error(`Local plugin ${pluginName} requires a package.json file`) | ||
} | ||
|
||
return { | ||
validLocalPlugin: true, | ||
localPluginPath: resolvedPath, | ||
} | ||
} |