Skip to content

Commit

Permalink
feat: inject netlify graph environment variables during ntl build (#…
Browse files Browse the repository at this point in the history
…4363)

* feat: inject netlify graph environment variables during `ntl build`

* fix: honor `--offline`
  • Loading branch information
anmonteiro authored Feb 28, 2022
1 parent 550c5c0 commit eecb146
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/commands/build/build.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-check
const { getBuildOptions, runBuild } = require('../../lib/build')
const { error, exit, getToken } = require('../../utils')
const { error, exit, generateNetlifyGraphJWT, getToken } = require('../../utils')

/**
* @param {import('../../lib/build').BuildConfig} options
Expand All @@ -15,6 +15,32 @@ const checkOptions = ({ cachedConfig: { siteInfo = {} }, token }) => {
}
}

const injectNetlifyGraphEnv = async function (command, { api, buildOptions, site }) {
const siteData = await api.getSite({ siteId: site.id })
const authlifyTokenId = siteData && siteData.authlify_token_id

if (authlifyTokenId) {
const netlifyToken = await command.authenticate()
// Only inject the authlify config if a token ID exists. This prevents
// calling command.authenticate() (which opens a browser window) if the
// user hasn't enabled API Authentication
const netlifyGraphConfig = {
netlifyToken,
authlifyTokenId,
siteId: site.id,
}

const netlifyGraphJWT = generateNetlifyGraphJWT(netlifyGraphConfig)

if (netlifyGraphJWT != null) {
// XXX(anmonteiro): this name is deprecated. Delete after 3/31/2022
const varData = { sources: ['general'], value: netlifyGraphJWT }
buildOptions.cachedConfig.env.ONEGRAPH_AUTHLIFY_TOKEN = varData
buildOptions.cachedConfig.env.NETLIFY_GRAPH_TOKEN = varData
}
}
}

/**
* The build command
* @param {import('commander').OptionValues} options
Expand All @@ -34,6 +60,8 @@ const build = async (options, command) => {

if (!options.offline) {
checkOptions(buildOptions)
const { api, site } = command.netlify
await injectNetlifyGraphEnv(command, { api, site, buildOptions })
}

const { exitCode } = await runBuild(buildOptions)
Expand Down

1 comment on commit eecb146

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

Package size: 443 MB

Please sign in to comment.