1
1
import fs from 'fs'
2
2
import process from 'process'
3
3
4
- import build from '@netlify/build'
4
+ import build , { type OnEnd , type OnPostBuild } from '@netlify/build'
5
5
// @ts -expect-error TS(7016) FIXME: Could not find a declaration file for module 'toml... Remove this comment to see the full error message
6
6
import tomlify from 'tomlify-j0.4'
7
+ import type { OptionValues } from 'commander'
7
8
8
9
import { getFeatureFlagsFromSiteInfo } from '../utils/feature-flags.js'
10
+ import type { EnvironmentVariables } from '../utils/types.js'
9
11
10
12
import { getBootstrapURL } from './edge-functions/bootstrap.js'
11
13
import { featureFlags as edgeFunctionsFeatureFlags } from './edge-functions/consts.js'
12
14
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
+ }
18
54
19
55
// We have already resolved the configuration using `@netlify/config`
20
56
// This is stored as `netlify.cachedConfig` and can be passed to
21
57
// `@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
- */
33
58
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
35
59
cachedConfig,
36
- // @ts -expect-error TS(7031) FIXME: Binding element 'currentDir' implicitly has an 'an... Remove this comment to see the full error message
37
60
currentDir,
38
- // @ts -expect-error TS(7031) FIXME: Binding element 'defaultConfig' implicitly has an '... Remove this comment to see the full error message
39
61
defaultConfig,
40
- // @ts -expect-error TS(7031) FIXME: Binding element 'deployHandler' implicitly has an ... Remove this comment to see the full error message
41
62
deployHandler,
42
- // @ts -expect-error TS(7031) FIXME: Binding element 'context' implicitly has an 'any' ... Remove this comment to see the full error message
43
63
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
45
64
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
47
65
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 > } = {
50
76
onEnd : {
51
- // @ts -expect-error TS(7031) FIXME: Binding element 'netlifyConfig' implicitly has an ... Remove this comment to see the full error message
52
77
handler : ( { netlifyConfig } ) => {
53
78
const string = tomlify . toToml ( netlifyConfig )
54
79
@@ -64,7 +89,6 @@ export const getBuildOptions = async ({
64
89
}
65
90
66
91
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
68
92
eventHandlers . onPostBuild = {
69
93
handler : deployHandler ,
70
94
description : 'Deploy Site' ,
@@ -73,11 +97,11 @@ export const getBuildOptions = async ({
73
97
74
98
return {
75
99
cachedConfig,
76
- defaultConfig,
100
+ defaultConfig : defaultConfig ?? { } ,
77
101
siteId : cachedConfig . siteInfo . id ,
78
102
accountId : cachedConfig . siteInfo . account_id ,
79
103
packagePath,
80
- token,
104
+ token : token ?? undefined ,
81
105
dry,
82
106
debug,
83
107
context,
@@ -92,18 +116,13 @@ export const getBuildOptions = async ({
92
116
...getFeatureFlagsFromSiteInfo ( cachedConfig . siteInfo ) ,
93
117
functionsBundlingManifest : true ,
94
118
} ,
119
+ // @ts -expect-error(serhalp) -- TODO(serhalp) Upstream the type fixes above into @netlify/build
95
120
eventHandlers,
96
121
edgeFunctionsBootstrapURL : await getBootstrapURL ( ) ,
97
122
}
98
123
}
99
124
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 ) => {
107
126
// If netlify NETLIFY_API_URL is set we need to pass this information to @netlify /build
108
127
// TODO don't use testOpts, but add real properties to do this.
109
128
if ( process . env . NETLIFY_API_URL ) {
@@ -112,6 +131,8 @@ export const runBuild = async (options) => {
112
131
scheme : apiUrl . protocol . slice ( 0 , - 1 ) ,
113
132
host : apiUrl . host ,
114
133
}
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.
115
136
options = { ...options , testOpts }
116
137
}
117
138
0 commit comments