Skip to content

Commit b5a5c75

Browse files
authored
types: fix 13 @ts-expect-errors in build command (#7048)
1 parent 504e7f3 commit b5a5c75

File tree

7 files changed

+80
-62
lines changed

7 files changed

+80
-62
lines changed

src/commands/build/build.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
import { OptionValues } from 'commander'
1+
import type { OptionValues } from 'commander'
22

3-
import { getBuildOptions, runBuild } from '../../lib/build.js'
3+
import { type BuildConfig, getBuildOptions, runBuild } from '../../lib/build.js'
44
import { detectFrameworkSettings, getDefaultConfig } from '../../utils/build-info.js'
55
import { error, exit, getToken } from '../../utils/command-helpers.js'
66
import { getEnvelopeEnv } from '../../utils/env/index.js'
7-
import BaseCommand from '../base-command.js'
8-
9-
/**
10-
* @param {import('../../lib/build.js').BuildConfig} options
11-
*/
12-
// @ts-expect-error TS(7031) FIXME: Binding element 'token' implicitly has an 'any' ty... Remove this comment to see the full error message
13-
export const checkOptions = ({ cachedConfig: { siteInfo = {} }, token }) => {
14-
// @ts-expect-error TS(2339) FIXME: Property 'id' does not exist on type '{}'.
7+
import type BaseCommand from '../base-command.js'
8+
9+
export const checkOptions = ({ cachedConfig: { siteInfo }, token }: BuildConfig) => {
1510
if (!siteInfo.id) {
1611
error(
1712
'Could not find the site ID. If your site is not on Netlify, please run `netlify init` or `netlify deploy` first. If it is, please run `netlify link`.',
@@ -36,7 +31,6 @@ export const build = async (options: OptionValues, command: BaseCommand) => {
3631
packagePath: command.workspacePackage,
3732
currentDir: command.workingDir,
3833
token,
39-
// @ts-expect-error TS(2740)
4034
options,
4135
})
4236

src/commands/env/env-list.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import ansiEscapes from 'ansi-escapes'
22
import AsciiTable from 'ascii-table'
33
import { isCI } from 'ci-info'
4-
import { OptionValues } from 'commander'
4+
import type { OptionValues } from 'commander'
55
import inquirer from 'inquirer'
66
import logUpdate from 'log-update'
77

88
import { chalk, log, logJson } from '../../utils/command-helpers.js'
99
import { AVAILABLE_CONTEXTS, getEnvelopeEnv, getHumanReadableScopes } from '../../utils/env/index.js'
10-
import BaseCommand from '../base-command.js'
11-
import { EnvironmentVariables } from '../types.js'
10+
import type BaseCommand from '../base-command.js'
11+
import { EnvironmentVariables } from '../../utils/types.js'
1212

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

src/commands/integration/deploy.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,6 @@ export const deploy = async (options: OptionValues, command: BaseCommand) => {
391391
packagePath: command.workspacePackage,
392392
currentDir: command.workingDir,
393393
token,
394-
// @ts-expect-error TS(2740)
395394
options,
396395
})
397396

src/commands/types.d.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import type { NetlifyTOML } from '@netlify/build-info'
33
import type { NetlifyAPI } from 'netlify'
44

55
import type { FrameworksAPIPaths } from "../utils/frameworks-api.ts";
6-
import StateConfig from '../utils/state-config.js'
7-
import { Account } from "../utils/types.ts";
8-
6+
import type StateConfig from '../utils/state-config.js'
7+
import type { Account } from "../utils/types.ts";
8+
import type { CachedConfig } from "../utils/build.js"
99

1010
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1111
type $TSFixMe = any;
@@ -49,10 +49,6 @@ type HTMLInjection = {
4949
html: string
5050
}
5151

52-
type EnvironmentVariableScope = 'builds' | 'functions' | 'runtime' | 'post_processing'
53-
type EnvironmentVariableSource = 'account' | 'addons' | 'configFile' | 'general' | 'internal' | 'ui'
54-
55-
export type EnvironmentVariables = Record<string, { sources: EnvironmentVariableSource[], value: string; scopes?: EnvironmentVariableScope[] }>
5652

5753
/**
5854
* The netlify object inside each command with the state
@@ -70,7 +66,7 @@ export type NetlifyOptions = {
7066
site: NetlifySite
7167
siteInfo: $TSFixMe
7268
config: PatchedConfig
73-
cachedConfig: Record<string, $TSFixMe> & { env: EnvironmentVariables }
69+
cachedConfig: CachedConfig
7470
globalConfig: $TSFixMe
7571
state: StateConfig
7672
frameworksAPIPaths: FrameworksAPIPaths

src/lib/build.ts

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,79 @@
11
import fs from 'fs'
22
import process from 'process'
33

4-
import build from '@netlify/build'
4+
import build, { type OnEnd, type OnPostBuild } from '@netlify/build'
55
// @ts-expect-error TS(7016) FIXME: Could not find a declaration file for module 'toml... Remove this comment to see the full error message
66
import tomlify from 'tomlify-j0.4'
7+
import type { OptionValues } from 'commander'
78

89
import { getFeatureFlagsFromSiteInfo } from '../utils/feature-flags.js'
10+
import type { EnvironmentVariables } from '../utils/types.js'
911

1012
import { getBootstrapURL } from './edge-functions/bootstrap.js'
1113
import { featureFlags as edgeFunctionsFeatureFlags } from './edge-functions/consts.js'
1214

13-
/**
14-
* The buildConfig + a missing cachedConfig
15-
* @typedef BuildConfig
16-
* @type {Parameters<import('@netlify/build/src/core/main.js')>[0] & {cachedConfig: any}}
17-
*/
15+
export interface CachedConfig {
16+
env: EnvironmentVariables
17+
siteInfo: {
18+
id?: string
19+
account_id?: string
20+
feature_flags?: Record<string, boolean | boolean | number>
21+
}
22+
// TODO(serhalp) Add remaining properties
23+
[k: string]: unknown
24+
}
25+
26+
interface DefaultConfig {
27+
// TODO(serhalp) Add remaining properties
28+
[k: string]: unknown
29+
}
30+
31+
// TODO(serhalp) This is patching weak or missing properties from @netlify/build. Fix there instead.
32+
export type BuildConfig = Parameters<typeof build>[0] & {
33+
cachedConfig: CachedConfig
34+
defaultConfig: DefaultConfig
35+
// It's possible this one is correct in @netlify/build. Remove and stop passing it if so.
36+
accountId?: string
37+
edgeFunctionsBootstrapURL: string
38+
}
39+
40+
interface HandlerResult {
41+
newEnvChanges?: Record<string, string>
42+
configMutations?: Record<string, string>
43+
status?: string
44+
}
45+
// The @netlify/build type incorrectly states a `void | Promise<void>` return type.
46+
type PatchedHandlerType<T extends (opts: any) => void | Promise<void>> = (
47+
opts: Parameters<T>[0],
48+
) => HandlerResult | Promise<HandlerResult>
49+
50+
type EventHandler<T extends (opts: any) => void | Promise<void>> = {
51+
handler: PatchedHandlerType<T>
52+
description: string
53+
}
1854

1955
// We have already resolved the configuration using `@netlify/config`
2056
// This is stored as `netlify.cachedConfig` and can be passed to
2157
// `@netlify/build --cachedConfig`.
22-
/**
23-
*
24-
* @param {object} config
25-
* @param {*} config.cachedConfig
26-
* @param {string} [config.packagePath]
27-
* @param {string} config.currentDir
28-
* @param {string} config.token
29-
* @param {import('commander').OptionValues} config.options
30-
* @param {*} config.deployHandler
31-
* @returns {BuildConfig}
32-
*/
3358
export const getBuildOptions = async ({
34-
// @ts-expect-error TS(7031) FIXME: Binding element 'cachedConfig' implicitly has an '... Remove this comment to see the full error message
3559
cachedConfig,
36-
// @ts-expect-error TS(7031) FIXME: Binding element 'currentDir' implicitly has an 'an... Remove this comment to see the full error message
3760
currentDir,
38-
// @ts-expect-error TS(7031) FIXME: Binding element 'defaultConfig' implicitly has an '... Remove this comment to see the full error message
3961
defaultConfig,
40-
// @ts-expect-error TS(7031) FIXME: Binding element 'deployHandler' implicitly has an ... Remove this comment to see the full error message
4162
deployHandler,
42-
// @ts-expect-error TS(7031) FIXME: Binding element 'context' implicitly has an 'any' ... Remove this comment to see the full error message
4363
options: { context, cwd, debug, dry, json, offline, silent },
44-
// @ts-expect-error TS(7031) FIXME: Binding element 'packagePath' implicitly has an 'a... Remove this comment to see the full error message
4564
packagePath,
46-
// @ts-expect-error TS(7031) FIXME: Binding element 'token' implicitly has an 'any' ty... Remove this comment to see the full error message
4765
token,
48-
}) => {
49-
const eventHandlers = {
66+
}: {
67+
cachedConfig: CachedConfig
68+
currentDir: string
69+
defaultConfig?: DefaultConfig
70+
deployHandler?: PatchedHandlerType<OnPostBuild>
71+
options: OptionValues
72+
packagePath?: string
73+
token?: null | string
74+
}): Promise<BuildConfig> => {
75+
const eventHandlers: { onEnd: EventHandler<OnEnd>; onPostBuild?: EventHandler<OnPostBuild> } = {
5076
onEnd: {
51-
// @ts-expect-error TS(7031) FIXME: Binding element 'netlifyConfig' implicitly has an ... Remove this comment to see the full error message
5277
handler: ({ netlifyConfig }) => {
5378
const string = tomlify.toToml(netlifyConfig)
5479

@@ -64,7 +89,6 @@ export const getBuildOptions = async ({
6489
}
6590

6691
if (deployHandler) {
67-
// @ts-expect-error TS(2339) FIXME: Property 'onPostBuild' does not exist on type '{ o... Remove this comment to see the full error message
6892
eventHandlers.onPostBuild = {
6993
handler: deployHandler,
7094
description: 'Deploy Site',
@@ -73,11 +97,11 @@ export const getBuildOptions = async ({
7397

7498
return {
7599
cachedConfig,
76-
defaultConfig,
100+
defaultConfig: defaultConfig ?? {},
77101
siteId: cachedConfig.siteInfo.id,
78102
accountId: cachedConfig.siteInfo.account_id,
79103
packagePath,
80-
token,
104+
token: token ?? undefined,
81105
dry,
82106
debug,
83107
context,
@@ -92,18 +116,13 @@ export const getBuildOptions = async ({
92116
...getFeatureFlagsFromSiteInfo(cachedConfig.siteInfo),
93117
functionsBundlingManifest: true,
94118
},
119+
// @ts-expect-error(serhalp) -- TODO(serhalp) Upstream the type fixes above into @netlify/build
95120
eventHandlers,
96121
edgeFunctionsBootstrapURL: await getBootstrapURL(),
97122
}
98123
}
99124

100-
/**
101-
* run the build command
102-
* @param {BuildConfig} options
103-
* @returns
104-
*/
105-
// @ts-expect-error TS(7006) FIXME: Parameter 'options' implicitly has an 'any' type.
106-
export const runBuild = async (options) => {
125+
export const runBuild = async (options: BuildConfig) => {
107126
// If netlify NETLIFY_API_URL is set we need to pass this information to @netlify/build
108127
// TODO don't use testOpts, but add real properties to do this.
109128
if (process.env.NETLIFY_API_URL) {
@@ -112,6 +131,8 @@ export const runBuild = async (options) => {
112131
scheme: apiUrl.protocol.slice(0, -1),
113132
host: apiUrl.host,
114133
}
134+
// @ts-expect-error(serhalp) -- I don't know what's going on here and I can't convince myself it even works as
135+
// intended. TODO(serhalp) Investigate and fix types.
115136
options = { ...options, testOpts }
116137
}
117138

src/utils/command-helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ export const pollForToken = async ({
133133
* @returns {Promise<[null|string, 'flag' | 'env' |'config' |'not found']>}
134134
*/
135135

136-
export type tokenTuple = [string | null, TokenLocation]
136+
export type TokenTuple = [string | null, TokenLocation]
137137

138-
export const getToken = async (tokenFromOptions?: string): Promise<tokenTuple> => {
138+
export const getToken = async (tokenFromOptions?: string): Promise<TokenTuple> => {
139139
// 1. First honor command flag --auth
140140
if (tokenFromOptions) {
141141
return [tokenFromOptions, 'flag']

src/utils/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,11 @@ export interface Template {
195195
sourceCodeUrl: string
196196
slug: string
197197
}
198+
199+
type EnvironmentVariableScope = 'builds' | 'functions' | 'runtime' | 'post_processing'
200+
type EnvironmentVariableSource = 'account' | 'addons' | 'configFile' | 'general' | 'internal' | 'ui'
201+
202+
export type EnvironmentVariables = Record<
203+
string,
204+
{ sources: EnvironmentVariableSource[]; value: string; scopes?: EnvironmentVariableScope[] }
205+
>

0 commit comments

Comments
 (0)