1 |
|
2 |
|
3 |
|
4 |
|
5 | const { getAddons } = require('netlify/src/addons')
|
6 | const chalk = require('chalk')
|
7 | const {
|
8 | NETLIFYDEVLOG,
|
9 |
|
10 | NETLIFYDEVERR
|
11 | } = require('./logo')
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | async function addEnvVariables(api, site, accessToken) {
|
27 |
|
28 | const addonUrls = {}
|
29 | const addons = await getAddons(site.id, accessToken).catch(error => {
|
30 | console.error(error)
|
31 | switch (error.status) {
|
32 | default:
|
33 | console.error(
|
34 | `${NETLIFYDEVERR} Error retrieving addons data for site ${chalk.yellow(
|
35 | site.id
|
36 | )}. Double-check your login status with 'netlify status' or contact support with details of your error.`
|
37 | )
|
38 | process.exit()
|
39 | }
|
40 | })
|
41 | if (Array.isArray(addons)) {
|
42 | addons.forEach(addon => {
|
43 | addonUrls[addon.slug] = `${addon.config.site_url}/.netlify/${addon.slug}`
|
44 | for (const key in addon.env) {
|
45 | const msg = () =>
|
46 | console.log(`${NETLIFYDEVLOG} Injected ${chalk.yellow.bold('addon')} env var: `, chalk.yellow(key))
|
47 | process.env[key] = assignLoudly(process.env[key], addon.env[key], msg)
|
48 | }
|
49 | })
|
50 | }
|
51 |
|
52 |
|
53 | const apiSite = await api.getSite({ site_id: site.id }).catch(error => {
|
54 | console.error(error)
|
55 | switch (error.status) {
|
56 | case 401:
|
57 | console.error(
|
58 | `${NETLIFYDEVERR} Unauthorized error: This Site ID ${chalk.yellow(site.id)} does not belong to your account.`
|
59 | )
|
60 | console.error(
|
61 | `${NETLIFYDEVERR} If you cloned someone else's code, try running 'npm unlink' and then 'npm init' or 'npm link'.`
|
62 | )
|
63 |
|
64 | process.exit()
|
65 | default:
|
66 | console.error(
|
67 | `${NETLIFYDEVERR} Error retrieving site data for site ${chalk.yellow(
|
68 | site.id
|
69 | )}. Double-check your login status with 'netlify status' or contact support with details of your error.`
|
70 | )
|
71 | process.exit()
|
72 | }
|
73 | })
|
74 |
|
75 |
|
76 |
|
77 | if (apiSite.build_settings && apiSite.build_settings.env) {
|
78 | for (const key in apiSite.build_settings.env) {
|
79 | const msg = () =>
|
80 | console.log(`${NETLIFYDEVLOG} Injected ${chalk.blue.bold('build setting')} env var: ${chalk.yellow(key)}`)
|
81 | process.env[key] = assignLoudly(process.env[key], apiSite.build_settings.env[key], msg)
|
82 | }
|
83 | }
|
84 |
|
85 | return addonUrls
|
86 | }
|
87 |
|
88 | module.exports = {
|
89 | addEnvVariables
|
90 | }
|
91 |
|
92 |
|
93 | function assignLoudly(
|
94 | optionalValue,
|
95 | defaultValue,
|
96 | tellUser = dV => console.log(`No value specified, using fallback of `, dV)
|
97 | ) {
|
98 | if (defaultValue === undefined) throw new Error('must have a defaultValue')
|
99 | if (defaultValue !== optionalValue && optionalValue === undefined) {
|
100 | tellUser(defaultValue)
|
101 | return defaultValue
|
102 | }
|
103 | return optionalValue
|
104 | }
|