This repository was archived by the owner on Sep 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathaddons.js
327 lines (292 loc) · 10.3 KB
/
addons.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* eslint no-console: 0 */
const { getAddons, createAddon } = require("netlify/src/addons");
// const chalk = require("chalk");
// const fetch = require("node-fetch");
/** main section - shamelessly adapted from CLI. we can extract and dedupe later. */
/** but we can DRY things up later. */
// eslint-disable-next-line max-params
module.exports.createSiteAddon = async function(
accessToken,
addonName,
siteId,
siteData,
log
) {
const addons = await getAddons(siteId, accessToken);
if (typeof addons === "object" && addons.error) {
log("API Error", addons);
return false;
}
// Filter down addons to current args.name
const currentAddon = addons.find(
addon => addon.service_path === `/.netlify/${addonName}`
);
const rawFlags = {};
if (currentAddon && currentAddon.id) {
log(`The "${addonName} add-on" already exists for ${siteData.name}`);
// // just exit
// log()
// const cmd = chalk.cyan(`\`netlify addons:config ${addonName}\``)
// log(`- To update this add-on run: ${cmd}`)
// const deleteCmd = chalk.cyan(`\`netlify addons:delete ${addonName}\``)
// log(`- To remove this add-on run: ${deleteCmd}`)
// log()
return false;
}
// const manifest = await getAddonManifest(addonName, accessToken);
let configValues = rawFlags;
// if (manifest.config) {
// const required = requiredConfigValues(manifest.config);
// console.log(`Starting the setup for "${addonName} add-on"`);
// console.log();
// // const missingValues = missingConfigValues(required, rawFlags);
// // if (Object.keys(rawFlags).length) {
// // const newConfig = updateConfigValues(manifest.config, {}, rawFlags)
// // if (missingValues.length) {
// // /* Warn user of missing required values */
// // console.log(
// // `${chalk.redBright.underline.bold(`Error: Missing required configuration for "${addonName} add-on"`)}`
// // )
// // console.log()
// // render.missingValues(missingValues, manifest)
// // console.log()
// // const msg = `netlify addons:create ${addonName}`
// // console.log(`Please supply the configuration values as CLI flags`)
// // console.log()
// // console.log(`Alternatively, you can run ${chalk.cyan(msg)} with no flags to walk through the setup steps`)
// // console.log()
// // return false
// // }
// // await createSiteAddon({
// // addonName,
// // settings: {
// // siteId: siteId,
// // addon: addonName,
// // config: newConfig
// // },
// // accessToken,
// // siteData
// // })
// // return false
// // }
// const words = `The ${addonName} add-on has the following configurable options:`;
// console.log(` ${chalk.yellowBright.bold(words)}`);
// render.configValues(addonName, manifest.config);
// console.log();
// console.log(` ${chalk.greenBright.bold("Lets configure those!")}`);
// console.log();
// console.log(
// ` - Hit ${chalk.white.bold("enter")} to confirm value or set empty value`
// );
// console.log(
// ` - Hit ${chalk.white.bold("ctrl + C")} to cancel & exit configuration`
// );
// console.log();
// const prompts = generatePrompts({
// config: manifest.config,
// configValues: rawFlags
// });
// const userInput = await inquirer.prompt(prompts);
// // Merge user input with the flags specified
// configValues = updateConfigValues(manifest.config, rawFlags, userInput);
// const missingRequiredValues = missingConfigValues(required, configValues);
// if (missingRequiredValues && missingRequiredValues.length) {
// missingRequiredValues.forEach(val => {
// console.log(
// `Missing required value "${val}". Please run the command again`
// );
// });
// return false;
// }
// }
await actuallyCreateSiteAddon({
addonName,
settings: {
siteId: siteId,
addon: addonName,
config: configValues
},
accessToken,
siteData
});
return addonName; // we dont really use this right now but may be helpful to know that an addon installation was successful
};
async function actuallyCreateSiteAddon({
addonName,
settings,
accessToken,
siteData
}) {
const addonResponse = await createAddon(settings, accessToken);
if (addonResponse.code === 404) {
console.log(
`No add-on "${addonName}" found. Please double check your add-on name and try again`
);
return false;
}
console.log(`Add-on "${addonName}" created for ${siteData.name}`);
if (addonResponse.config && addonResponse.config.message) {
console.log();
console.log(`${addonResponse.config.message}`);
}
return addonResponse;
}
/** all the utils used in the main section */
// async function getAddonManifest(addonName, netlifyApiToken) {
// const url = `https://api.netlify.com/api/v1/services/${addonName}/manifest`;
// const response = await fetch(url, {
// method: "GET",
// headers: {
// "Content-Type": "application/json",
// Authorization: `Bearer ${netlifyApiToken}`
// }
// });
// const data = await response.json();
// if (response.status === 422) {
// throw new Error(`Error ${JSON.stringify(data)}`);
// }
// return data;
// }
// function requiredConfigValues(config) {
// return Object.keys(config).filter(key => {
// return config[key].required;
// });
// }
// function missingConfigValues(requiredConfig, providedConfig) {
// return requiredConfig.filter(key => {
// return !providedConfig[key];
// });
// }
// function missingConfigValues(allowedConfig, currentConfig, newConfig) {
// return Object.keys(allowedConfig).reduce((acc, key) => {
// if (newConfig[key]) {
// acc[key] = newConfig[key];
// return acc;
// }
// acc[key] = currentConfig[key];
// return acc;
// }, {});
// }
// const chalk = require('chalk')
// /* programmatically generate CLI prompts */
// function generatePrompts(settings) {
// const { config, configValues } = settings;
// const configItems = Object.keys(config);
// const prompts = configItems
// .map((key, i) => {
// const setting = config[key];
// // const { type, displayName } = setting
// let prompt;
// // Tell user to use types
// if (!setting.type) {
// console.log(
// `⚠️ ${chalk.yellowBright(
// `Warning: no \`type\` is set for config key: ${configItems[i]}`
// )}`
// );
// console.log(
// `It's highly recommended that you type your configuration values. It will help with automatic documentation, sharing of your services, and make your services configurable through a GUI`
// );
// console.log("");
// }
// // Handle shorthand config. Probably will be removed. Severly limited + not great UX
// if (typeof setting === "string" || typeof setting === "boolean") {
// if (typeof setting === "string") {
// prompt = {
// type: "input",
// name: key,
// message: `Enter string value for '${key}':`
// };
// // if current stage value set show as default
// if (configValues[key]) {
// prompt.default = configValues[key];
// }
// } else if (typeof setting === "boolean") {
// prompt = {
// type: "confirm",
// name: key,
// message: `Do you want '${key}':`
// };
// }
// return prompt;
// }
// // For future use. Once UX is decided
// // const defaultValidation = (setting.required) ? validateRequired : noValidate
// const defaultValidation = noValidate;
// const validateFunction = setting.pattern
// ? validate(setting.pattern)
// : defaultValidation;
// const isRequiredText = setting.required
// ? ` (${chalk.yellow("required")})`
// : "";
// if (setting.type === "string" || setting.type.match(/string/)) {
// prompt = {
// type: "input",
// name: key,
// message:
// `${chalk.white(key)}${isRequiredText} - ${setting.displayName}` ||
// `Please enter value for ${key}`,
// validate: validateFunction
// };
// // if value previously set show it
// if (configValues[key]) {
// prompt.default = configValues[key];
// // else show default value if provided
// } else if (setting.default) {
// prompt.default = setting.default;
// }
// return prompt;
// }
// return undefined;
// })
// .filter(item => {
// return typeof item !== "undefined";
// });
// return prompts;
// }
// function noValidate() {
// return true;
// }
// function validate(pattern) {
// return function(value) {
// const regex = new RegExp(pattern);
// if (value.match(regex)) {
// return true;
// }
// return `Please enter a value matching regex pattern: /${chalk.yellowBright(
// pattern
// )}/`;
// };
// }
// const chalk = require('chalk')
// const AsciiTable = require("ascii-table");
// function missingValues(values, manifest) {
// const display = values
// .map(item => {
// const itemDisplay = chalk.redBright.bold(`${item}`);
// const niceNameDisplay = manifest.config[item].displayName;
// return ` - ${itemDisplay} ${niceNameDisplay}`;
// })
// .join("\n");
// console.log(display);
// }
// function configValues(addonName, configValues, currentValue) {
// const table = new AsciiTable(`${addonName} add-on settings`);
// const tableHeader = currentValue
// ? ["Setting Name", "Current Value", "Description"]
// : ["Setting Name", "Description", "Type", "Required"];
// table.setHeading(...tableHeader);
// Object.keys(configValues).map(key => {
// const { type, displayName, required } = configValues[key];
// let requiredText = required ? `true` : `false`;
// const typeInfo = type || "";
// const description = displayName || "";
// if (currentValue) {
// const value = currentValue[key] || "Not supplied";
// table.addRow(key, value, description);
// } else {
// table.addRow(key, description, typeInfo, requiredText);
// }
// });
// console.log(table.toString());
// }