1+ import * as path from 'path' ;
12import * as cxapi from '@aws-cdk/cx-api' ;
23import * as fs from 'fs-extra' ;
34import type { AssemblyDirectoryProps , AssemblySourceProps , ICloudAssemblySource } from '../' ;
@@ -25,9 +26,10 @@ export abstract class CloudAssemblySourceBuilder {
2526 /**
2627 * Create a Cloud Assembly from a Cloud Assembly builder function.
2728 *
28- * A temporary output directory will be created if no output directory is
29- * explicitly given. This directory will be cleaned up if synthesis fails, or
30- * when the Cloud Assembly produced by this source is disposed.
29+ * The output directory will be evaluated with respect to the working
30+ * directory if relative. If not given, it will synthesize into a temporary
31+ * system directory. The temporary directory will be cleaned up, unless
32+ * `deleteOutdir: false`.
3133 *
3234 * A write lock will be acquired on the output directory for the duration of
3335 * the CDK app synthesis (which means that no two apps can synthesize at the
@@ -51,10 +53,13 @@ export abstract class CloudAssemblySourceBuilder {
5153 lookups : props . lookups ,
5254 } ;
5355
56+ const workingDirectory = props . workingDirectory ?? process . cwd ( ) ;
57+ const outdir = props . outdir ? path . resolve ( workingDirectory , props . outdir ) : undefined ;
58+
5459 return new ContextAwareCloudAssemblySource (
5560 {
5661 produce : async ( ) => {
57- await using execution = await ExecutionEnvironment . create ( services , { outdir : props . outdir } ) ;
62+ await using execution = await ExecutionEnvironment . create ( services , { outdir } ) ;
5863
5964 const env = await execution . defaultEnvVars ( ) ;
6065 const assembly = await execution . changeDir ( async ( ) =>
@@ -74,15 +79,16 @@ export abstract class CloudAssemblySourceBuilder {
7479 throw AssemblyError . withCause ( 'Assembly builder failed' , error ) ;
7580 }
7681 } ) ,
77- ) , props . workingDirectory ) ;
82+ ) , workingDirectory ) ;
7883
7984 // Convert what we got to the definitely correct type we're expecting, a cxapi.CloudAssembly
8085 const asm = cxapi . CloudAssembly . isCloudAssembly ( assembly )
8186 ? assembly
8287 : await assemblyFromDirectory ( assembly . directory , services . ioHelper , props . loadAssemblyOptions ) ;
8388
8489 const success = await execution . markSuccessful ( ) ;
85- return new ReadableCloudAssembly ( asm , success . readLock , { deleteOnDispose : execution . outDirIsTemporary } ) ;
90+ const deleteOnDispose = props . deleteOutdir ?? execution . outDirIsTemporary ;
91+ return new ReadableCloudAssembly ( asm , success . readLock , { deleteOnDispose } ) ;
8692 } ,
8793 } ,
8894 contextAssemblyProps ,
@@ -129,9 +135,10 @@ export abstract class CloudAssemblySourceBuilder {
129135 /**
130136 * Use a directory containing an AWS CDK app as source.
131137 *
132- * A temporary output directory will be created if no output directory is
133- * explicitly given. This directory will be cleaned up if synthesis fails, or
134- * when the Cloud Assembly produced by this source is disposed.
138+ * The output directory will be evaluated with respect to the working
139+ * directory if relative. If not given, it will synthesize into a `cdk.out`
140+ * subdirectory. This directory will not be cleaned up, unless
141+ * `deleteOutdir: true`.
135142 *
136143 * A write lock will be acquired on the output directory for the duration of
137144 * the CDK app synthesis (which means that no two apps can synthesize at the
@@ -152,6 +159,9 @@ export abstract class CloudAssemblySourceBuilder {
152159 lookups : props . lookups ,
153160 } ;
154161
162+ const workingDirectory = props . workingDirectory ?? process . cwd ( ) ;
163+ const outdir = path . resolve ( workingDirectory , props . outdir ?? 'cdk.out' ) ;
164+
155165 return new ContextAwareCloudAssemblySource (
156166 {
157167 produce : async ( ) => {
@@ -161,7 +171,6 @@ export abstract class CloudAssemblySourceBuilder {
161171 // await execInChildProcess(build, { cwd: props.workingDirectory });
162172 // }
163173
164- const outdir = props . outdir ?? 'cdk.out' ;
165174 try {
166175 fs . mkdirpSync ( outdir ) ;
167176 } catch ( e : any ) {
@@ -185,13 +194,14 @@ export abstract class CloudAssemblySourceBuilder {
185194 }
186195 } ,
187196 extraEnv : envWithContext ,
188- cwd : props . workingDirectory ,
197+ cwd : workingDirectory ,
189198 } ) ;
190199
191200 const asm = await assemblyFromDirectory ( outdir , services . ioHelper , props . loadAssemblyOptions ) ;
192201
193202 const success = await execution . markSuccessful ( ) ;
194- return new ReadableCloudAssembly ( asm , success . readLock , { deleteOnDispose : execution . outDirIsTemporary } ) ;
203+ const deleteOnDispose = props . deleteOutdir ?? execution . outDirIsTemporary ;
204+ return new ReadableCloudAssembly ( asm , success . readLock , { deleteOnDispose } ) ;
195205 } ) ;
196206 } ,
197207 } ,
0 commit comments