Skip to content

Commit

Permalink
perf: tweak up npm audit invoker
Browse files Browse the repository at this point in the history
BREAKING CHANGE: --package-lock-only is disabled by default
  • Loading branch information
antongolub committed Aug 22, 2020
1 parent c457a18 commit 197ca96
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 202 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
"jest": "jest -w 1 --config=jest.config.json",
"lint:fix": "tslint -p tsconfig.json -c tslint.json --fix && tslint -c tslint.json src/test/js/*.js --fix",
"test:unit": "yarn jest",
"test:integration": "node ./target/es5/cli.js",
"test:integration": "node ./target/es5/cli.js --package-lock-only",
"test:integration-build": "yarn build:es5 && yarn test:integration",
"test": "yarn lint && yarn test:unit && yarn test:integration",
"clean": "rimraf target typings flow-typed buildcache",
"build": "yarn clean && yarn build:es5 && yarn build:es6 && yarn build:ts && yarn build:libdef && yarn docs && yarn uglify",
"build": "yarn clean && yarn build:es5 && yarn build:es6 && yarn build:ts && yarn build:libdef && yarn docs",
"build:es5": "mkdir -p target/es5 && tsc -p tsconfig.es5.json",
"build:es6": "mkdir -p target/es6 && tsc -p tsconfig.es6.json",
"build:ts": "cp -r src/main/ts/ target/ts/",
Expand Down Expand Up @@ -58,7 +58,7 @@
"packages/*"
],
"dependencies": {
"@antongolub/synp": "^2.0.11",
"@antongolub/synp": "^2.0.12",
"@types/bash-glob": "^2.0.0",
"@types/find-cache-dir": "^3.2.0",
"@types/fs-extra": "^9.0.1",
Expand All @@ -68,7 +68,7 @@
"find-cache-dir": "^3.3.1",
"fs-extra": "^9.0.1",
"minimist": "^1.2.5",
"npm": "7.0.0-beta.4",
"npm": "7.0.0-beta.5",
"tslib": "^2.0.1"
},
"devDependencies": {
Expand Down
37 changes: 0 additions & 37 deletions src/main/ts/audit.ts

This file was deleted.

9 changes: 4 additions & 5 deletions src/main/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import synp from '@antongolub/synp'
import {join} from 'path'
import findCacheDir from 'find-cache-dir'
import chalk from 'chalk'
import {audit} from './audit'
import {invoke, formatFlags, getSymlinkType, getWorkspaces} from './util'

type TContext = { cwd: string, temp: string, flags: Record<string, any> }
Expand Down Expand Up @@ -58,13 +57,13 @@ const yarnLockToPkgLock: TCallback = ({temp}) => {
* @param {TContext} cxt
* @return {void}
*/
const npmAuditFix: TCallback = async({temp, flags}) => {
const npmAuditFix: TCallback = ({temp, flags}) => {
const auditArgs = [
'audit',
'fix',
...formatFlags(flags, 'package-lock-only', 'verbose', 'loglevel', 'only', 'force', 'audit-level', 'silent'),
]

await audit(auditArgs, temp, flags.silent)
invoke('node', ['node_modules/npm/bin/npm-cli.js', ...auditArgs, `--prefix=${temp}`], temp, flags.silent)
}

/**
Expand Down Expand Up @@ -134,6 +133,6 @@ export const run = async(flags: Record<string, any> = {}) => {
for (const [description, ...steps] of stages) {
!flags.silent && console.log(chalk.bold(description))

for (const step of steps) await step(ctx)
for (const step of steps) step(ctx)
}
}
72 changes: 24 additions & 48 deletions src/test/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import cp from 'child_process'
import {join} from 'path'
import fs from 'fs-extra'
import synp from '@antongolub/synp'
import npm from 'npm'
import findCacheDir from 'find-cache-dir'
import {factory as iop} from 'inside-out-promise'
import {run} from '../../main/ts'
Expand Down Expand Up @@ -31,18 +30,9 @@ describe('yarn-audit-fix', () => {
// @ts-ignore
synp.yarnToNpm.mockImplementation(() => '{}')
// @ts-ignore
cp.spawnSync.mockImplementation(() => ({status: 0, stdout: 'foobar'}))

synp.npmToYarn.mockImplementation(() => '{}')
// @ts-ignore
npm.load.mockImplementation((cfg, cb) => {
cb()
})
npm.commands = {
// @ts-ignore
audit: jest.fn((args: any, cb: any) => {
cb()
}),
}
cp.spawnSync.mockImplementation(() => ({status: 0, stdout: 'foobar'}))
})
afterEach(jest.clearAllMocks)
afterAll(jest.resetAllMocks)
Expand All @@ -64,10 +54,14 @@ describe('yarn-audit-fix', () => {
expect(fs.removeSync).toHaveBeenCalledWith(join(temp, 'yarn.lock'))

// Applying npm audit fix...
expect(npm.load).toHaveBeenCalledWith({prefix: temp}, expect.any(Function))

// @ts-ignore
expect(npm.commands.audit).toHaveBeenCalledWith(['fix', '--verbose', '--package-lock-only'], expect.any(Function))
expect(cp.spawnSync).toHaveBeenCalledWith('node', [
'node_modules/npm/bin/npm-cli.js',
'audit',
'fix',
'--verbose',
'--package-lock-only',
`--prefix=${temp}`,
], {cwd: temp, stdio})

// Updating yarn.lock from package-lock.json...
expect(fs.copyFileSync).toHaveBeenCalledWith(join(temp, 'yarn.lock'), 'yarn.lock')
Expand All @@ -81,51 +75,33 @@ describe('yarn-audit-fix', () => {
checkFlow()
})

describe('handles exceptions of', () => {
it('invoke', async() => {
let reason = {error: new Error('foobar')} as any
// @ts-ignore
cp.spawnSync.mockImplementation(() => reason)
await expect(run({silent: true})).rejects.toBe(reason)

reason = {status: 1}
// @ts-ignore
cp.spawnSync.mockImplementation(() => reason)
await expect(run({silent: true})).rejects.toBe(reason)

reason = new TypeError('foo')
// @ts-ignore
cp.spawnSync.mockImplementation(() => {
throw reason
})
await expect(run()).rejects.toBe(reason)
})
})
it('handles exceptions', async() => {
let reason = {error: new Error('foobar')} as any
// @ts-ignore
cp.spawnSync.mockImplementation(() => reason)
await expect(run({silent: true})).rejects.toBe(reason)

it('npm api', async() => {
const reason = new Error()
npm.commands = {
// @ts-ignore
audit: jest.fn((args: any, cb: any) => {
cb(reason)
}),
}
reason = {status: 1}
// @ts-ignore
cp.spawnSync.mockImplementation(() => reason)
await expect(run({silent: true})).rejects.toBe(reason)

reason = new TypeError('foo')
// @ts-ignore
npm.load.mockImplementation((cfg, cb) => {
cb(reason)
cp.spawnSync.mockImplementation(() => {
throw reason
})
await expect(run({silent: true})).rejects.toBe(reason)
await expect(run()).rejects.toBe(reason)
})
})

describe('cli', () => {
it('invokes cmd queue with proper args', () => {
jest.isolateModules(() => {
process.argv.push('--verbose --package-lock-only')
process.argv.push('--verbose', '--package-lock-only')
require('../../main/ts/cli')
})
checkFlow()
})

describe('on error', () => {
Expand Down
Loading

0 comments on commit 197ca96

Please sign in to comment.