-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cli] Allow bootstrapping plugins from templates (#355)
* [cli] Remove sanity-style plugin init code * [cli] Allow bootstrapping plugins from templates * [cli] Add minimum version check to plugin template bootstrap * [cli] Add tool templates * [cli] Don't allow bootstrapping to existing plugin folder
- Loading branch information
Showing
14 changed files
with
276 additions
and
202 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
126 changes: 126 additions & 0 deletions
126
packages/@sanity/cli/src/actions/init-plugin/bootstrapFromTemplate.js
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,126 @@ | ||
const path = require('path') | ||
const semver = require('semver') | ||
const fse = require('fs-extra') | ||
const simpleGet = require('simple-get') | ||
const decompress = require('decompress') | ||
const resolveFrom = require('resolve-from') | ||
const validateNpmPackageName = require('validate-npm-package-name') | ||
const {pathTools} = require('@sanity/util') | ||
const pkg = require('../../../package.json') | ||
const {absolutify, pathIsEmpty} = pathTools | ||
|
||
module.exports = async (context, url) => { | ||
const {prompt, workDir} = context | ||
let inProjectContext = false | ||
try { | ||
const projectManifest = await fse.readJson(path.join(workDir, 'sanity.json')) | ||
inProjectContext = Boolean(projectManifest.root) | ||
} catch (err) { | ||
// Intentional noop | ||
} | ||
|
||
let zip | ||
try { | ||
zip = await getZip(url) | ||
} catch (err) { | ||
err.message = `Failed to get template: ${err.message}` | ||
throw err | ||
} | ||
|
||
const manifest = zip.find(file => path.basename(file.path) === 'package.json') | ||
const baseDir = path.join(path.dirname(manifest.path), 'template') | ||
const templateFiles = zip.filter(file => file.type === 'file' && file.path.indexOf(baseDir) === 0) | ||
const manifestContent = manifest.data.toString() | ||
const tplVars = parseJson(manifestContent).sanityTemplate || {} | ||
const {minimumBaseVersion} = tplVars | ||
|
||
if (minimumBaseVersion) { | ||
const installed = getSanityVersion(workDir) | ||
if (semver.lt(installed, minimumBaseVersion)) { | ||
throw new Error( | ||
`Template requires Sanity at version ${minimumBaseVersion}, installed is ${installed}` | ||
) | ||
} | ||
} | ||
|
||
const name = await prompt.single({ | ||
type: 'input', | ||
message: 'Plugin name:', | ||
default: tplVars.suggestedName || '', | ||
validate: async pkgName => { | ||
const {validForNewPackages, errors} = validateNpmPackageName(pkgName) | ||
if (!validForNewPackages) { | ||
return errors[0] | ||
} | ||
|
||
const outputPath = path.join(workDir, 'plugins', pkgName) | ||
const isEmpty = await pathIsEmpty(outputPath) | ||
if (inProjectContext && !isEmpty) { | ||
return 'Plugin with given name already exists in project' | ||
} | ||
|
||
return true | ||
} | ||
}) | ||
|
||
let outputPath = path.join(workDir, 'plugins', name) | ||
if (!inProjectContext) { | ||
outputPath = await prompt.single({ | ||
type: 'input', | ||
message: 'Output path:', | ||
default: workDir, | ||
validate: validateEmptyPath, | ||
filter: absolutify | ||
}) | ||
} | ||
|
||
let createConfig = tplVars.requiresConfig | ||
if (typeof createConfig === 'undefined') { | ||
createConfig = await prompt.single({ | ||
type: 'confirm', | ||
message: 'Does the plugin need a configuration file?', | ||
default: false | ||
}) | ||
} | ||
|
||
await fse.ensureDir(outputPath) | ||
await Promise.all( | ||
templateFiles.map(file => { | ||
const filename = file.path.slice(baseDir.length) | ||
return fse.outputFile(path.join(outputPath, filename), file.data) | ||
}) | ||
) | ||
|
||
return {name, outputPath, inPluginsPath: inProjectContext} | ||
} | ||
|
||
async function validateEmptyPath(dir) { | ||
const isEmpty = await pathIsEmpty(dir) | ||
return isEmpty ? true : 'Path is not empty' | ||
} | ||
|
||
function getZip(url) { | ||
return new Promise((resolve, reject) => { | ||
simpleGet.concat(url, (err, res, data) => { | ||
if (err) { | ||
reject(err) | ||
return | ||
} | ||
|
||
resolve(decompress(data)) | ||
}) | ||
}) | ||
} | ||
|
||
function parseJson(json) { | ||
try { | ||
return JSON.parse(json) | ||
} catch (err) { | ||
return {} | ||
} | ||
} | ||
|
||
function getSanityVersion(workDir) { | ||
const basePkg = resolveFrom.silent(workDir, '@sanity/base/package.json') | ||
return basePkg ? require(basePkg).version : pkg.version | ||
} |
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
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
Oops, something went wrong.