Skip to content
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

Feat: (gatsby-cli) Add a CLI command for listing plugins #28018

Merged
merged 13 commits into from
Dec 1, 2020
23 changes: 23 additions & 0 deletions integration-tests/gatsby-cli/__tests__/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { GatsbyCLI } from "../test-helpers"


const MAX_TIMEOUT = 2147483647
jest.setTimeout(MAX_TIMEOUT)

describe(`gatsby plugin`, () => {
const cwd = `gatsby-sites/gatsby-plugin`

beforeAll(() => GatsbyCLI.from(cwd).invoke(`clean`))
afterAll(() => GatsbyCLI.from(cwd).invoke(`clean`))

it(`lists plugins`, async () => {
const [code, logs] = GatsbyCLI.from(cwd).invoke([`plugin`, `ls`])

logs.should.contain(`gatsby-source-filesystem`)
logs.should.contain(`gatsby-plugin-offline`)
expect(logs).toEqual(expect.not.stringContaining(`ignore comments`))
expect(code).toBe(0)

})
})

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Disable eslint-loader
69 changes: 69 additions & 0 deletions integration-tests/gatsby-cli/gatsby-sites/gatsby-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# dotenv environment variable files
.env*

# gatsby files
.cache/
public

# Mac files
.DS_Store

# Yarn
yarn-error.log
.pnp/
.pnp.js
# Yarn Integrity file
.yarn-integrity
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
plugins: [
{
resolve: "gatsby-source-filesystem",
options: {
"name": "pages",
"path": "./src/pages/"
}

},
`gatsby-plugin-offline`,
// ignore comments
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "gatsby-starter-default-plugin",
"private": true,
"description": "A simple starter to get up and developing quickly with Gatsby",
"version": "0.1.0",
"author": "Kyle Mathews <[email protected]>",
"dependencies": {
"gatsby": "^2.19.45",
"prop-types": "^15.7.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-helmet": "^5.2.1",
"gatsby-source-filesystem": "^2.5.0",
"gatsby-plugin-offline": "^3.4.0"
},
"devDependencies": {
"prettier": "2.0.4"
},
"keywords": [
"gatsby"
],
"license": "MIT",
"scripts": {
"build": "gatsby build",
"develop": "gatsby develop",
"format": "prettier --write \"**/*.{js,jsx,json,md}\"",
"start": "npm run develop",
"serve": "gatsby serve",
"clean": "gatsby clean",
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/gatsbyjs/gatsby-starter-default"
},
"bugs": {
"url": "https://github.com/gatsbyjs/gatsby/issues"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import React from "react"
export default () => <div>Hi</div>
11 changes: 7 additions & 4 deletions packages/create-gatsby/src/install-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ export async function installPlugins(
}

try {
installPluginCommand = require.resolve(`gatsby-cli/lib/plugin-add`, {
// Try to find gatsby-cli in the site root, or in the site's gatsby dir
paths: [rootPath, path.dirname(gatsbyPath)],
})
installPluginCommand = require.resolve(
`gatsby-cli/lib/handlers/plugin-add`,
{
// Try to find gatsby-cli in the site root, or in the site's gatsby dir
paths: [rootPath, path.dirname(gatsbyPath)],
}
)
} catch (e) {
// The file is missing
}
Expand Down
13 changes: 10 additions & 3 deletions packages/gatsby-cli/src/create-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { recipesHandler } from "./recipes"
import { startGraphQLServer } from "gatsby-recipes"
import { getPackageManager, setPackageManager } from "./util/package-manager"
import reporter from "./reporter"
import pluginHandler from "./handlers/plugin"

const handlerP = (fn: Function) => (...args: Array<unknown>): void => {
Promise.resolve(fn(...args)).then(
Expand Down Expand Up @@ -400,15 +401,21 @@ function buildLocalCommands(cli: yargs.Argv, isLocalSite: boolean): void {
builder: yargs =>
yargs
.positional(`cmd`, {
choices: [`docs`],
describe: "Valid commands include `docs`.",
choices: [`docs`, `ls`],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed now, but I wonder if we should add list as an alias for ls. Windows people might not know what ls means

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough and easy to do. Let’s get this in and we can add it in the next pass.

describe: "Valid commands include `docs`, `ls`.",
type: `string`,
})
.positional(`plugins`, {
describe: `The plugin names`,
type: `string`,
}),
handler: getCommandHandler(`plugin`),
handler: async ({
cmd,
}: yargs.Arguments<{
cmd: string | undefined
}>) => {
await pluginHandler(siteInfo.directory, cmd)
},
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NPMPackage, GatsbyPlugin } from "gatsby-recipes"
import reporter from "./reporter"
import reporter from "../reporter"
const normalizePluginName = (plugin: string): string => {
if (plugin.startsWith(`gatsby-`)) {
return plugin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IProgram } from "./types"
import { GatsbyPlugin } from "gatsby-recipes"
import reporter from "../reporter"

module.exports = async (args: IProgram & { cmd: string }): Promise<void> => {
const { report, cmd } = args
export default async (root: string, cmd: string | undefined): Promise<void> => {
switch (cmd) {
case `docs`:
console.log(`
Expand All @@ -25,9 +25,20 @@ module.exports = async (args: IProgram & { cmd: string }): Promise<void> => {
- Join Discord #plugin-authoring channel to ask questions! (https://gatsby.dev/discord/)
`)
return
case `ls`: {
try {
const plugins = await GatsbyPlugin.all({ root }, false)
console.log(plugins)
} catch {
reporter.error(
`There was a problem parsing your \`gatsby-config.js\` file.\nIt may be malformed. Or, the syntax you're using is not currently supported by this command.`
)
}

return
}
default:
report.error(`Unknown command ${cmd}`)
reporter.error(`Unknown command ${cmd}`)
}
return
}
6 changes: 4 additions & 2 deletions packages/gatsby-recipes/src/providers/gatsby/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,13 @@ export { create, create as update, read, destroy }

export const config = {}

export const all = async ({ root }) => {
export const all = async ({ root }, processPlugins = true) => {
const configSrc = await readConfigFile(root)
const plugins = getPluginsFromConfig(configSrc)

return Promise.all(plugins.map(({ name }) => read({ root }, name)))
return Promise.all(
laurieontech marked this conversation as resolved.
Show resolved Hide resolved
plugins.map(({ name }) => (processPlugins ? read({ root }, name) : name))
)
}

const schema = {
Expand Down