forked from sanity-io/sanity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstallCommand.js
79 lines (69 loc) · 2.72 KB
/
installCommand.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import fse from 'fs-extra'
import path from 'path'
import generateConfigChecksum from '../../util/generateConfigChecksum'
import addPluginToManifest from '@sanity/util/lib/addPluginToManifest'
import {setChecksum, hasSameChecksum} from '../../util/pluginChecksumManifest'
export default {
name: 'install',
signature: '[PLUGIN]',
description: 'Installs a Sanity plugin to the current Sanity configuration',
action: async (args, context) => {
const {extOptions} = args
const {yarn} = context
const [plugin] = args.argsWithoutOptions
if (!plugin) {
const flags = extOptions.offline ? ['--offline'] : []
return yarn(['install'].concat(flags), context)
}
// @todo add support for multiple simultaneous plugins to be installed
return installPlugin(plugin, context)
},
}
async function installPlugin(plugin, context) {
const {output, workDir, yarn} = context
const isNamespaced = plugin[0] === '@'
let shortName = plugin
let fullName = plugin
if (!isNamespaced) {
const isFullName = plugin.indexOf('sanity-plugin-') === 0
shortName = isFullName ? plugin.substr(14) : plugin
fullName = isFullName ? plugin : `sanity-plugin-${plugin}`
}
await yarn(['add', fullName], context)
await addPluginToManifest(workDir, shortName)
await copyConfiguration(workDir, fullName, shortName, output)
output.print(`Plugin '${fullName}' installed`)
}
async function copyConfiguration(rootDir, fullName, shortName, output) {
const configPath = path.join(rootDir, 'node_modules', fullName, 'config.dist.json')
const dstPath = path.join(rootDir, 'config', `${shortName}.json`)
if (!fse.existsSync(configPath)) {
return
}
// Configuration exists, check if user has local configuration already
if (
// eslint-disable-line no-constant-condition
false /* disabled for now until we can offer the user a way to fix this */ &&
fse.existsSync(dstPath)
) {
const distChecksum = await generateConfigChecksum(configPath)
const sameChecksum = await hasSameChecksum(rootDir, fullName, distChecksum)
warnOnDifferentChecksum(shortName, sameChecksum, output.print)
} else {
// Destination file does not exist, copy
await fse.copy(configPath, dstPath)
const checksum = await generateConfigChecksum(configPath)
await setChecksum(rootDir, fullName, checksum)
}
}
// @todo Improve with some sort of helpful key differ or similar
function warnOnDifferentChecksum(plugin, sameChecksum, printer) {
if (!sameChecksum) {
printer(
[
`[Warning] Default configuration for plugin '${plugin}' has changed since you first installed it,`,
'check local configuration vs distributed configuration to ensure your configuration is up to date',
].join(' ')
)
}
}