UNPKG

3.51 kBJavaScriptView Raw
1/* eslint no-console: 0 */
2
3// reusable code for netlify dev
4// bit of a hasty abstraction but recommended by oclif
5const { getAddons } = require('netlify/src/addons')
6const chalk = require('chalk')
7const {
8 NETLIFYDEVLOG,
9 // NETLIFYDEVWARN,
10 NETLIFYDEVERR
11} = require('./logo')
12/**
13 * inject environment variables from netlify addons and buildbot
14 * into your local dev process.env
15 *
16 * ```
17 * // usage example
18 * const { site, api } = this.netlify
19 * if (site.id) {
20 * const accessToken = api.accessToken
21 * const addonUrls = await addEnvVariables(site, accessToken)
22 * // addonUrls is only for startProxy in netlify dev:index
23 * }
24 * ```
25 */
26async function addEnvVariables(api, site, accessToken) {
27 /** from addons */
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 /** from web UI */
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 // TODO: We should move the environment outside of build settings and possibly have a
75 // `/api/v1/sites/:site_id/environment` endpoint for it that we can also gate access to
76 // In the future and that we could make context dependend
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
88module.exports = {
89 addEnvVariables
90}
91
92// if first arg is undefined, use default, but tell user about it in case it is unintentional
93function 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}