diff --git a/package.json b/package.json index 2b9efae..90e74a2 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,8 @@ "bugs": "https://github.com/adriencaccia/serverless-analyze-bundle-plugin/issues", "dependencies": { "@babel/runtime": "^7.18.6", - "check-node-version": "^4.2.1" + "check-node-version": "^4.2.1", + "node-stream-zip": "^1.15.0" }, "devDependencies": { "@babel/cli": "^7.18.6", diff --git a/plugin/bundleVisualizer.ts b/plugin/bundleVisualizer.ts index 0754722..d03f95c 100644 --- a/plugin/bundleVisualizer.ts +++ b/plugin/bundleVisualizer.ts @@ -1,8 +1,11 @@ import check from 'check-node-version'; import { exec } from 'child_process'; import { readdirSync, statSync } from 'fs'; -import { join, parse } from 'path'; +import { tmpdir } from 'os'; +import { mkdir } from 'fs/promises'; +import { join, normalize, parse } from 'path'; import { FunctionDefinitionHandler } from 'serverless'; +import StreamZip from 'node-stream-zip'; import type { ServerlessAnalyzeBundlePlugin } from './serverlessAnalyzeBundle'; @@ -28,17 +31,18 @@ const getAllFiles = function (dirPath: string, arrayOfFilesInput: string[] = []) let arrayOfFiles = arrayOfFilesInput; files.forEach(function (file) { - if (statSync(dirPath + '/' + file).isDirectory()) { - arrayOfFiles = getAllFiles(dirPath + '/' + file, arrayOfFiles); + const filePath = join(dirPath, file); + if (statSync(filePath).isDirectory()) { + arrayOfFiles = getAllFiles(filePath, arrayOfFiles); } else { - arrayOfFiles.push(join(dirPath, '/', file)); + arrayOfFiles.push(filePath); } }); return arrayOfFiles; }; -const TMP_FOLDER = '/tmp/serverless-esbuild-bundle-analyzer'; +const TMP_FOLDER = join(tmpdir(), 'serverless-esbuild-bundle-analyzer'); async function bundleVisualizer(this: ServerlessAnalyzeBundlePlugin): Promise { const { analyze: functionName } = this.options; @@ -61,13 +65,14 @@ async function bundleVisualizer(this: ServerlessAnalyzeBundlePlugin): Promise fileName.includes(handlerPath) && fileName.endsWith('-meta.json'), )[0]; diff --git a/yarn.lock b/yarn.lock index b39e177..306ec9d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6713,6 +6713,13 @@ __metadata: languageName: node linkType: hard +"node-stream-zip@npm:^1.15.0": + version: 1.15.0 + resolution: "node-stream-zip@npm:1.15.0" + checksum: 0b73ffbb09490e479c8f47038d7cba803e6242618fbc1b71c26782009d388742ed6fb5ce6e9d31f528b410249e7eb1c6e7534e9d3792a0cafd99813ac5a35107 + languageName: node + linkType: hard + "nopt@npm:^5.0.0": version: 5.0.0 resolution: "nopt@npm:5.0.0" @@ -7878,6 +7885,7 @@ __metadata: eslint-plugin-prettier: ^4.2.1 husky: ^8.0.1 lint-staged: ^12.5.0 + node-stream-zip: ^1.15.0 prettier: ^2.7.1 serverless: ^3.21.0 standard-version: ^9.5.0