5
5
6
6
import { fileExists } from '../../filesystemUtilities'
7
7
import { getLogger , Logger } from '../../logger'
8
- import { DefaultSamCliProcessInvoker } from './samCliInvoker'
9
8
import { logAndThrowIfUnexpectedExitCode , SamCliProcessInvoker } from './samCliInvokerUtils'
9
+ import { DefaultSamCliProcessInvoker } from './samCliInvoker'
10
10
11
11
export interface SamCliBuildInvocationArguments {
12
12
/**
@@ -23,16 +23,17 @@ export interface SamCliBuildInvocationArguments {
23
23
*/
24
24
templatePath : string
25
25
/**
26
- * Environment variables to set on the child process.
26
+ * Environment variables set when invoking the SAM process (NOT passed to the Lambda) .
27
27
*/
28
28
environmentVariables ?: NodeJS . ProcessEnv
29
29
/**
30
30
* Manages the sam cli execution.
31
31
*/
32
32
invoker : SamCliProcessInvoker
33
33
/**
34
- * If your functions depend on packages that have natively compiled dependencies,
35
- * use this flag to build your function inside an AWS Lambda-like Docker container.
34
+ * - true: If your Lambda depends on packages that need to be compiled natively,
35
+ * use this flag to build your function inside an AWS Lambda-like Docker container.
36
+ * - false: Lambda will be built on local machine instead of in a Docker image.
36
37
*/
37
38
useContainer ?: boolean
38
39
/**
@@ -42,13 +43,16 @@ export interface SamCliBuildInvocationArguments {
42
43
*/
43
44
dockerNetwork ?: string
44
45
/**
45
- * Specifies whether the command should skip pulling down the latest Docker image for Lambda runtime.
46
+ * - true: Do not pull the latest Docker image for Lambda runtime.
47
+ * - false: Pull the latest Docker image if necessary
46
48
*/
47
49
skipPullImage ?: boolean
48
50
/**
49
51
* The path to a custom dependency manifest (ex: package.json) to use instead of the default one.
50
52
*/
51
53
manifestPath ?: string
54
+ /** SAM args specified by user (`sam.buildArguments`). */
55
+ extraArgs ?: string [ ]
52
56
}
53
57
54
58
export interface FileFunctions {
@@ -59,64 +63,44 @@ export interface FileFunctions {
59
63
* An elaborate way to run `sam build`.
60
64
*/
61
65
export class SamCliBuildInvocation {
62
- private readonly buildDir : string
63
- private readonly baseDir ?: string
64
- private readonly environmentVariables ?: NodeJS . ProcessEnv
65
- private readonly templatePath : string
66
- private readonly invoker : SamCliProcessInvoker
67
- private readonly useContainer : boolean
68
- private readonly dockerNetwork ?: string
69
- private readonly skipPullImage : boolean
70
- private readonly manifestPath ?: string
71
-
72
- /**
73
- * @see SamCliBuildInvocationArguments for parameter info
74
- * invoker - Defaults to DefaultSamCliProcessInvoker
75
- * useContainer - Defaults to false (function will be built on local machine instead of in a docker image)
76
- * skipPullImage - Defaults to false (the latest Docker image will be pulled down if necessary)
77
- */
78
66
public constructor (
79
- {
80
- invoker = new DefaultSamCliProcessInvoker ( ) ,
81
- useContainer = false ,
82
- skipPullImage = false ,
83
- ...params
84
- } : SamCliBuildInvocationArguments ,
67
+ private readonly args : SamCliBuildInvocationArguments ,
85
68
private readonly context : { file : FileFunctions } = { file : getDefaultFileFunctions ( ) }
86
69
) {
87
- this . buildDir = params . buildDir
88
- this . baseDir = params . baseDir
89
- this . templatePath = params . templatePath
90
- this . environmentVariables = params . environmentVariables
91
- this . invoker = invoker
92
- this . useContainer = useContainer
93
- this . dockerNetwork = params . dockerNetwork
94
- this . skipPullImage = skipPullImage
95
- this . manifestPath = params . manifestPath
70
+ this . args . invoker = this . args . invoker ?? new DefaultSamCliProcessInvoker ( )
71
+ this . args . useContainer = ! ! this . args . useContainer
72
+ this . args . skipPullImage = ! ! this . args . skipPullImage
96
73
}
97
74
98
- public async execute ( ) : Promise < void > {
75
+ /**
76
+ *
77
+ * @returns process exit/status code
78
+ */
79
+ public async execute ( ) : Promise < number > {
99
80
await this . validate ( )
100
81
101
- const invokeArgs : string [ ] = [ 'build' , '--build-dir' , this . buildDir , '--template' , this . templatePath ]
82
+ const invokeArgs : string [ ] = [ 'build' , '--build-dir' , this . args . buildDir , '--template' , this . args . templatePath ]
102
83
103
- this . addArgumentIf ( invokeArgs , ! ! this . baseDir , '--base-dir' , this . baseDir ! )
104
- this . addArgumentIf ( invokeArgs , ! ! this . dockerNetwork , '--docker-network' , this . dockerNetwork ! )
105
- this . addArgumentIf ( invokeArgs , ! ! this . useContainer , '--use-container' )
106
- this . addArgumentIf ( invokeArgs , ! ! this . skipPullImage , '--skip-pull-image' )
107
- this . addArgumentIf ( invokeArgs , ! ! this . manifestPath , '--manifest' , this . manifestPath ! )
84
+ this . addArgumentIf ( invokeArgs , ! ! this . args . baseDir , '--base-dir' , this . args . baseDir ! )
85
+ this . addArgumentIf ( invokeArgs , ! ! this . args . dockerNetwork , '--docker-network' , this . args . dockerNetwork ! )
86
+ this . addArgumentIf ( invokeArgs , ! ! this . args . useContainer , '--use-container' )
87
+ this . addArgumentIf ( invokeArgs , ! ! this . args . skipPullImage , '--skip-pull-image' )
88
+ this . addArgumentIf ( invokeArgs , ! ! this . args . manifestPath , '--manifest' , this . args . manifestPath ! )
89
+ invokeArgs . push ( ...( this . args . extraArgs ?? [ ] ) )
108
90
109
91
const env : NodeJS . ProcessEnv = {
110
92
...process . env ,
111
- ...this . environmentVariables ,
93
+ ...this . args . environmentVariables ,
112
94
}
113
95
114
- const childProcessResult = await this . invoker . invoke ( {
96
+ const childProcessResult = await this . args . invoker . invoke ( {
115
97
spawnOptions : { env } ,
116
98
arguments : invokeArgs ,
117
99
} )
118
100
119
101
logAndThrowIfUnexpectedExitCode ( childProcessResult , 0 )
102
+
103
+ return childProcessResult . exitCode
120
104
}
121
105
122
106
private addArgumentIf ( args : string [ ] , addIfConditional : boolean , ...argsToAdd : string [ ] ) {
@@ -126,10 +110,10 @@ export class SamCliBuildInvocation {
126
110
}
127
111
128
112
private async validate ( ) : Promise < void > {
129
- if ( ! ( await this . context . file . fileExists ( this . templatePath ) ) ) {
113
+ if ( ! ( await this . context . file . fileExists ( this . args . templatePath ) ) ) {
130
114
const logger : Logger = getLogger ( )
131
115
132
- const err = new Error ( `template path does not exist: ${ this . templatePath } ` )
116
+ const err = new Error ( `template path does not exist: ${ this . args . templatePath } ` )
133
117
logger . error ( err )
134
118
throw err
135
119
}
0 commit comments