-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathaddons-config.ts
164 lines (149 loc) · 5.42 KB
/
addons-config.ts
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { OptionValues } from 'commander'
import Enquirer from 'enquirer'
import isEmpty from 'lodash/isEmpty.js'
import compare from '../../utils/addons/compare.js'
import diffValues from '../../utils/addons/diffs/index.js'
import { ADDON_VALIDATION, prepareAddonCommand } from '../../utils/addons/prepare.js'
import generatePrompts from '../../utils/addons/prompts.js'
import { renderConfigValues } from '../../utils/addons/render.js'
import { missingConfigValues, requiredConfigValues, updateConfigValues } from '../../utils/addons/validation.js'
import { chalk, error, log } from '../../utils/command-helpers.js'
import { parseRawFlags } from '../../utils/parse-raw-flags.js'
import BaseCommand from '../base-command.js'
// @ts-expect-error TS(7031) FIXME: Binding element 'addonName' implicitly has an 'any... Remove this comment to see the full error message
const update = async function ({ addonName, api, currentConfig, instanceId, newConfig, siteId }) {
const codeDiff = diffValues(currentConfig, newConfig)
if (!codeDiff) {
log('No changes, exiting early')
return false
}
log()
const msg = `Updating ${addonName} add-on config values...`
log(`${chalk.white.bold(msg)}`)
log()
log(`${codeDiff}\n`)
log()
try {
await api.updateServiceInstance({
siteId,
addon: addonName,
instanceId,
body: { config: newConfig },
})
log(`Add-on "${addonName}" successfully updated`)
} catch (error_) {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
error(error_.message)
}
}
export const addonsConfig = async (addonName: string, options: OptionValues, command: BaseCommand) => {
const { addon, manifest, siteData } = await prepareAddonCommand({
command,
addonName,
validation: ADDON_VALIDATION.EXISTS,
})
const { api, site } = command.netlify
const siteId = site.id
const hasConfig = !isEmpty(manifest.config)
// Parse flags
const rawFlags = parseRawFlags(command.args)
// Get Existing Config
const currentConfig = addon.config || {}
const words = `Current "${addonName} add-on" Settings:`
log(` ${chalk.yellowBright.bold(words)}`)
if (hasConfig) {
if (!rawFlags.silent) {
renderConfigValues(addonName, manifest.config, currentConfig)
}
} else {
// For addons without manifest. TODO remove once we enforce manifests
Object.keys(currentConfig).forEach((key) => {
log(`${key} - ${currentConfig[key]}`)
})
}
if (hasConfig) {
const required = requiredConfigValues(manifest.config)
const missingValues = missingConfigValues(required, rawFlags)
/* Config set by command line flags */
if (rawFlags && missingValues.length === 0) {
const newConfig = updateConfigValues(manifest.config, currentConfig, rawFlags)
await update({
addonName,
currentConfig,
newConfig,
siteId,
instanceId: addon.id,
api,
})
return false
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const updatePrompt = await Enquirer.prompt<any>([
{
type: 'confirm',
name: 'updateNow',
message: `Do you want to update config values?`,
initial: false,
},
])
if (!updatePrompt.updateNow) {
log('Sounds good! Exiting configuration...')
return false
}
log()
log(` - Hit ${chalk.white.bold('enter')} to keep the existing value in (parentheses)`)
log(` - Hit ${chalk.white.bold('down arrow')} to remove the value`)
log(` - Hit ${chalk.white.bold('ctrl + C')} to cancel & exit configuration`)
log()
log(` You will need to verify the changed before we push them to your live site!`)
log()
const prompts = generatePrompts({
config: manifest.config,
configValues: currentConfig,
})
// TODO: Fix type argument
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const userInput = await Enquirer.prompt<any>(prompts as any)
// Merge user input with the flags specified
const newConfig = updateConfigValues(manifest.config, currentConfig, userInput)
const diffs = compare(currentConfig, newConfig)
// log('compare', diffs)
// @ts-expect-error TS(2339) FIXME: Property 'isEqual' does not exist on type 'string'... Remove this comment to see the full error message
if (diffs.isEqual) {
log(`No changes. exiting early`)
return false
}
log()
log(`${chalk.yellowBright.bold.underline('Confirm your updates:')}`)
log()
// @ts-expect-error TS(2339) FIXME: Property 'keys' does not exist on type 'string'.
diffs.keys.forEach((key) => {
// @ts-expect-error TS(2339) FIXME: Property 'diffs' does not exist on type 'string'.
const { newValue, oldValue } = diffs.diffs[key]
const oldVal = oldValue || 'NO VALUE'
log(`${chalk.cyan(key)} changed from ${chalk.whiteBright(oldVal)} to ${chalk.green(newValue)}`)
})
log()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const confirmPrompt = await Enquirer.prompt<any>([
{
type: 'confirm',
name: 'confirmChange',
message: `Do you want to publish the updated "${addonName} add-on" settings for ${chalk.cyan(siteData.name)}?`,
initial: false,
},
])
if (!confirmPrompt.confirmChange) {
log('Canceling changes... You are good to go!')
return false
}
await update({
addonName,
currentConfig,
newConfig,
siteId,
instanceId: addon.id,
api,
})
}
}