1
- import { resolve } from 'path'
1
+ import { basename , resolve } from 'path'
2
2
3
3
import { type NodeBundlerName , RUNTIME , zipFunctions , type FunctionResult } from '@netlify/zip-it-and-ship-it'
4
4
import { pathExists } from 'path-exists'
5
5
6
6
import { addErrorInfo } from '../../error/info.js'
7
7
import { log } from '../../log/logger.js'
8
+ import type { ReturnValue } from '../../types/step.js'
8
9
import { logBundleResults , logFunctionsNonExistingDir , logFunctionsToBundle } from '../../log/messages/core_steps.js'
9
10
import { FRAMEWORKS_API_FUNCTIONS_ENDPOINT } from '../../utils/frameworks_api.js'
10
11
@@ -68,6 +69,7 @@ const zipFunctionsAndLogResults = async ({
68
69
functionsConfig,
69
70
functionsDist,
70
71
functionsSrc,
72
+ generatedFunctions,
71
73
frameworkFunctionsSrc,
72
74
internalFunctionsSrc,
73
75
isRunningLocally,
@@ -94,7 +96,9 @@ const zipFunctionsAndLogResults = async ({
94
96
// Printing an empty line before bundling output.
95
97
log ( logs , '' )
96
98
97
- const sourceDirectories = [ internalFunctionsSrc , frameworkFunctionsSrc , functionsSrc ] . filter ( Boolean )
99
+ const sourceDirectories = [ internalFunctionsSrc , frameworkFunctionsSrc , functionsSrc , ...generatedFunctions ] . filter (
100
+ Boolean ,
101
+ )
98
102
const results = await zipItAndShipIt . zipFunctions ( sourceDirectories , functionsDist , zisiParameters )
99
103
100
104
validateCustomRoutes ( results )
@@ -128,6 +132,7 @@ const coreStep = async function ({
128
132
repositoryRoot,
129
133
userNodeVersion,
130
134
systemLog,
135
+ returnValues,
131
136
} ) {
132
137
const functionsSrc = relativeFunctionsSrc === undefined ? undefined : resolve ( buildDir , relativeFunctionsSrc )
133
138
const functionsDist = resolve ( buildDir , relativeFunctionsDist )
@@ -154,6 +159,8 @@ const coreStep = async function ({
154
159
}
155
160
}
156
161
162
+ const generatedFunctions = getGeneratedFunctionPaths ( returnValues )
163
+
157
164
logFunctionsToBundle ( {
158
165
logs,
159
166
userFunctions,
@@ -162,9 +169,15 @@ const coreStep = async function ({
162
169
internalFunctions,
163
170
internalFunctionsSrc : relativeInternalFunctionsSrc ,
164
171
frameworkFunctions,
172
+ generatedFunctions : getGeneratedFunctionsByGenerator ( returnValues ) ,
165
173
} )
166
174
167
- if ( userFunctions . length === 0 && internalFunctions . length === 0 && frameworkFunctions . length === 0 ) {
175
+ if (
176
+ userFunctions . length === 0 &&
177
+ internalFunctions . length === 0 &&
178
+ frameworkFunctions . length === 0 &&
179
+ generatedFunctions . length === 0
180
+ ) {
168
181
return { }
169
182
}
170
183
@@ -183,6 +196,7 @@ const coreStep = async function ({
183
196
repositoryRoot,
184
197
userNodeVersion,
185
198
systemLog,
199
+ generatedFunctions,
186
200
} )
187
201
188
202
const metrics = getMetrics ( internalFunctions , userFunctions )
@@ -203,6 +217,7 @@ const hasFunctionsDirectories = async function ({
203
217
buildDir,
204
218
constants : { INTERNAL_FUNCTIONS_SRC , FUNCTIONS_SRC } ,
205
219
packagePath,
220
+ returnValues,
206
221
} ) {
207
222
const hasFunctionsSrc = FUNCTIONS_SRC !== undefined && FUNCTIONS_SRC !== ''
208
223
@@ -218,7 +233,49 @@ const hasFunctionsDirectories = async function ({
218
233
219
234
const frameworkFunctionsSrc = resolve ( buildDir , packagePath || '' , FRAMEWORKS_API_FUNCTIONS_ENDPOINT )
220
235
221
- return await pathExists ( frameworkFunctionsSrc )
236
+ if ( await pathExists ( frameworkFunctionsSrc ) ) {
237
+ return true
238
+ }
239
+
240
+ // We must run the core step if the return value of any of the previous steps
241
+ // has declared generated functions.
242
+ for ( const id in returnValues ) {
243
+ if ( returnValues [ id ] . generatedFunctions && returnValues [ id ] . generatedFunctions . length !== 0 ) {
244
+ return true
245
+ }
246
+ }
247
+
248
+ return false
249
+ }
250
+
251
+ // Takes a list of return values and produces an array with the paths of all
252
+ // generated functions.
253
+ const getGeneratedFunctionPaths = ( returnValues : Record < string , ReturnValue > ) => {
254
+ return Object . values ( returnValues ) . flatMap (
255
+ ( returnValue ) => returnValue . generatedFunctions ?. map ( ( func ) => func . path ) || [ ] ,
256
+ )
257
+ }
258
+
259
+ // Takes a list of return values and produces an object with the names of the
260
+ // generated functions for each generator. This is used for printing logs only.
261
+ const getGeneratedFunctionsByGenerator = ( returnValues : Record < string , ReturnValue > ) => {
262
+ const result : Record < string , { displayName : string ; generatorType : string ; functionNames : string [ ] } > = { }
263
+
264
+ for ( const id in returnValues ) {
265
+ const { displayName, generatedFunctions, generatorType } = returnValues [ id ]
266
+
267
+ if ( ! generatedFunctions || generatedFunctions . length === 0 ) {
268
+ continue
269
+ }
270
+
271
+ result [ id ] = {
272
+ displayName,
273
+ generatorType,
274
+ functionNames : generatedFunctions . map ( ( func ) => basename ( func . path ) ) ,
275
+ }
276
+ }
277
+
278
+ return result
222
279
}
223
280
224
281
export const bundleFunctions = {
0 commit comments