Skip to content

Commit

Permalink
fix: make plugin usable on Windows
Browse files Browse the repository at this point in the history
- replace usage of mkdir CLI with equivalent Node API
- replace usage of unzip CLI with node-stream-zip lib
- use os.tempdir() for getting a cross-platform temp directory
- replace path concatenation with join/normalize otherwise fileName.includes(handlerPath)
  never succeeds because of path separator mismatch

Closes #7
  • Loading branch information
coyoteecd committed Aug 18, 2022
1 parent a7362c5 commit ca36d42
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 14 additions & 9 deletions plugin/bundleVisualizer.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<void> {
const { analyze: functionName } = this.options;
Expand All @@ -61,13 +65,14 @@ async function bundleVisualizer(this: ServerlessAnalyzeBundlePlugin): Promise<vo

this.serverless.cli.log(`⏳ Analyzing function ${functionName}`, 'ServerlessAnalyzeBundlePlugin');

const TEMP_DIR_LOCATION = `${TMP_FOLDER}/${functionZipName}/${new Date().getTime()}`;
await pExec(`mkdir -p ${TEMP_DIR_LOCATION}`);
const TEMP_DIR_LOCATION = join(TMP_FOLDER, functionZipName, new Date().getTime().toString());
await mkdir(TEMP_DIR_LOCATION, { recursive: true });

await pExec(`unzip .serverless/${functionZipName} -d ${TEMP_DIR_LOCATION}`);
const functionZip = new StreamZip.async({ file: `.serverless/${functionZipName}` });
await functionZip.extract(null, TEMP_DIR_LOCATION);

const allFiles = getAllFiles(`${TEMP_DIR_LOCATION}`);
const handlerPath = (slsFunction as FunctionDefinitionHandler).handler.split('.')[0];
const handlerPath = normalize((slsFunction as FunctionDefinitionHandler).handler.split('.')[0]);
const metafileName = allFiles.filter(
fileName => fileName.includes(handlerPath) && fileName.endsWith('-meta.json'),
)[0];
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ca36d42

Please sign in to comment.