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: type cachedConfig.env #6288

Merged
merged 5 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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
4 changes: 3 additions & 1 deletion src/commands/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type PatchedConfig = NetlifyTOML & {
}
}

export type EnvironmentVariables = Record<string, { sources: string[], value: string; scopes?: string[] }>
Skn0tt marked this conversation as resolved.
Show resolved Hide resolved

/**
* The netlify object inside each command with the state
*/
Expand All @@ -39,7 +41,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