Skip to content

Commit 70364c4

Browse files
committed
refactor: type commander options in deploy command
1 parent ae637a7 commit 70364c4

File tree

5 files changed

+81
-25
lines changed

5 files changed

+81
-25
lines changed

src/commands/base-command.ts

+9
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@ async function getRepositoryRoot(cwd?: string): Promise<string | undefined> {
148148
}
149149
}
150150

151+
export type BaseOptionValues = {
152+
auth?: string
153+
cwd?: string
154+
debug?: boolean
155+
filter?: string
156+
httpProxy?: string
157+
silent?: string
158+
}
159+
151160
/** Base command class that provides tracking and config initialization */
152161
export default class BaseCommand extends Command {
153162
/** The netlify object inside each command with the state */

src/commands/deploy/deploy.ts

+46-21
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@ import { type Stats } from 'fs'
22
import { stat } from 'fs/promises'
33
import { basename, resolve } from 'path'
44

5-
import { type NetlifyConfig, runCoreSteps } from '@netlify/build'
6-
import { type OptionValues } from 'commander'
5+
import { type NetlifyConfig, type OnPostBuild, runCoreSteps } from '@netlify/build'
76
import inquirer from 'inquirer'
87
import isEmpty from 'lodash/isEmpty.js'
98
import isObject from 'lodash/isObject.js'
109
import { parseAllHeaders } from '@netlify/headers-parser'
1110
import { parseAllRedirects } from '@netlify/redirect-parser'
11+
import type { NetlifyAPI } from 'netlify'
1212
import prettyjson from 'prettyjson'
1313

1414
import { cancelDeploy } from '../../lib/api.js'
15-
import { type CachedConfig, getRunBuildOptions, runBuild } from '../../lib/build.js'
15+
import {
16+
type CachedConfig,
17+
type DefaultConfig,
18+
type PatchedHandlerType,
19+
getRunBuildOptions,
20+
runBuild,
21+
} from '../../lib/build.js'
1622
import { getBootstrapURL } from '../../lib/edge-functions/bootstrap.js'
1723
import { featureFlags as edgeFunctionsFeatureFlags } from '../../lib/edge-functions/consts.js'
1824
import { normalizeFunctionsConfig } from '../../lib/functions/config.js'
@@ -42,9 +48,19 @@ import { link } from '../link/link.js'
4248
import { sitesCreate } from '../sites/sites-create.js'
4349
import type { $TSFixMe } from '../types.js'
4450
import { SiteInfo } from '../../utils/types.js'
51+
import type { DeployOptionValues } from './option_values.js'
4552

46-
// @ts-expect-error TS(7031) FIXME: Binding element 'api' implicitly has an 'any' type... Remove this comment to see the full error message
47-
const triggerDeploy = async ({ api, options, siteData, siteId }) => {
53+
const triggerDeploy = async ({
54+
api,
55+
options,
56+
siteData,
57+
siteId,
58+
}: {
59+
api: NetlifyAPI
60+
options: DeployOptionValues
61+
siteData: { name: string }
62+
siteId: string
63+
}) => {
4864
try {
4965
const siteBuild = await api.createSiteBuild({ siteId })
5066
if (options.json) {
@@ -80,7 +96,7 @@ const getDeployFolder = async ({
8096
}: {
8197
command: BaseCommand
8298
config: $TSFixMe
83-
options: OptionValues
99+
options: DeployOptionValues
84100
site: $TSFixMe
85101
siteData: $TSFixMe
86102
}): Promise<string> => {
@@ -150,7 +166,7 @@ const getFunctionsFolder = ({
150166
workingDir,
151167
}: {
152168
config: $TSFixMe
153-
options: OptionValues
169+
options: DeployOptionValues
154170
site: $TSFixMe
155171
siteData: $TSFixMe
156172
/** The process working directory where the build command is executed */
@@ -347,7 +363,7 @@ const uploadDeployBlobs = async ({
347363
}: {
348364
cachedConfig: CachedConfig
349365
deployId: string
350-
options: OptionValues
366+
options: DeployOptionValues
351367
packagePath?: string
352368
silent: boolean
353369
siteId: string
@@ -540,18 +556,21 @@ const runDeploy = async ({
540556
}
541557
}
542558

543-
/**
544-
*
545-
* @param {object} config
546-
* @param {*} config.cachedConfig
547-
* @param {string} [config.packagePath]
548-
* @param {*} config.deployHandler
549-
* @param {string} config.currentDir
550-
* @param {import('commander').OptionValues} config.options The options of the command
551-
* @returns
552-
*/
553-
// @ts-expect-error TS(7031) FIXME: Binding element 'cachedConfig' implicitly has an '... Remove this comment to see the full error message
554-
const handleBuild = async ({ cachedConfig, currentDir, defaultConfig, deployHandler, options, packagePath }) => {
559+
const handleBuild = async ({
560+
cachedConfig,
561+
currentDir,
562+
defaultConfig,
563+
deployHandler,
564+
options,
565+
packagePath,
566+
}: {
567+
cachedConfig: CachedConfig
568+
currentDir: string
569+
defaultConfig?: DefaultConfig | undefined
570+
deployHandler?: PatchedHandlerType<OnPostBuild> | undefined
571+
options: DeployOptionValues
572+
packagePath: string | undefined
573+
}) => {
555574
if (!options.build) {
556575
return {}
557576
}
@@ -782,7 +801,7 @@ const prepAndRunDeploy = async ({
782801
return results
783802
}
784803

785-
export const deploy = async (options: OptionValues, command: BaseCommand) => {
804+
export const deploy = async (options: DeployOptionValues, command: BaseCommand) => {
786805
const { workingDir } = command
787806
const { api, site, siteInfo } = command.netlify
788807
const alias = options.alias || options.branch
@@ -834,6 +853,12 @@ export const deploy = async (options: OptionValues, command: BaseCommand) => {
834853
}
835854
}
836855

856+
if (!siteId) {
857+
return logAndThrowError(
858+
"Unable to determine which site to deploy to. Make sure you've run 'netlify link' or that you're specifying your desired site using the '--site' option.",
859+
)
860+
}
861+
837862
// This is the best I could come up with to make TS happy with the complexities above.
838863
const siteData = initialSiteData ?? newSiteData
839864

src/commands/deploy/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { env } from 'process'
22

3-
import { Option, OptionValues } from 'commander'
3+
import { Option } from 'commander'
44

55
import BaseCommand from '../base-command.js'
6+
import type { DeployOptionValues } from './option_values.js'
67

78
export const createDeployCommand = (program: BaseCommand) =>
89
program
@@ -107,7 +108,7 @@ Support for package.json's main field, and intrinsic index.js entrypoints are co
107108
'build',
108109
),
109110
)
110-
.option('--build', 'Run build command before deploying')
111+
.option('--build', 'Run build command before deploying', false)
111112
.option('--context <context>', 'Context to use when resolving build configuration')
112113
.option(
113114
'--skip-functions-cache',
@@ -125,7 +126,7 @@ Support for package.json's main field, and intrinsic index.js entrypoints are co
125126
'netlify deploy --trigger',
126127
'netlify deploy --build --context deploy-preview',
127128
])
128-
.action(async (options: OptionValues, command: BaseCommand) => {
129+
.action(async (options: DeployOptionValues, command: BaseCommand) => {
129130
const { deploy } = await import('./deploy.js')
130131
await deploy(options, command)
131132
})

src/commands/deploy/option_values.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This type lives in a separate file to prevent import cycles.
2+
3+
import type { BaseOptionValues } from '../base-command.js'
4+
5+
export type DeployOptionValues = BaseOptionValues & {
6+
alias?: string
7+
build: boolean
8+
branch?: string
9+
context?: string
10+
dir?: string
11+
functions?: string
12+
json: boolean
13+
message?: string
14+
open: boolean
15+
prod: boolean
16+
prodIfUnlocked: boolean
17+
site?: string
18+
skipFunctionsCache: boolean
19+
timeout?: number
20+
trigger?: boolean
21+
}

src/lib/build.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ interface HandlerResult {
115115
status?: string
116116
}
117117
// The @netlify/build type incorrectly states a `void | Promise<void>` return type.
118-
type PatchedHandlerType<T extends (opts: any) => void | Promise<void>> = (
118+
export type PatchedHandlerType<T extends (opts: any) => void | Promise<void>> = (
119119
opts: Parameters<T>[0],
120120
) => HandlerResult | Promise<HandlerResult>
121121

0 commit comments

Comments
 (0)