Skip to content

Commit

Permalink
feat: update packages and remove json stream
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Csutorás authored and csutorasr committed May 3, 2022
1 parent f53a08d commit 5df853a
Show file tree
Hide file tree
Showing 11 changed files with 10,037 additions and 13,147 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
},
extends: [
'plugin:@typescript-eslint/recommended',
'prettier/@typescript-eslint',
'prettier',
'plugin:prettier/recommended',
],
rules: {},
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,7 @@ jobs:
run: npm test
env:
CI: true
- name: Run build
run: npm run build
- name: Run integration tests
run: npx @team-supercharge/audit-ci-wrapper
run: node ./lib/index.js
File renamed without changes.
23,039 changes: 9,964 additions & 13,075 deletions package-lock.json

Large diffs are not rendered by default.

48 changes: 25 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"release": "npx standard-version",
"prerelease": "npx standard-version --prerelease alpha"
},
"engines": {
"npm": "7.x || 8.x"
},
"publishConfig": {
"access": "public"
},
Expand All @@ -32,24 +35,24 @@
"author": "",
"license": "ISC",
"devDependencies": {
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@types/argparse": "^2.0.0",
"@types/event-stream": "^3.3.34",
"@types/jest": "^26.0.13",
"@types/semver": "^7.3.4",
"@typescript-eslint/eslint-plugin": "^4.1.0",
"@typescript-eslint/parser": "^4.1.0",
"eslint": "^7.9.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-prettier": "^3.1.4",
"husky": "^4.3.0",
"jest": "^26.4.2",
"lint-staged": "^10.3.0",
"prettier": "^2.1.1",
"standard-version": "^9.0.0",
"ts-jest": "^26.3.0",
"typescript": "^4.0.2"
"@commitlint/cli": "16.2.4",
"@commitlint/config-conventional": "16.2.4",
"@types/argparse": "2.0.10",
"@types/event-stream": "4.0.0",
"@types/jest": "27.5.0",
"@types/semver": "7.3.9",
"@typescript-eslint/eslint-plugin": "5.22.0",
"@typescript-eslint/parser": "5.22.0",
"eslint": "8.14.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-prettier": "4.0.0",
"husky": "4.3.0",
"jest": "27.5.1",
"lint-staged": "12.4.1",
"prettier": "2.6.2",
"standard-version": "9.3.2",
"ts-jest": "27.1.4",
"typescript": "4.6.4"
},
"husky": {
"hooks": {
Expand All @@ -63,10 +66,9 @@
]
},
"dependencies": {
"JSONStream": "^1.3.5",
"argparse": "^2.0.1",
"chalk": "^4.1.0",
"event-stream": "^4.0.1",
"semver": "^7.3.2"
"@types/node": "17.0.31",
"argparse": "2.0.1",
"chalk": "4.1.0",
"semver": "7.3.7"
}
}
2 changes: 1 addition & 1 deletion src/arguments.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ArgumentParser } from 'argparse';
import { readFileSync } from 'fs';
import { readFileSync } from 'node:fs';

const version = JSON.parse(
readFileSync(`${__dirname}/../package.json`).toString()
Expand Down
1 change: 0 additions & 1 deletion src/audit/JSONStream.ts

This file was deleted.

28 changes: 13 additions & 15 deletions src/audit/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import * as childProcess from 'child_process';
import * as JSONStream from 'JSONStream';
import * as es from 'event-stream';
import { AuditOptions } from '../config';
import { IAudit } from '../types/npm-audit';
import { ArgumentResult } from '../arguments';
Expand All @@ -11,6 +9,7 @@ export const runAudit = (
): Promise<IAudit> => {
return new Promise((resolve, reject) => {
let stderr = '';
let stdout = '';

const command = /^win/.test(process.platform) ? 'npm.cmd' : 'npm';
const command_args = ['audit', '--json'];
Expand All @@ -31,14 +30,10 @@ export const runAudit = (
detached: false,
});

let auditData: IAudit | null = null;

// Use stream processing of JSON data to be able to handle large data
audit_proc.stdout.pipe(JSONStream.parse()).pipe(
es.mapSync((data: IAudit) => {
auditData = data;
})
);
audit_proc.stdout.on('data', (data) => {
const holder = stdout;
stdout = holder.concat(data);
});

audit_proc.stderr.on('data', (data) => {
const holder = stderr;
Expand All @@ -50,15 +45,18 @@ export const runAudit = (
console.error(`npm audit exited with code ${exit_code}.`);
}
if (
(stderr.length > 0 &&
stderr !==
'Debugger attached.\nWaiting for the debugger to disconnect...\n') ||
auditData === null ||
!!auditData.error
stderr.length > 0 &&
stderr !==
'Debugger attached.\nWaiting for the debugger to disconnect...\n'
) {
reject(stderr);
return;
}
const auditData: IAudit = JSON.parse(stdout);
if (!!auditData.error) {
reject(auditData.error);
return;
}
resolve(auditData);
});
});
Expand Down
55 changes: 27 additions & 28 deletions src/config/read-config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
import * as fs from 'fs';
import * as fs from 'node:fs';
import { AuditOptions, defaultOptions } from './default-config';
import { readConfig } from './read-config';

let options: AuditOptions;

jest.mock('node:fs');

describe('Read config file', () => {
let spy: jest.SpyInstance;
let options: AuditOptions;
beforeEach(() => {
options = defaultOptions;
spy = jest
.spyOn(fs, 'readFile')
.mockImplementation(
(
path: string | number | Buffer | URL,
callback: (err: NodeJS.ErrnoException | null, data: Buffer) => void
) => {
if (path === 'aconfig.json') {
return callback(null, Buffer.from(JSON.stringify(options)));
}
return callback(
{
message: `ENOENT: no such file or directory, open '${path}'`,
name: 'Error',
code: 'ENOENT',
errno: -2,
syscall: 'open',
path: path.toString(),
},
Buffer.from('')
);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
spy = fs.readFile.mockImplementation(
(
path: string | number | Buffer | URL,
callback: (err: NodeJS.ErrnoException | null, data: Buffer) => void
) => {
if (path === 'aconfig.json') {
return callback(null, Buffer.from(JSON.stringify(options)));
}
);
return callback(
{
message: `ENOENT: no such file or directory, open '${path}'`,
name: 'Error',
code: 'ENOENT',
errno: -2,
syscall: 'open',
path: path.toString(),
},
Buffer.from('')
);
}
);
});

it('should return default if no config is given', async () => {
Expand Down Expand Up @@ -68,8 +71,4 @@ describe('Read config file', () => {
});
expect(config.ignoreDevelopmentDependencies).toBe(true);
});

afterEach(() => {
spy.mockClear();
});
});
2 changes: 1 addition & 1 deletion src/config/read-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fs from 'fs';
import * as fs from 'node:fs';
import { promisify } from 'util';
import { AuditOptions, defaultOptions } from './default-config';
import { ArgumentResult } from '../arguments';
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
"declaration": true,
"outDir": "./lib",
"strict": true,
"resolveJsonModule": true,
"sourceMap": true
},
"include": ["src", "src/audit/JSONStream.ts"],
Expand Down

0 comments on commit 5df853a

Please sign in to comment.