From 4d4f09f3e208ca3c6d906e338e339471921581b1 Mon Sep 17 00:00:00 2001 From: tsuf239 <39455181+tsuf239@users.noreply.github.com> Date: Tue, 1 Oct 2024 14:37:47 +0300 Subject: [PATCH] creating zip manually --- packages/@winglang/sdk/package.json | 4 +- .../sdk/src/target-tf-aws/function.ts | 21 +++-- pnpm-lock.yaml | 23 +++++- tests/valid/folder with spaces/main.test.w | 7 ++ .../main.test.w.tf-aws.snap.md | 81 +++++++++++++++++++ 5 files changed, 124 insertions(+), 12 deletions(-) create mode 100644 tests/valid/folder with spaces/main.test.w create mode 100644 tests/valid/folder with spaces/main.test.w.tf-aws.snap.md diff --git a/packages/@winglang/sdk/package.json b/packages/@winglang/sdk/package.json index 8f59d40e1d5..8b04523e948 100644 --- a/packages/@winglang/sdk/package.json +++ b/packages/@winglang/sdk/package.json @@ -44,6 +44,7 @@ "@types/mime-types": "^2.1.4", "@types/node": "^20.11.0", "@types/uuid": "^9.0.8", + "@types/adm-zip": "^0.5.5", "@typescript-eslint/eslint-plugin": "^7", "@typescript-eslint/parser": "^7", "@winglang/jsii-docgen": "workspace:^", @@ -73,7 +74,8 @@ "tsx": "^4.15.7", "typescript": "^5.5.2", "vitest": "^1.6.0", - "wing-api-checker": "workspace:^" + "wing-api-checker": "workspace:^", + "adm-zip": "^0.5.16" }, "peerDependencies": { "constructs": "^10.3" diff --git a/packages/@winglang/sdk/src/target-tf-aws/function.ts b/packages/@winglang/sdk/src/target-tf-aws/function.ts index afd4942eb75..4baaa75cfb7 100644 --- a/packages/@winglang/sdk/src/target-tf-aws/function.ts +++ b/packages/@winglang/sdk/src/target-tf-aws/function.ts @@ -1,5 +1,7 @@ -import { AssetType, Lazy, TerraformAsset } from "cdktf"; +import { join } from "path"; +import { Lazy } from "cdktf"; import { Construct } from "constructs"; +import { ensureDirSync } from "fs-extra"; import { App } from "./app"; import { CloudwatchLogGroup } from "../.gen/providers/aws/cloudwatch-log-group"; import { IamRole } from "../.gen/providers/aws/iam-role"; @@ -26,6 +28,7 @@ import { import { makeAwsLambdaHandler } from "../shared-aws/function-util"; import { IInflightHost, Resource } from "../std"; import { Duration } from "../std/duration"; +import AdmZip from "adm-zip"; /** * Function names are limited to 64 characters. @@ -284,15 +287,17 @@ export class Function extends cloud.Function implements IAwsFunction { const bundle = createBundle(this.entrypoint, externalLibraries); - // would prefer to create TerraformAsset in the constructor, but using a CDKTF token for - // the "path" argument isn't supported - const asset = new TerraformAsset(this, "Asset", { - path: bundle.directory, - type: AssetType.ARCHIVE, - }); + // we used to use terraformAsset- but it can't handle file paths that contain spaces- so we create the bundle zip manually. + const zip = new AdmZip(); + zip.addLocalFolder(bundle.directory); + + const folderPath = join(App.of(this).outdir, "assets"); + const filename = `${this.node.addr}.zip`; + ensureDirSync(folderPath); + zip.writeZip(join(folderPath, filename)); + this.assetPath = join("assets", filename); this.bundleHash = bundle.hash; - this.assetPath = asset.path; } /** @internal */ diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3bdb1046a90..c264706c8d3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -425,6 +425,9 @@ importers: '@cdktf/provider-aws': specifier: ^19 version: 19.23.0(cdktf@0.20.7)(constructs@10.3.0) + '@types/adm-zip': + specifier: ^0.5.5 + version: 0.5.5 '@types/express': specifier: ^4.17.21 version: 4.17.21 @@ -452,6 +455,9 @@ importers: '@winglang/jsii-docgen': specifier: workspace:^ version: link:../jsii-docgen + adm-zip: + specifier: ^0.5.16 + version: 0.5.16 aws-sdk-client-mock: specifier: 3.0.0 version: 3.0.0 @@ -10520,6 +10526,12 @@ packages: minimatch: 9.0.4 dev: false + /@types/adm-zip@0.5.5: + resolution: {integrity: sha512-YCGstVMjc4LTY5uK9/obvxBya93axZOVOyf2GSUulADzmLhYE45u2nAssCs/fWBs1Ifq5Vat75JTPwd5XZoPJw==} + dependencies: + '@types/node': 22.5.5 + dev: true + /@types/aria-query@5.0.4: resolution: {integrity: sha512-rfT93uj5s0PRL7EzccGMs3brplhcrghnDoV26NqKhCAS1hVo+WdNsPvE/yb6ilfr5hi2MEk6d5EWJTKdxg8jVw==} dev: true @@ -12084,6 +12096,11 @@ packages: engines: {node: '>= 10.0.0'} dev: true + /adm-zip@0.5.16: + resolution: {integrity: sha512-TGw5yVi4saajsSEgz25grObGHEUaDrniwvA2qwSC060KfqGPdglhvPMA2lPIoxs3PQIItj2iag35fONcQqgUaQ==} + engines: {node: '>=12.0'} + dev: true + /agent-base@5.1.1: resolution: {integrity: sha512-TMeqbNl2fMW0nMjTEPOwe3J/PRFP4vqeoNuQMG0HlMrtm5QxKqdvAkZ1pRBQ/ulIyDD5Yq0nJ7YbdD8ey0TO3g==} engines: {node: '>= 6.0.0'} @@ -14299,7 +14316,7 @@ packages: dependencies: semver: 7.6.3 shelljs: 0.8.5 - typescript: 5.7.0-dev.20240917 + typescript: 5.7.0-dev.20241001 dev: true /dset@3.1.3: @@ -22621,8 +22638,8 @@ packages: engines: {node: '>=14.17'} hasBin: true - /typescript@5.7.0-dev.20240917: - resolution: {integrity: sha512-LoeyRa9RbB6lMasZvBmepDPrI6KNHURSleqbQ4BL4lxvXbCAQkMgmRzqyjA2DJE0ytuj5OEPFU5k/g3f9GaQKA==} + /typescript@5.7.0-dev.20241001: + resolution: {integrity: sha512-E+sQ0WmFXNyLddzyWmBBVwyn4NpBRgVJoiq8lLy25nQBLTKFMPnHmrKq+09uOv35ey3XhMA6y3lVzayuukJRpQ==} engines: {node: '>=14.17'} hasBin: true dev: true diff --git a/tests/valid/folder with spaces/main.test.w b/tests/valid/folder with spaces/main.test.w new file mode 100644 index 00000000000..68691a2503b --- /dev/null +++ b/tests/valid/folder with spaces/main.test.w @@ -0,0 +1,7 @@ +bring cloud; + +let bucket = new cloud.Bucket(); + +test "Bucket is empty" { + assert(bucket.list().length == 0); +} \ No newline at end of file diff --git a/tests/valid/folder with spaces/main.test.w.tf-aws.snap.md b/tests/valid/folder with spaces/main.test.w.tf-aws.snap.md new file mode 100644 index 00000000000..d117455af8d --- /dev/null +++ b/tests/valid/folder with spaces/main.test.w.tf-aws.snap.md @@ -0,0 +1,81 @@ +# `main.test.w.tf-aws.snap.md` + +## main.tf.json + +```json +{ + "//": { + "metadata": { + "backend": "local", + "stackName": "root", + "version": "0.20.7" + }, + "outputs": { + } + }, + "provider": { + "aws": [ + { + } + ] + }, + "resource": { + "aws_s3_bucket": { + "Bucket": { + "//": { + "metadata": { + "path": "root/Default/Default/Bucket/Default", + "uniqueId": "Bucket" + } + }, + "bucket_prefix": "bucket-c88fdc5f-", + "force_destroy": false + } + }, + "aws_s3_bucket_cors_configuration": { + "Bucket_CorsConfiguration-1357ca3a_A4CCA40A": { + "//": { + "metadata": { + "path": "root/Default/Default/Bucket/CorsConfiguration-1357ca3a", + "uniqueId": "Bucket_CorsConfiguration-1357ca3a_A4CCA40A" + } + }, + "bucket": "${aws_s3_bucket.Bucket.id}", + "cors_rule": [ + { + "allowed_headers": [ + "*" + ], + "allowed_methods": [ + "GET", + "POST", + "PUT", + "DELETE", + "HEAD" + ], + "allowed_origins": [ + "*" + ], + "expose_headers": [ + ], + "max_age_seconds": 0 + } + ] + } + } + }, + "terraform": { + "backend": { + "local": { + "path": "./terraform.tfstate" + } + }, + "required_providers": { + "aws": { + "source": "aws", + "version": "5.56.1" + } + } + } +} +```