From e5eb81fe3159d97d8173f26227ca9ad29379b312 Mon Sep 17 00:00:00 2001 From: Corneliu Croitoru Date: Fri, 24 May 2024 19:05:54 +0200 Subject: [PATCH] zip at install --- .npmignore | 2 ++ CHANGELOG.md | 1 + bin/cli/actions/deploy.ts | 17 -------------- package.json | 4 +++- resources/cff_templates/index_angularjs.js | 4 ++-- resources/cff_templates/index_basic.js | 5 +++-- resources/cff_templates/index_nextjs.js | 5 +++-- resources/cff_templates/index_reactjs.js | 5 +++-- resources/cff_templates/index_vuejs.js | 5 +++-- scripts/createDummyZip.js | 26 ++++++++++++++++++++++ 10 files changed, 46 insertions(+), 28 deletions(-) create mode 100644 scripts/createDummyZip.js diff --git a/.npmignore b/.npmignore index 51ba691..f68fa3d 100644 --- a/.npmignore +++ b/.npmignore @@ -11,6 +11,8 @@ !lambda/**/*.js !resources/**/*/*.* +!scripts/*.js + !package.json !package-lock.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b6f450..e72d506 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,5 +67,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Use one CloudFront Function template per framework +- Generate the necessary dummy zip file required by the Pipeline during installation, instead of using the CLI deploy command ### Fixed diff --git a/bin/cli/actions/deploy.ts b/bin/cli/actions/deploy.ts index a36ab04..e2e26bb 100644 --- a/bin/cli/actions/deploy.ts +++ b/bin/cli/actions/deploy.ts @@ -62,19 +62,6 @@ interface Command { cmd: any; } -export async function createZipArchive() { - const outputFile = getBuildConfigS3Folder() + "/dummy.zip"; - // Check if the zip file already exists - if (fs.existsSync(outputFile)) { - return; - } - - const zip = new AdmZip(); - - zip.addLocalFile(getBuildConfigS3Folder() + "/dummy.txt"); - zip.writeZip(outputFile); - -} export default async function handleDeployCommand() { await checkAWSConnection(); @@ -95,10 +82,6 @@ export default async function handleDeployCommand() { var counter = 1; const hostingConfiguration = await loadHostingConfiguration(); - if (isS3Config(hostingConfiguration)) { - await createZipArchive(); - } - var certificateArnCmdParam = ""; if (hostingConfiguration.domainName) { console.log( diff --git a/package.json b/package.json index 4008b58..48d2e24 100644 --- a/package.json +++ b/package.json @@ -34,8 +34,10 @@ "audit": "npm audit && cdk synth | cfn_nag", "cloudfront-hosting-toolkit": "node bin/cli/index.js", "test": "jest --coverage", - "postinstall": "cd lambda/layers/aws_sdk/nodejs && npm install" + "zipfile": "node scripts/createDummyZip.js", + "postinstall": "cd lambda/layers/aws_sdk/nodejs && npm install && cd ../../../.. && npm run zipfile" }, + "devDependencies": { "@types/archiver": "5.3.2", "@types/aws-lambda": "^8.10.133", diff --git a/resources/cff_templates/index_angularjs.js b/resources/cff_templates/index_angularjs.js index 8f70556..fb6335c 100644 --- a/resources/cff_templates/index_angularjs.js +++ b/resources/cff_templates/index_angularjs.js @@ -26,9 +26,9 @@ async function handler(event) { var request = event.request; var uri = request.uri; - //console.log("URI BEFORE: " + request.uri); -> Uncomment if needed + //console.log("URI BEFORE: " + request.uri); // Uncomment if needed request.uri = await updateURI(uri); - //console.log("URI AFTER: " + request.uri);-> Uncomment if needed + //console.log("URI AFTER: " + request.uri); // Uncomment if needed return request; } diff --git a/resources/cff_templates/index_basic.js b/resources/cff_templates/index_basic.js index 68fb0f3..3f402f2 100644 --- a/resources/cff_templates/index_basic.js +++ b/resources/cff_templates/index_basic.js @@ -52,9 +52,10 @@ async function handler(event) { var request = event.request; var uri = request.uri; - //console.log("URI BEFORE: " + request.uri); -> Uncomment if needed + //console.log("URI BEFORE: " + request.uri); // Uncomment if needed request.uri = await updateURI(uri); - //console.log("URI AFTER: " + request.uri);-> Uncomment if needed + //console.log("URI AFTER: " + request.uri); // Uncomment if needed + return request; diff --git a/resources/cff_templates/index_nextjs.js b/resources/cff_templates/index_nextjs.js index 68fb0f3..3f402f2 100644 --- a/resources/cff_templates/index_nextjs.js +++ b/resources/cff_templates/index_nextjs.js @@ -52,9 +52,10 @@ async function handler(event) { var request = event.request; var uri = request.uri; - //console.log("URI BEFORE: " + request.uri); -> Uncomment if needed + //console.log("URI BEFORE: " + request.uri); // Uncomment if needed request.uri = await updateURI(uri); - //console.log("URI AFTER: " + request.uri);-> Uncomment if needed + //console.log("URI AFTER: " + request.uri); // Uncomment if needed + return request; diff --git a/resources/cff_templates/index_reactjs.js b/resources/cff_templates/index_reactjs.js index d312531..fa647fe 100644 --- a/resources/cff_templates/index_reactjs.js +++ b/resources/cff_templates/index_reactjs.js @@ -33,9 +33,10 @@ async function handler(event) { var request = event.request; var uri = request.uri; - //console.log("URI BEFORE: " + request.uri); -> Uncomment if needed + //console.log("URI BEFORE: " + request.uri); // Uncomment if needed request.uri = await updateURI(uri); - //console.log("URI AFTER: " + request.uri);-> Uncomment if needed + //console.log("URI AFTER: " + request.uri); // Uncomment if needed + return request; diff --git a/resources/cff_templates/index_vuejs.js b/resources/cff_templates/index_vuejs.js index 68fb0f3..3f402f2 100644 --- a/resources/cff_templates/index_vuejs.js +++ b/resources/cff_templates/index_vuejs.js @@ -52,9 +52,10 @@ async function handler(event) { var request = event.request; var uri = request.uri; - //console.log("URI BEFORE: " + request.uri); -> Uncomment if needed + //console.log("URI BEFORE: " + request.uri); // Uncomment if needed request.uri = await updateURI(uri); - //console.log("URI AFTER: " + request.uri);-> Uncomment if needed + //console.log("URI AFTER: " + request.uri); // Uncomment if needed + return request; diff --git a/scripts/createDummyZip.js b/scripts/createDummyZip.js new file mode 100644 index 0000000..6189d57 --- /dev/null +++ b/scripts/createDummyZip.js @@ -0,0 +1,26 @@ +// scripts/createZipArchive.js + +const fs = require("fs"); +const AdmZip = require("adm-zip"); + +console.log("createZipArchive"); +const outputFile = "resources/s3_trigger/dummy.zip"; // Adjust the path as needed + +// Check if the zip file already exists +if (fs.existsSync(outputFile)) { + console.log("Zip file already exists."); + return; +} + +console.log("Creating new zip file..."); + +const zip = new AdmZip(); + +try { + zip.addLocalFile("resources/s3_trigger/dummy.txt"); // Adjust the path as needed + zip.writeZip(outputFile); + console.log("Zip file written successfully."); +} catch (error) { + console.error("Error writing zip file:", error); + process.exit(1); +}