Skip to content

Commit

Permalink
refactor: type cachedConfig.env (#6288)
Browse files Browse the repository at this point in the history
* refactor: type cachedConfig.env

* refactor: rename to less verbose `version`

* refactor: use getPort

* refactor: replace some deprecated signatures

* Update src/commands/types.d.ts

Co-authored-by: Eduardo Bouças <[email protected]>

---------

Co-authored-by: Eduardo Bouças <[email protected]>
  • Loading branch information
Skn0tt and eduardoboucas authored Jan 8, 2024
1 parent 1066ff3 commit e1372a3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/commands/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
exit,
getToken,
log,
netlifyCliVersion,
version,
normalizeConfig,
padLeft,
pollForToken,
Expand Down Expand Up @@ -548,7 +548,7 @@ export default class BaseCommand extends Command {
...apiUrlOpts,
})
const { buildDir, config, configPath, env, repositoryRoot, siteInfo } = cachedConfig
env.NETLIFY_CLI_VERSION = { sources: ['internal'], value: netlifyCliVersion }
env.NETLIFY_CLI_VERSION = { sources: ['internal'], value: version }
const normalizedConfig = normalizeConfig(config)
const agent = await getAgent({
httpProxy: flags.httpProxy,
Expand Down
19 changes: 11 additions & 8 deletions src/commands/env/env-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,31 @@ import logUpdate from 'log-update'
import { chalk, error, log, logJson } from '../../utils/command-helpers.js'
import { AVAILABLE_CONTEXTS, getEnvelopeEnv, getHumanReadableScopes } from '../../utils/env/index.js'
import BaseCommand from '../base-command.js'
import { EnvironmentVariables } from '../types.js'

const MASK_LENGTH = 50
const MASK = '*'.repeat(MASK_LENGTH)

// @ts-expect-error TS(7031) FIXME: Binding element 'environment' implicitly has an 'a... Remove this comment to see the full error message
const getTable = ({ environment, hideValues, scopesColumn }) => {
const getTable = ({
environment,
hideValues,
scopesColumn,
}: {
environment: EnvironmentVariables
hideValues: boolean
scopesColumn: boolean
}) => {
const table = new AsciiTable(`Environment variables`)
const headings = ['Key', 'Value', scopesColumn && 'Scope'].filter(Boolean)
const headings = ['Key', 'Value', scopesColumn && 'Scope'].filter(Boolean) as string[]
table.setHeading(...headings)
table.addRowMatrix(
Object.entries(environment).map(([key, variable]) =>
[
// Key
key,
// Value
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
hideValues ? MASK : variable.value || ' ',
// Scope
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
scopesColumn && getHumanReadableScopes(variable.scopes),
].filter(Boolean),
),
Expand Down Expand Up @@ -62,15 +68,13 @@ export const envList = async (options: OptionValues, command: BaseCommand) => {
// filter out general sources
environment = Object.fromEntries(
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
if (options.json) {
const envDictionary = Object.fromEntries(
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
Object.entries(environment).map(([key, variable]) => [key, variable.value]),
)
logJson(envDictionary)
Expand All @@ -79,7 +83,6 @@ export const envList = async (options: OptionValues, command: BaseCommand) => {

if (options.plain) {
const plaintext = Object.entries(environment)
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
.map(([key, variable]) => `${key}=${variable.value}`)
.join('\n')
log(plaintext)
Expand Down
7 changes: 6 additions & 1 deletion src/commands/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ type PatchedConfig = NetlifyTOML & {
}
}

type EnvironmentVariableScope = 'builds' | 'functions' | 'runtime' | 'post_processing'
type EnvironmentVariableSource = 'account' | 'addons' | 'configFile' | 'general' | 'internal' | 'ui'

export type EnvironmentVariables = Record<string, { sources: EnvironmentVariableSource[], value: string; scopes?: EnvironmentVariableScope[] }>

/**
* The netlify object inside each command with the state
*/
Expand All @@ -39,7 +44,7 @@ export type NetlifyOptions = {
site: NetlifySite
siteInfo: $TSFixMe
config: PatchedConfig
cachedConfig: Record<string, $TSFixMe>
cachedConfig: Record<string, $TSFixMe> & { env: EnvironmentVariables }
globalConfig: $TSFixMe
state: StateConfig
}
6 changes: 3 additions & 3 deletions src/utils/command-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ export const padLeft = (str, count, filler = ' ') => str.padStart(str.length + c
const platform = WSL ? 'wsl' : os.platform()
const arch = os.arch() === 'ia32' ? 'x86' : os.arch()

const { name, version } = await getPackageJson()
const { name, version: packageVersion } = await getPackageJson()

export const netlifyCliVersion = version
export const USER_AGENT = `${name}/${netlifyCliVersion} ${platform}-${arch} node-${process.version}`
export const version = packageVersion
export const USER_AGENT = `${name}/${version} ${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
Loading

1 comment on commit e1372a3

@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,183

Please sign in to comment.