"use strict";var _a,_b;Object.defineProperty(exports,"__esModule",{value:!0}),exports.DockerVolumeConsistency=exports.DockerImage=exports.BundlingDockerImage=exports.BundlingOutput=void 0;const jsiiDeprecationWarnings=require("../../.warnings.jsii.js"),JSII_RTTI_SYMBOL_1=Symbol.for("jsii.rtti"),child_process_1=require("child_process"),crypto=require("crypto"),path_1=require("path"),fs_1=require("./fs"),jsii_deprecated_1=require("./private/jsii-deprecated");var BundlingOutput;(function(BundlingOutput2){BundlingOutput2.ARCHIVED="archived",BundlingOutput2.NOT_ARCHIVED="not-archived",BundlingOutput2.AUTO_DISCOVER="auto-discover"})(BundlingOutput=exports.BundlingOutput||(exports.BundlingOutput={}));class BundlingDockerImage{constructor(image,_imageHash){this.image=image,this._imageHash=_imageHash}static fromRegistry(image){return new DockerImage(image)}static fromAsset(path,options={}){return DockerImage.fromBuild(path,options)}toJSON(){var _c;return(_c=this._imageHash)!==null&&_c!==void 0?_c:this.image}run(options={}){var _c,_d;const volumes=options.volumes||[],environment=options.environment||{},entrypoint=((_c=options.entrypoint)===null||_c===void 0?void 0:_c[0])||null,command=[...((_d=options.entrypoint)===null||_d===void 0?void 0:_d[1])?[...options.entrypoint.slice(1)]:[],...options.command?[...options.command]:[]],dockerArgs=["run","--rm",...options.securityOpt?["--security-opt",options.securityOpt]:[],...options.user?["-u",options.user]:[],...flatten(volumes.map(v=>{var _c2;return["-v",`${v.hostPath}:${v.containerPath}:${isSeLinux()?"z,":""}${(_c2=v.consistency)!==null&&_c2!==void 0?_c2:DockerVolumeConsistency.DELEGATED}`]})),...flatten(Object.entries(environment).map(([k,v])=>["--env",`${k}=${v}`])),...options.workingDirectory?["-w",options.workingDirectory]:[],...entrypoint?["--entrypoint",entrypoint]:[],this.image,...command];dockerExec(dockerArgs)}cp(imagePath,outputPath){const{stdout}=dockerExec(["create",this.image],{}),match=stdout.toString().match(/([0-9a-f]{16,})/);if(!match)throw new Error("Failed to extract container ID from Docker create output");const containerId=match[1],containerPath=`${containerId}:${imagePath}`,destPath=outputPath!=null?outputPath:fs_1.FileSystem.mkdtemp("cdk-docker-cp-");try{return dockerExec(["cp",containerPath,destPath]),destPath}catch(err){throw new Error(`Failed to copy files from ${containerPath} to ${destPath}: ${err}`)}finally{dockerExec(["rm","-v",containerId])}}}exports.BundlingDockerImage=BundlingDockerImage,_a=JSII_RTTI_SYMBOL_1,BundlingDockerImage[_a]={fqn:"aws-cdk-lib.BundlingDockerImage",version:"2.13.0"};class DockerImage extends BundlingDockerImage{constructor(image,_imageHash){const deprecated=jsii_deprecated_1.quiet();super(image,_imageHash);jsii_deprecated_1.reset(deprecated),this.image=image}static fromBuild(path,options={}){jsiiDeprecationWarnings.aws_cdk_lib_DockerBuildOptions(options);const buildArgs=options.buildArgs||{};if(options.file&&path_1.isAbsolute(options.file))throw new Error(`"file" must be relative to the docker build directory. Got ${options.file}`);const input=JSON.stringify({path,...options}),tag=`cdk-${crypto.createHash("sha256").update(input).digest("hex")}`,dockerArgs=["build","-t",tag,...options.file?["-f",path_1.join(path,options.file)]:[],...options.platform?["--platform",options.platform]:[],...flatten(Object.entries(buildArgs).map(([k,v])=>["--build-arg",`${k}=${v}`])),path];dockerExec(dockerArgs);const hash=fs_1.FileSystem.fingerprint(path,{extraHash:JSON.stringify(options)});return new DockerImage(tag,hash)}static fromRegistry(image){return new DockerImage(image)}toJSON(){const deprecated=jsii_deprecated_1.quiet(),json=super.toJSON();return jsii_deprecated_1.reset(deprecated),json}run(options={}){jsiiDeprecationWarnings.aws_cdk_lib_DockerRunOptions(options);const deprecated=jsii_deprecated_1.quiet(),result=super.run(options);return jsii_deprecated_1.reset(deprecated),result}cp(imagePath,outputPath){const deprecated=jsii_deprecated_1.quiet(),result=super.cp(imagePath,outputPath);return jsii_deprecated_1.reset(deprecated),result}}exports.DockerImage=DockerImage,_b=JSII_RTTI_SYMBOL_1,DockerImage[_b]={fqn:"aws-cdk-lib.DockerImage",version:"2.13.0"};var DockerVolumeConsistency;(function(DockerVolumeConsistency2){DockerVolumeConsistency2.CONSISTENT="consistent",DockerVolumeConsistency2.DELEGATED="delegated",DockerVolumeConsistency2.CACHED="cached"})(DockerVolumeConsistency=exports.DockerVolumeConsistency||(exports.DockerVolumeConsistency={}));function flatten(x){return Array.prototype.concat([],...x)}function dockerExec(args,options){var _c,_d,_e;const prog=(_c=process.env.CDK_DOCKER)!==null&&_c!==void 0?_c:"docker",proc=child_process_1.spawnSync(prog,args,options!=null?options:{stdio:["ignore",process.stderr,"inherit"]});if(proc.error)throw proc.error;if(proc.status!==0)throw proc.stdout||proc.stderr?new Error(`[Status ${proc.status}] stdout: ${(_d=proc.stdout)===null||_d===void 0?void 0:_d.toString().trim()}


stderr: ${(_e=proc.stderr)===null||_e===void 0?void 0:_e.toString().trim()}`):new Error(`${prog} exited with status ${proc.status}`);return proc}function isSeLinux(){if(process.platform!="linux")return!1;const prog="selinuxenabled",proc=child_process_1.spawnSync(prog,[],{stdio:["pipe",process.stderr,"inherit"]});return proc.error?!1:proc.status==0}
//# sourceMappingURL=bundling.js.map
