Skip to content

Commit

Permalink
feat: allow frameworks to detect version of CLI that's being used (#6226
Browse files Browse the repository at this point in the history
)

* refactor: typescriptify a bit

* feat: inject Netlify CLI Version to build command

* chore: also test for ntl dev

* fix: dont show internal variables in env:list
  • Loading branch information
Skn0tt authored Jan 8, 2024
1 parent 10d470d commit 1066ff3
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/commands/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
exit,
getToken,
log,
netlifyCliVersion,
normalizeConfig,
padLeft,
pollForToken,
Expand Down Expand Up @@ -546,7 +547,8 @@ export default class BaseCommand extends Command {
token,
...apiUrlOpts,
})
const { buildDir, config, configPath, repositoryRoot, siteInfo } = cachedConfig
const { buildDir, config, configPath, env, repositoryRoot, siteInfo } = cachedConfig
env.NETLIFY_CLI_VERSION = { sources: ['internal'], value: netlifyCliVersion }
const normalizedConfig = normalizeConfig(config)
const agent = await getAgent({
httpProxy: flags.httpProxy,
Expand Down
6 changes: 4 additions & 2 deletions src/commands/env/env-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ export const envList = async (options: OptionValues, command: BaseCommand) => {

// filter out general sources
environment = Object.fromEntries(
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
Object.entries(environment).filter(([, variable]) => variable.sources[0] !== 'general'),
Object.entries(environment).filter(
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
([, variable]) => variable.sources[0] !== 'general' && variable.sources[0] !== 'internal',
),
)

// Return json response for piping commands
Expand Down
3 changes: 2 additions & 1 deletion src/utils/command-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ const arch = os.arch() === 'ia32' ? 'x86' : os.arch()

const { name, version } = await getPackageJson()

export const USER_AGENT = `${name}/${version} ${platform}-${arch} node-${process.version}`
export const netlifyCliVersion = version
export const USER_AGENT = `${name}/${netlifyCliVersion} ${platform}-${arch} node-${process.version}`

/** A list of base command flags that needs to be sorted down on documentation and on help pages */
const BASE_FLAGS = new Set(['--debug', '--httpProxy', '--httpProxyCertificateFilename'])
Expand Down
29 changes: 28 additions & 1 deletion tests/integration/commands/build/build.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path'
import process from 'process'

import execa from 'execa'
import { describe, test } from 'vitest'
Expand Down Expand Up @@ -38,7 +39,11 @@ const runBuildCommand = async function (
outputs = [outputs]
}
outputs.forEach((output) => {
t.expect(all.includes(output), `Output of build command does not include '${output}'`).toBe(true)
if (output instanceof RegExp) {
t.expect(all).toMatch(output)
} else {
t.expect(all.includes(output), `Output of build command does not include '${output}'`).toBe(true)
}
})
t.expect(exitCode).toBe(expectedExitCode)
}
Expand Down Expand Up @@ -286,4 +291,26 @@ describe.concurrent('command/build', () => {
})
})
})

test('should have version in NETLIFY_CLI_VERSION variable', async (t) => {
await withSiteBuilder('NETLIFY_CLI_VERSION-env', async (builder) => {
await builder
.withNetlifyToml({
config: {
build: {
command:
process.platform === 'win32'
? 'echo NETLIFY_CLI_VERSION=%NETLIFY_CLI_VERSION%'
: 'echo NETLIFY_CLI_VERSION=$NETLIFY_CLI_VERSION',
},
},
})
.build()

await runBuildCommand(t, builder.directory, {
output: /NETLIFY_CLI_VERSION=\d+\.\d+.\d+/,
flags: ['--offline'],
})
})
})
})
35 changes: 35 additions & 0 deletions tests/integration/commands/dev/dev.config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,41 @@ describe.concurrent('commands/dev/config', () => {
})
})

test('should provide CLI version in env var', async (t) => {
await withSiteBuilder('site-with-netlify-version-env-var', async (builder) => {
await builder
.withContentFile({
content: `
import http from "http";
http.createServer((req, res) => {
res.write(JSON.stringify({
NETLIFY_CLI_VERSION: process.env.NETLIFY_CLI_VERSION,
}))
res.end()
}).listen(1234);
`,
path: 'devserver.mjs',
})
.withNetlifyToml({
config: {
dev: {
framework: '#custom',
command: 'node devserver.mjs',
targetPort: 1234,
},
},
})
.build()

await withDevServer({ cwd: builder.directory }, async (server) => {
const resp = await fetch(server.url)
const { NETLIFY_CLI_VERSION } = await resp.json()
t.expect(NETLIFY_CLI_VERSION).toMatch(/\d+\.\d+\.\d+/)
})
})
})

test('should set value of the CONTEXT env variable', async (t) => {
await withSiteBuilder('site-with-context-override', async (builder) => {
builder.withNetlifyToml({ config: { functions: { directory: 'functions' } } }).withFunction({
Expand Down

2 comments on commit 1066ff3

@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

  • Dependency count: 1,410
  • Package size: 422 MB
  • Number of ts-expect-error directives: 1,189

@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

  • Dependency count: 1,410
  • Package size: 422 MB
  • Number of ts-expect-error directives: 1,189

Please sign in to comment.