@@ -4,43 +4,44 @@ import boxen from 'boxen';
4
4
import chalk from 'chalk' ;
5
5
import logSymbols from 'log-symbols' ;
6
6
import wrapAnsi from 'wrap-ansi' ;
7
+ import { ServerConfig } from '../../../runtime-handler/dist/dev-runtime/types' ;
7
8
import { StartCliConfig } from '../config/start' ;
8
9
import { getFunctionsAndAssets } from '../runtime/internal/runtime-paths' ;
9
10
import { logger } from '../utils/logger' ;
10
11
import { shouldPrettyPrint , terminalLink , windowSize } from './utils' ;
11
12
12
13
function printAsset (
13
14
asset : ServerlessResourceConfig ,
14
- config : StartCliConfig
15
+ config : StartCliConfig | ServerConfig
15
16
) : string {
16
17
const prefix = config . legacyMode ? '/asset' : '' ;
17
18
return chalk `{dim ${ config . url } ${ prefix } }${ asset . path } ` ;
18
19
}
19
20
20
21
function printFunction (
21
22
fn : ServerlessResourceConfig ,
22
- config : StartCliConfig
23
+ config : StartCliConfig | ServerConfig
23
24
) : string {
24
25
return chalk `{dim ${ config . url } }${ fn . path } ` ;
25
26
}
26
27
27
28
function printPlainRouteInfo (
28
29
functions : ServerlessResourceConfig [ ] ,
29
30
assets : ServerlessResourceConfig [ ] ,
30
- config : StartCliConfig
31
+ config : StartCliConfig | ServerConfig
31
32
) : string {
32
33
const functionHeading = 'Functions' ;
33
34
let functionInfo ;
34
35
if ( functions . length > 0 ) {
35
- functionInfo = functions . map ( fn => printFunction ( fn , config ) ) . join ( '\n' ) ;
36
+ functionInfo = functions . map ( ( fn ) => printFunction ( fn , config ) ) . join ( '\n' ) ;
36
37
} else {
37
38
functionInfo = 'No functions found' ;
38
39
}
39
40
40
41
const assetHeading = 'Assets' ;
41
42
let assetInfo ;
42
43
if ( assets . length > 0 ) {
43
- assetInfo = assets . map ( asset => printAsset ( asset , config ) ) . join ( '\n' ) ;
44
+ assetInfo = assets . map ( ( asset ) => printAsset ( asset , config ) ) . join ( '\n' ) ;
44
45
} else {
45
46
assetInfo = 'No assets found' ;
46
47
}
@@ -67,7 +68,7 @@ function printPlainRouteInfo(
67
68
68
69
function prettyPrintAsset (
69
70
asset : ServerlessResourceConfig ,
70
- config : StartCliConfig
71
+ config : StartCliConfig | ServerConfig
71
72
) : string {
72
73
const prefix = config . legacyMode ? '/asset' : '' ;
73
74
const assetPath = prefix + asset . path ;
@@ -83,7 +84,7 @@ function prettyPrintAsset(
83
84
84
85
function prettyPrintFunction (
85
86
fn : ServerlessResourceConfig ,
86
- config : StartCliConfig
87
+ config : StartCliConfig | ServerConfig
87
88
) {
88
89
const accessPrefix =
89
90
fn . access === 'protected' ? chalk . cyan . bold ( '[protected] ' ) : '' ;
@@ -94,7 +95,7 @@ function prettyPrintFunction(
94
95
function printPrettyRouteInfo (
95
96
functions : ServerlessResourceConfig [ ] ,
96
97
assets : ServerlessResourceConfig [ ] ,
97
- config : StartCliConfig
98
+ config : StartCliConfig | ServerConfig
98
99
) : string {
99
100
const functionHeading = chalk `{green.bold Twilio functions available:}` ;
100
101
let functionInfo ;
@@ -146,15 +147,34 @@ function printPrettyRouteInfo(
146
147
return boxen ( wrappedOutput , { padding : 1 } ) ;
147
148
}
148
149
149
- export async function printRouteInfo ( config : StartCliConfig ) : Promise < void > {
150
- const searchConfig : SearchConfig = {
151
- functionsFolderNames : config . functionsFolderName
152
- ? [ config . functionsFolderName ]
153
- : undefined ,
154
- assetsFolderNames : config . assetsFolderName
155
- ? [ config . assetsFolderName ]
156
- : undefined ,
157
- } ;
150
+ // A magic function from https://fettblog.eu/typescript-hasownproperty/
151
+ // This narrows down a type based on the property that an object has. It is used
152
+ // below to distinguish between `StartCliConfig` and `ServerConfig` as the
153
+ // `ServerConfig` does not have functionsFolderName or assetsFolderName
154
+ // properties.
155
+ function hasOwnProperty < X extends { } , Y extends PropertyKey > (
156
+ obj : X ,
157
+ prop : Y
158
+ ) : obj is X & Record < Y , unknown > {
159
+ return obj . hasOwnProperty ( prop ) ;
160
+ }
161
+
162
+ export async function printRouteInfo (
163
+ config : StartCliConfig | ServerConfig
164
+ ) : Promise < void > {
165
+ const searchConfig : SearchConfig = { } ;
166
+ if (
167
+ hasOwnProperty ( config , 'functionsFolderName' ) &&
168
+ typeof config . functionsFolderName === 'string'
169
+ ) {
170
+ searchConfig . functionsFolderNames = [ config . functionsFolderName ] ;
171
+ }
172
+ if (
173
+ hasOwnProperty ( config , 'assetsFolderName' ) &&
174
+ typeof config . assetsFolderName === 'string'
175
+ ) {
176
+ searchConfig . assetsFolderNames = [ config . assetsFolderName ] ;
177
+ }
158
178
159
179
const { functions, assets } = await getFunctionsAndAssets (
160
180
config . baseDir ,
0 commit comments