Skip to content

Commit

Permalink
[cli] Support specifying dependencies in plugin templates
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Nov 23, 2017
1 parent bdd4aa3 commit 529868b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ module.exports = async (context, url) => {
})
)

return {name, outputPath, inPluginsPath: inProjectContext}
return {name, outputPath, inPluginsPath: inProjectContext, dependencies: tplVars.dependencies}
}

async function validateEmptyPath(dir) {
Expand Down
33 changes: 30 additions & 3 deletions packages/@sanity/cli/src/commands/init/initPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export default async function initPlugin(args, context, initOpts = {}) {
}

async function bootstrapFromUrl(context, url) {
const {output, prompt, workDir} = context
const {name, outputPath, inPluginsPath} = await bootstrapFromTemplate(context, url)
const {output, prompt, yarn, workDir} = context
const {name, outputPath, inPluginsPath, dependencies} = await bootstrapFromTemplate(context, url)

if (inPluginsPath) {
const addIt = await prompt.single({
Expand All @@ -53,5 +53,32 @@ async function bootstrapFromUrl(context, url) {
}
}

output.print(`Success! Plugin initialized at ${outputPath}`)
if (dependencies) {
const dependencyString = JSON.stringify(dependencies, null, 2)
.split('\n')
.slice(1, -1)
.join('\n')
.replace(/"/g, '')

output.print('\nThe following dependencies are required for this template:')
output.print(`${dependencyString}\n`)
}

if (dependencies && inPluginsPath) {
const addDeps = await prompt.single({
type: 'confirm',
message: 'Install dependencies in current project?',
default: true
})

if (addDeps) {
const deps = Object.keys(dependencies).map(dep => `${dep}@${dependencies[dep]}`)
await yarn(['add'].concat(deps), {...output, rootDir: workDir})

output.print('Dependencies installed.')
output.print('Remember to remove them from `package.json` if you no longer need them!')
}
}

output.print(`\nSuccess! Plugin initialized at ${outputPath}`)
}

0 comments on commit 529868b

Please sign in to comment.