Skip to content

Commit

Permalink
[dev] Add support for injecting shared environment variables (#699)
Browse files Browse the repository at this point in the history
  • Loading branch information
RaeesBhatti authored Mar 5, 2020
1 parent 234e1a2 commit 57e4974
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions src/utils/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,31 @@ async function addEnvVariables(api, site, accessToken) {
}

/** from web UI */
const apiSite = await api.getSite({ site_id: site.id }).catch(error => {
console.error(error)
switch (error.status) {
case 401:
console.error(
`${NETLIFYDEVERR} Unauthorized error: This Site ID ${chalk.yellow(site.id)} does not belong to your account.`
)
console.error(
`${NETLIFYDEVERR} If you cloned someone else's code, try running 'npm unlink' and then 'npm init' or 'npm link'.`
)
const [apiSite, accounts] = await Promise.all([api.getSite({ site_id: site.id }), api.listAccountsForUser()]).catch(
error => {
console.error(error)
switch (error.status) {
case 401:
console.error(
`${NETLIFYDEVERR} Unauthorized error: This Site ID ${chalk.yellow(
site.id
)} does not belong to your account.`
)
console.error(
`${NETLIFYDEVERR} If you cloned someone else's code, try running 'npm unlink' and then 'npm init' or 'npm link'.`
)

process.exit()
default:
console.error(
`${NETLIFYDEVERR} Error retrieving site data for site ${chalk.yellow(
site.id
)}. Double-check your login status with 'netlify status' or contact support with details of your error.`
)
process.exit()
process.exit()
default:
console.error(
`${NETLIFYDEVERR} Error retrieving site data for site ${chalk.yellow(
site.id
)}. Double-check your login status with 'netlify status' or contact support with details of your error.`
)
process.exit()
}
}
})
)
// TODO: We should move the environment outside of build settings and possibly have a
// `/api/v1/sites/:site_id/environment` endpoint for it that we can also gate access to
// In the future and that we could make context dependend
Expand All @@ -82,6 +86,17 @@ async function addEnvVariables(api, site, accessToken) {
}
}

const siteAccount = accounts.find(acc => acc.slug === apiSite.account_slug)
if (siteAccount && siteAccount.site_env) {
for (const key in siteAccount.site_env) {
const msg = () =>
console.log(
`${NETLIFYDEVLOG} Injected ${chalk.blue.bold('shared build setting')} env var: ${chalk.yellow(key)}`
)
process.env[key] = assignLoudly(process.env[key], siteAccount.site_env[key], msg)
}
}

return addonUrls
}

Expand Down

0 comments on commit 57e4974

Please sign in to comment.