Skip to content

Commit

Permalink
fix: use asynchronous processing
Browse files Browse the repository at this point in the history
  • Loading branch information
jzhangdev committed Apr 7, 2024
1 parent f3a4686 commit 5b6bed1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: use asynchronous processing",
"packageName": "@rightcapital/verdaccio-package-diff",
"email": "zhangjie9477@gmail.com",
"dependentChangeType": "patch"
}
47 changes: 30 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { spawnSync } from 'node:child_process';
import { spawn } from 'node:child_process';
import fs from 'node:fs/promises';
import path from 'node:path';
import type {
Expand Down Expand Up @@ -106,28 +106,41 @@ export default class VerdaccioMiddlewarePlugin
const controller = new AbortController();
const { signal } = controller;

try {
const npmDiff = spawnSync(
'npm',
[
'diff',
`--diff=${name as string}@${from as string}`,
`--diff=${name as string}@${to as string}`,
],
{ signal },
);
if (npmDiff.status !== 0) {
textTypeResponse(req, res, next)(npmDiff.stderr.toString());
const npmDiff = spawn(
'npm',
[
'diff',
`--diff=${name as string}@${from as string}`,
`--diff=${name as string}@${to as string}`,
],
{ signal },
);
let outputData = '';
let errorData = '';

npmDiff.stdout.on('data', (data: Buffer) => {
outputData += data.toString();
});

npmDiff.stderr.on('data', (data: Buffer) => {
errorData += data.toString();
});

npmDiff.on('close', (code) => {
if (code !== 0) {
textTypeResponse(req, res, next)(errorData);
} else {
textTypeResponse(req, res, next)(npmDiff.stdout.toString());
textTypeResponse(req, res, next)(outputData);
}
} catch (npmDiffUnexpectedError) {
});

npmDiff.on('error', (npmDiffUnexpectedError) => {
internalServerErrorResponse(
req,
res,
next,
)(npmDiffUnexpectedError as Error);
}
)(npmDiffUnexpectedError);
});
}
},
);
Expand Down

0 comments on commit 5b6bed1

Please sign in to comment.