Skip to content

Commit

Permalink
misc fixes and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
moonmeister committed Mar 22, 2019
1 parent 2dc9332 commit e3bbc54
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
16 changes: 11 additions & 5 deletions packages/gatsby-cli/src/create-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,23 @@ function buildLocalCommands(cli, isLocalSite) {
})

cli.command({
command: `plugin <action> <plugins..>`,
command: `plugin <action> <plugins..> [options]`,
desc: `Manage Gatsby plugins.`,
builder: _ =>
_.positional(`action`, {
type: `string`,
describe: `Action to be taken for provided plugin.`,
choices: [`add`, `remove`, `config`, `search`],
}).positional(`plugin`, {
type: `string`,
describe: `Package(s) to add, remove, configure, or search.`,
}),
})
.positional(`plugin`, {
type: `string`,
describe: `Package(s) to add, remove, configure, or search.`,
})
.option(`dry-run`, {
default: false,
type: `boolean`,
describe: `Don't actually write any changes to disk or run npm/yarn.`,
}),
handler: getCommandHandler(`plugin`),
})

Expand Down
46 changes: 34 additions & 12 deletions packages/gatsby/src/commands/plugin.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
const enquirer = require(`enquirer`)
const report = require(`packages/gatsby-cli/lib/reporter`)
const report = require(`gatsby-cli/lib/reporter`)
const execa = require(`execa`)
const shouldUseYarn = require(`packages/gatsby-cli/src`).shouldUseYarn

const spawn = cmd => {
const [file, ...args] = cmd.split(/\s+/)
return execa(file, args)
}

// Returns true if yarn exists, false otherwise
const shouldUseYarn = () => {
try {
execa.sync(`yarnpkg`, `--version`, { stdio: `ignore` })
return true
} catch (e) {
return false
}
}

const makeList = array => {
let list = ``

Expand Down Expand Up @@ -67,9 +76,15 @@ const getVerbs = verb => {
}
}

const addRemovePlugin = async (action, plugins) => {
const addRemovePlugin = async (action, plugins, dryRun) => {
let questions = new Array()

if (dryRun) {
report.warn(
`Dry Run: The workflow will be unchanged, but nothing will be done in the end.`
)
}

plugins.forEach((plugin, index, plugins) => {
if (!plugin.startsWith(`gatsby-`)) {
plugin = `gatsby-` + plugin
Expand All @@ -83,8 +98,7 @@ const addRemovePlugin = async (action, plugins) => {
action.present
} it ${
action.preposition
} 'gatsby-config.js' and 'package.json'.\n Are you sure you want to do this?`,
default: action.present === `add` ? true : false,
} ' package.json'.\n Are you sure you want to do this?`,
})
})

Expand All @@ -104,11 +118,20 @@ const addRemovePlugin = async (action, plugins) => {
try {
spinner.tick(`${action.participle} plugin(s): ${confirmedPlugins}`)

await spawn(
`${shouldUseYarn() ? `yarn` : `npm`} ${action.present} ${pluginString}`
)
if (dryRun) {
report.warn(
`Dry Run: Usually I'd be installing stuff right now, but this is a dry run!`
)
} else {
await spawn(
`${shouldUseYarn() ? `yarn` : `npm`} ${
action.present
} ${pluginString}`
)

report.success(`Successfully ${action.past}: ${pluginString}`)
}

report.success(`Successfully ${action.past}: ${pluginString}`)
spinner.end()

if (action.present === `add`) {
Expand All @@ -133,13 +156,12 @@ const addRemovePlugin = async (action, plugins) => {
module.exports = async program => {
let action = getVerbs(program.action)
let plugins = program.plugins
let dry = program.dryRun

switch (action.present) {
case `add`:
await addRemovePlugin(action, plugins)
break
case `remove`:
await addRemovePlugin(action, plugins)
await addRemovePlugin(action, plugins, dry)
break
case `config`:
report.info(`The future is not yet...but with your PR it could be soon!`)
Expand Down

0 comments on commit e3bbc54

Please sign in to comment.