Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: clean up addEnvVariables function #1124

Merged
merged 2 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/commands/dev/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ class ExecCommand extends Command {
const { site, api } = this.netlify
if (site.id) {
this.log(`${NETLIFYDEVLOG} Checking your site's environment variables...`) // just to show some visual response first
const accessToken = api.accessToken
const { addEnvVariables } = require('../../utils/dev')
await addEnvVariables(api, site, accessToken)
await addEnvVariables(api, site)
} else {
this.log(
`${NETLIFYDEVERR} No Site ID detected. You probably forgot to run \`netlify link\` or \`netlify init\`. `
Expand Down
44 changes: 26 additions & 18 deletions src/commands/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,29 @@ const getBuildFunction = functionBuilder =>
}
}

const getAddonsUrlsAndAddEnvVariablesToProcessEnv = async ({ api, site, flags }) => {
if (site.id && !flags.offline) {
const { addEnvVariables } = require('../../utils/dev')
const addonUrls = await addEnvVariables(api, site)
return addonUrls
} else {
return {}
}
}

const addDotFileEnvs = async ({ site }) => {
const envSettings = await getEnvSettings(site.root)
if (envSettings.file) {
console.log(
`${NETLIFYDEVLOG} Overriding the following env variables with ${chalk.blue(
path.relative(site.root, envSettings.file)
)} file:`,
chalk.yellow(Object.keys(envSettings.vars))
)
Object.entries(envSettings.vars).forEach(([key, val]) => (process.env[key] = val))
}
}

class DevCommand extends Command {
async run() {
this.log(`${NETLIFYDEV}`)
Expand All @@ -476,26 +499,10 @@ class DevCommand extends Command {
...config.dev,
...flags,
}
let addonUrls = {}

const accessToken = api.accessToken
if (site.id && !flags.offline) {
const { addEnvVariables } = require('../../utils/dev')
addonUrls = await addEnvVariables(api, site, accessToken)
}

const addonUrls = await getAddonsUrlsAndAddEnvVariablesToProcessEnv({ api, site, flags })
process.env.NETLIFY_DEV = 'true'
// Override env variables with .env file
const envSettings = await getEnvSettings(site.root)
if (envSettings.file) {
console.log(
`${NETLIFYDEVLOG} Overriding the following env variables with ${chalk.blue(
path.relative(site.root, envSettings.file)
)} file:`,
chalk.yellow(Object.keys(envSettings.vars))
)
Object.entries(envSettings.vars).forEach(([key, val]) => (process.env[key] = val))
}
await addDotFileEnvs({ site })

let settings = {}
try {
Expand Down Expand Up @@ -553,6 +560,7 @@ class DevCommand extends Command {
}

if (flags.live) {
const accessToken = api.accessToken
await waitPort({ port: settings.frameworkPort, output: 'silent' })
const liveSession = await createTunnel(site.id, accessToken, this.log)
url = liveSession.session_url
Expand Down
6 changes: 3 additions & 3 deletions src/commands/functions/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ async function downloadFromURL(flags, args, functionsDir) {

await installAddons.call(this, addons, path.resolve(fnFolder))
if (onComplete) {
await addEnvVariables(this.netlify.api, this.netlify.site, this.netlify.api.accessToken)
await addEnvVariables(this.netlify.api, this.netlify.site)
await onComplete.call(this)
}
fs.unlinkSync(fnTemplateFile) // delete
Expand Down Expand Up @@ -329,7 +329,7 @@ async function scaffoldFromTemplate(flags, args, functionsDir) {

installAddons.call(this, addons, path.resolve(functionPath))
if (onComplete) {
await addEnvVariables(this.netlify.api, this.netlify.site, this.netlify.api.accessToken)
await addEnvVariables(this.netlify.api, this.netlify.site)
await onComplete.call(this)
}
})
Expand Down Expand Up @@ -357,7 +357,7 @@ async function installAddons(addons = [], fnPath) {
// spinner.success("installed addon: " + addonName);
if (addonDidInstall) {
const { addEnvVariables } = require('../../utils/dev')
await addEnvVariables(api, site, accessToken)
await addEnvVariables(api, site)
const { confirmPostInstall } = await inquirer.prompt([
{
type: 'confirm',
Expand Down
9 changes: 4 additions & 5 deletions src/utils/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ const {
*
* ```
* // usage example
* const { site, api } = this.netlify
* const { api, site } = this.netlify
* if (site.id) {
* const accessToken = api.accessToken
* const addonUrls = await addEnvVariables(site, accessToken)
* const addonUrls = await addEnvVariables(api, site)
* // addonUrls is only for startProxy in netlify dev:index
* }
* ```
*/
async function addEnvVariables(api, site, accessToken) {
async function addEnvVariables(api, site) {
/** from addons */
const addonUrls = {}
const addons = await getAddons(site.id, accessToken).catch(error => {
const addons = await getAddons(site.id, api.accessToken).catch(error => {
console.error(error)
switch (error.status) {
default:
Expand Down