Skip to content

Commit d1ec47f

Browse files
Copilotserhalp
andcommitted
Apply type fixes from PRs #7058 and #7068 - phase 1
Co-authored-by: serhalp <1377702+serhalp@users.noreply.github.com>
1 parent a5e4a07 commit d1ec47f

File tree

7 files changed

+79
-66
lines changed

7 files changed

+79
-66
lines changed

src/commands/deploy/deploy.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -228,20 +228,14 @@ const validateFolders = async ({
228228
return { deployFolderStat, functionsFolderStat }
229229
}
230230

231-
/**
232-
* @param {object} config
233-
* @param {string} config.deployFolder
234-
* @param {*} config.site
235-
* @returns
236-
*/
237-
// @ts-expect-error TS(7031) FIXME: Binding element 'deployFolder' implicitly has an '... Remove this comment to see the full error message
238-
const getDeployFilesFilter = ({ deployFolder, site }) => {
231+
const getDeployFilesFilter = ({ deployFolder, site }: { deployFolder: string; site: { root: string } }) => {
239232
// site.root === deployFolder can happen when users run `netlify deploy --dir .`
240233
// in that specific case we don't want to publish the repo node_modules
241234
// when site.root !== deployFolder the behaviour matches our buildbot
242235
const skipNodeModules = site.root === deployFolder
243236

244237
return (filename: string) => {
238+
// TODO(serhalp) Per types, this should not be possible. Confirm and remove this check.
245239
if (filename == null) {
246240
return false
247241
}
@@ -285,16 +279,20 @@ const prepareProductionDeploy = async ({ api, siteData }) => {
285279
}
286280
}
287281

288-
// @ts-expect-error TS(7006) FIXME: Parameter 'actual' implicitly has an 'any' type.
289-
const hasErrorMessage = (actual, expected) => {
282+
const hasErrorMessage = (actual: unknown, expected: string): boolean => {
290283
if (typeof actual === 'string') {
291284
return actual.includes(expected)
292285
}
293286
return false
294287
}
295288

296-
// @ts-expect-error TS(7031) FIXME: Binding element 'error_' implicitly has an 'any' t... Remove this comment to see the full error message
297-
const reportDeployError = ({ error_, failAndExit }) => {
289+
const reportDeployError = ({
290+
error_,
291+
failAndExit,
292+
}: {
293+
error_: (Error & { json?: Record<string, unknown>; status?: number }) | any
294+
failAndExit: (errorOrMessage: Error | string) => void
295+
}) => {
298296
switch (true) {
299297
case error_.name === 'JSONHTTPError': {
300298
const message = error_?.json?.message ?? ''

src/commands/functions/functions-create.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ const handleAddonDidInstall = async ({ addonCreated, addonDidInstall, command, f
640640
return
641641
}
642642

643-
await injectEnvVariables({
643+
injectEnvVariables({
644644
devConfig: { ...config.dev },
645645
env: command.netlify.cachedConfig.env,
646646
site: command.netlify.site,

src/commands/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type NetlifySite = {
1414
configPath?: string
1515
siteId?: string
1616
get id(): string | undefined
17-
set id(id: string): void
17+
set id(id: string)
1818
}
1919

2020
/**

src/utils/dev.ts

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { supportsBackgroundFunctions } from '../lib/account.js'
88

99
import { NETLIFYDEVLOG, chalk, logAndThrowError, log, warn, APIError } from './command-helpers.js'
1010
import { loadDotEnvFiles } from './dot-env.js'
11+
import type { CachedConfig } from '../lib/build.js'
12+
import type { NetlifySite } from '../commands/types.js'
13+
import type { DevConfig } from '../commands/dev/types.js'
1114
import type { EnvironmentVariables, SiteInfo } from './types.js'
1215

1316
// Possible sources of environment variables. For the purpose of printing log messages only. Order does not matter.
@@ -41,8 +44,7 @@ const ENV_VAR_SOURCES = {
4144
const ERROR_CALL_TO_ACTION =
4245
"Double-check your login status with 'netlify status' or contact support with details of your error."
4346

44-
// @ts-expect-error TS(7031) FIXME: Binding element 'site' implicitly has an 'any' typ... Remove this comment to see the full error message
45-
const validateSiteInfo = ({ site, siteInfo }) => {
47+
const validateSiteInfo = ({ site, siteInfo }: { site: NetlifySite; siteInfo: SiteInfo }): void => {
4648
if (isEmpty(siteInfo)) {
4749
return logAndThrowError(
4850
`Failed to retrieve project information for project ${chalk.yellow(site.id)}. ${ERROR_CALL_TO_ACTION}`,
@@ -73,9 +75,9 @@ const getAccounts = async ({ api }: { api: NetlifyAPI }) => {
7375
}
7476
}
7577

76-
// @ts-expect-error TS(7031) FIXME: Binding element 'api' implicitly has an 'any' type... Remove this comment to see the full error message
77-
const getAddons = async ({ api, site }) => {
78+
const getAddons = async ({ api, site }: { api: NetlifyAPI; site: NetlifySite }) => {
7879
try {
80+
// @ts-expect-error(serhalp) One of three types is incorrect here (is `site.id` optional?). Dig and fix.
7981
const addons = await api.listServiceInstancesForSite({ siteId: site.id })
8082
return addons
8183
} catch (error_) {
@@ -87,13 +89,11 @@ const getAddons = async ({ api, site }) => {
8789
}
8890
}
8991

90-
// @ts-expect-error TS(7031) FIXME: Binding element 'addons' implicitly has an 'any' t... Remove this comment to see the full error message
91-
const getAddonsInformation = ({ addons, siteInfo }) => {
92+
type Addons = Awaited<ReturnType<NetlifyAPI['listServiceInstancesForSite']>>
93+
const getAddonsInformation = ({ addons, siteInfo }: { addons: Addons; siteInfo: SiteInfo }) => {
9294
const urls = Object.fromEntries(
93-
// @ts-expect-error TS(7006) FIXME: Parameter 'addon' implicitly has an 'any' type.
9495
addons.map((addon) => [addon.service_slug, `${siteInfo.ssl_url}${addon.service_path}`]),
9596
)
96-
// @ts-expect-error TS(7006) FIXME: Parameter 'addon' implicitly has an 'any' type.
9797
const env = Object.assign({}, ...addons.map((addon) => addon.env))
9898
return { urls, env }
9999
}
@@ -113,17 +113,17 @@ const SYNCHRONOUS_FUNCTION_TIMEOUT = 30
113113
// default 15 minutes for background functions
114114
const BACKGROUND_FUNCTION_TIMEOUT = 900
115115

116-
/**
117-
*
118-
* @param {object} config
119-
* @param {boolean} config.offline
120-
* @param {*} config.api
121-
* @param {*} config.site
122-
* @param {*} config.siteInfo
123-
* @returns
124-
*/
125-
// @ts-expect-error TS(7031) FIXME: Binding element 'api' implicitly has an 'any' type... Remove this comment to see the full error message
126-
export const getSiteInformation = async ({ api, offline, site, siteInfo }) => {
116+
export const getSiteInformation = async ({
117+
api,
118+
offline,
119+
site,
120+
siteInfo,
121+
}: {
122+
api: NetlifyAPI
123+
offline: boolean
124+
site: NetlifySite
125+
siteInfo: SiteInfo
126+
}) => {
127127
if (site.id && !offline) {
128128
validateSiteInfo({ site, siteInfo })
129129
const [accounts, addons] = await Promise.all([getAccounts({ api }), getAddons({ api, site })])
@@ -157,21 +157,22 @@ export const getSiteInformation = async ({ api, offline, site, siteInfo }) => {
157157
}
158158
}
159159

160-
// @ts-expect-error TS(7006) FIXME: Parameter 'source' implicitly has an 'any' type.
161-
const getEnvSourceName = (source) => {
162-
// @ts-expect-error TS(7053) FIXME: Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
163-
const { name = source, printFn = chalk.green } = ENV_VAR_SOURCES[source] || {}
160+
const getEnvSourceName = (source: string) => {
161+
const { name = source, printFn = chalk.green } = ENV_VAR_SOURCES[source] ?? {}
164162

165163
return printFn(name)
166164
}
167165

168-
/**
169-
* @param {{devConfig: any, env: Record<string, { sources: string[], value: string}>, site: any}} param0
170-
*/
171-
// @ts-expect-error TS(7031) FIXME: Binding element 'devConfig' implicitly has an 'any... Remove this comment to see the full error message
172-
export const getDotEnvVariables = async ({ devConfig, env, site }): Promise<EnvironmentVariables> => {
166+
export const getDotEnvVariables = async ({
167+
devConfig,
168+
env,
169+
site,
170+
}: {
171+
devConfig: DevConfig
172+
env: CachedConfig['env']
173+
site: NetlifySite
174+
}): Promise<Record<string, { sources: string[]; value: string }>> => {
173175
const dotEnvFiles = await loadDotEnvFiles({ envFiles: devConfig.envFiles, projectDir: site.root })
174-
// @ts-expect-error TS(2339) FIXME: Property 'env' does not exist on type '{ warning: ... Remove this comment to see the full error message
175176
dotEnvFiles.forEach(({ env: fileEnv, file }) => {
176177
const newSourceName = `${file} file`
177178

@@ -183,6 +184,7 @@ export const getDotEnvVariables = async ({ devConfig, env, site }): Promise<Envi
183184
}
184185

185186
env[key] = {
187+
// @ts-expect-error(serhalp) Something isn't right with these types but it's a can of worms.
186188
sources,
187189
value: fileEnv[key],
188190
}
@@ -248,8 +250,7 @@ export const acquirePort = async ({
248250
return acquiredPort
249251
}
250252

251-
// @ts-expect-error TS(7006) FIXME: Parameter 'fn' implicitly has an 'any' type.
252-
export const processOnExit = (fn) => {
253+
export const processOnExit = (fn: (...args: unknown[]) => void) => {
253254
const signals = ['SIGINT', 'SIGTERM', 'SIGQUIT', 'SIGHUP', 'exit']
254255
signals.forEach((signal) => {
255256
process.on(signal, fn)

src/utils/dot-env.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,58 @@
11
import { readFile } from 'fs/promises'
22
import path from 'path'
33

4-
import dotenv from 'dotenv'
4+
import dotenv, { type DotenvParseOutput } from 'dotenv'
55

66
import { isFileAsync } from '../lib/fs.js'
77

88
import { warn } from './command-helpers.js'
99

10-
// @ts-expect-error TS(7031) FIXME: Binding element 'envFiles' implicitly has an 'any'... Remove this comment to see the full error message
11-
export const loadDotEnvFiles = async function ({ envFiles, projectDir }) {
12-
const response = await tryLoadDotEnvFiles({ projectDir, dotenvFiles: envFiles })
10+
interface LoadedDotEnvFile {
11+
file: string
12+
env: DotenvParseOutput
13+
}
14+
15+
export const loadDotEnvFiles = async function ({
16+
envFiles,
17+
projectDir,
18+
}: {
19+
envFiles?: string[]
20+
projectDir?: string
21+
}): Promise<LoadedDotEnvFile[]> {
22+
const loadedDotEnvFiles = await tryLoadDotEnvFiles({ projectDir, dotenvFiles: envFiles })
1323

14-
// @ts-expect-error TS(2532) FIXME: Object is possibly 'undefined'.
15-
const filesWithWarning = response.filter((el) => el.warning)
16-
filesWithWarning.forEach((el) => {
17-
// @ts-expect-error TS(2532) FIXME: Object is possibly 'undefined'.
18-
warn(el.warning)
19-
})
24+
loadedDotEnvFiles
25+
.filter((el): el is { warning: string } => 'warning' in el)
26+
.forEach((el) => {
27+
warn(el.warning)
28+
})
2029

21-
// @ts-expect-error TS(2532) FIXME: Object is possibly 'undefined'.
22-
return response.filter((el) => el.file && el.env)
30+
return loadedDotEnvFiles.filter((el): el is LoadedDotEnvFile => 'file' in el && 'env' in el)
2331
}
2432

2533
// in the user configuration, the order is highest to lowest
2634
const defaultEnvFiles = ['.env.development.local', '.env.local', '.env.development', '.env']
2735

28-
// @ts-expect-error TS(7031) FIXME: Binding element 'projectDir' implicitly has an 'an... Remove this comment to see the full error message
29-
export const tryLoadDotEnvFiles = async ({ dotenvFiles = defaultEnvFiles, projectDir }) => {
36+
export const tryLoadDotEnvFiles = async ({
37+
dotenvFiles = defaultEnvFiles,
38+
projectDir,
39+
}: {
40+
dotenvFiles?: string[]
41+
projectDir?: string
42+
}): Promise<Array<LoadedDotEnvFile | { warning: string }>> => {
3043
const results = await Promise.all(
3144
dotenvFiles.map(async (file) => {
32-
const filepath = path.resolve(projectDir, file)
45+
const filepath = path.resolve(projectDir ?? '', file)
3346
try {
3447
const isFile = await isFileAsync(filepath)
3548
if (!isFile) {
3649
return
3750
}
3851
} catch (error) {
3952
return {
40-
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
41-
warning: `Failed reading env variables from file: ${filepath}: ${error.message}`,
53+
warning: `Failed reading env variables from file: ${filepath}: ${
54+
error instanceof Error ? error.message : error?.toString()
55+
}`,
4256
}
4357
}
4458
const content = await readFile(filepath, 'utf-8')
@@ -48,5 +62,5 @@ export const tryLoadDotEnvFiles = async ({ dotenvFiles = defaultEnvFiles, projec
4862
)
4963

5064
// we return in order of lowest to highest priority
51-
return results.filter(Boolean).reverse()
65+
return results.filter((result): result is LoadedDotEnvFile => result != null).reverse()
5266
}

src/utils/open-browser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ const unableToOpenBrowserMessage = function ({ message, url }: BrowserUnableMess
1818
log('---------------------------')
1919
}
2020

21-
type OpenBrowsrProps = {
21+
type OpenBrowserProps = {
2222
silentBrowserNoneError?: boolean
2323
url: string
2424
}
2525

26-
const openBrowser = async function ({ silentBrowserNoneError, url }: OpenBrowsrProps) {
26+
const openBrowser = async function ({ silentBrowserNoneError, url }: OpenBrowserProps) {
2727
if (isDockerContainer()) {
2828
unableToOpenBrowserMessage({ url, message: 'Running inside a docker container' })
2929
return

src/utils/proxy-server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export const startProxyServer = async ({
6464
state,
6565
}: {
6666
accountId: string | undefined
67-
addonsUrls: $TSFixMe
67+
addonsUrls: Record<string, string>
6868
api?: NetlifyOptions['api']
6969
blobsContext?: BlobsContextWithEdgeAccess
7070
command: BaseCommand

0 commit comments

Comments
 (0)