Skip to content

Commit

Permalink
fix: exit with non-zero if anything fails (#11)
Browse files Browse the repository at this point in the history
closes #10
  • Loading branch information
qwelias authored Aug 6, 2020
1 parent 20eb4ec commit 3e7eb93
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 52 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
"lint": "tslint -p tsconfig.json -c tslint.json && tslint -c tslint.json src/test/js/*.js",
"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": "yarn lint && yarn jest",
"test:unit": "yarn jest",
"test:integration": "yarn build && node ./target/es5/cli.js",
"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:es5": "mkdir -p target/es5 && tsc -p tsconfig.es5.json",
Expand Down
14 changes: 4 additions & 10 deletions src/main/ts/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@

import {run} from './index'

let promise

// tslint:disable-next-line:no-floating-promises
(async() => {
promise = run()

await promise
})()

module.exports = promise
run().catch(reason => {
console.error(reason)
process.exit(reason.status || 1)
})
12 changes: 5 additions & 7 deletions src/main/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {join} from 'path'
import findCacheDir from 'find-cache-dir'
import {
invoke,
promisify,
asyncForEach,
} from './util'

type TContext = { cwd: string, temp: string }
Expand All @@ -14,8 +12,8 @@ type TCallback = (cxt: TContext) => void

type TStage = [string, ...TCallback[]]

// https://github.com/antongolub/yarn-audit-fix/issues/2
const fixWorkspaces: TCallback = ({temp}) => {
// https://github.com/antongolub/yarn-audit-fix/issues/2
const pkgJsonData = JSON.parse(fs.readFileSync(join(temp, 'package.json'), 'utf-8').trim())
delete pkgJsonData.workspaces

Expand Down Expand Up @@ -77,14 +75,14 @@ export const stages: TStage[] = [
]

export const run = async() => {
const cxt = {
const ctx = {
cwd: process.cwd(),
temp: findCacheDir({name: 'yarn-audit-fix', create: true}) + '',
}

return asyncForEach(stages, async([description, ...cbs]) => {
for (const [description, ...steps] of stages) {
console.log(description)

return asyncForEach(cbs, async(cb) => promisify(cb)(cxt))
})
for (const step of steps) step(ctx)
}
}
16 changes: 3 additions & 13 deletions src/main/ts/util.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import cp from 'child_process'

export const invoke = (cmd: string, args: string[], cwd: string) => {
const result = cp.spawnSync(cmd, args, {cwd})
const result = cp.spawnSync(cmd, args, {cwd, stdio: ['inherit', 'inherit', 'inherit']})

console.log('invoke', cmd, ...args)

if (result.error || result.status !== 0) {
throw result.error || result.stderr.toString('utf-8')
if (result.error || result.status) {
throw result
}

console.log(result.stdout.toString('utf-8'))
}

export const asyncForEach = async<T extends any[]>(array: T, callback: (a: T[number], index: number, arr: T) => any) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array)
}
}

export const promisify = <T extends (...args: any[]) => any>(fn: T) => (...args: Parameters<T>): Promise<ReturnType<T>> => Promise.resolve(fn(...args))
22 changes: 12 additions & 10 deletions src/test/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ jest.mock('child_process')
jest.mock('fs-extra')
jest.mock('synp')

const stdio = ['inherit', 'inherit', 'inherit']

describe('yarn-audit-fix', () => {

beforeAll(() => {
Expand All @@ -34,7 +36,7 @@ describe('yarn-audit-fix', () => {
const temp = findCacheDir({name: 'yarn-audit-fix', create: true}) + ''
const cwd = process.cwd()

await require('../../main/ts/cli')
await run()

// Preparing...
expect(fs.emptyDirSync).toHaveBeenCalledWith(temp)
Expand All @@ -47,25 +49,25 @@ describe('yarn-audit-fix', () => {
expect(fs.removeSync).toHaveBeenCalledWith(join(temp, 'yarn.lock'))

// Applying npm audit fix...
expect(cp.spawnSync).toHaveBeenCalledWith('npm', ['audit', 'fix', '--package-lock-only'], {cwd: temp})
expect(cp.spawnSync).toHaveBeenCalledWith('npm', ['audit', 'fix', '--package-lock-only'], {cwd: temp, stdio})

// Updating yarn.lock from package-lock.json...
expect(cp.spawnSync).toHaveBeenCalledWith('yarn', ['import'], {cwd: temp})
expect(cp.spawnSync).toHaveBeenCalledWith('yarn', ['import'], {cwd: temp, stdio})
expect(fs.copyFileSync).toHaveBeenCalledWith(join(temp, 'yarn.lock'), 'yarn.lock')
expect(cp.spawnSync).toHaveBeenCalledWith('yarn', [], {cwd})
expect(cp.spawnSync).toHaveBeenCalledWith('yarn', [], {cwd, stdio})
expect(fs.emptyDirSync).toHaveBeenCalledWith(temp)
})

it('throws exception if occurs', async() => {
let reason = {error: new Error('foobar')} as any
// @ts-ignore
cp.spawnSync.mockImplementation(() => ({error: new Error('foobar')}))

await expect(run()).rejects.toThrow('foobar')
cp.spawnSync.mockImplementation(() => reason)
await expect(run()).rejects.toBe(reason)

reason = {status: 1}
// @ts-ignore
cp.spawnSync.mockImplementation(() => ({status: 1}))

await expect(run()).rejects.toThrowError()
cp.spawnSync.mockImplementation(() => reason)
await expect(run()).rejects.toBe(reason)
})
})
})
5 changes: 0 additions & 5 deletions src/test/ts/intergration.ts

This file was deleted.

12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3591,9 +3591,9 @@ is-descriptor@^1.0.0, is-descriptor@^1.0.2:
kind-of "^6.0.2"

is-docker@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/is-docker/-/is-docker-2.0.0.tgz#2cb0df0e75e2d064fe1864c37cdeacb7b2dcf25b"
integrity sha512-pJEdRugimx4fBMra5z2/5iRdZ63OhYV0vr0Dwm5+xtW4D1FvRkB8hamMIhnWfyJeDdyr/aa7BDyNbtG38VxgoQ==
version "2.1.1"
resolved "https://registry.yarnpkg.com/is-docker/-/is-docker-2.1.1.tgz#4125a88e44e450d384e09047ede71adc2d144156"
integrity sha512-ZOoqiXfEwtGknTiuDEy8pN2CfE3TxMHprvNer1mXiqwkOT77Rw3YVrUQ52EqAOU3QAWDQ+bQdx7HJzrv7LS2Hw==

is-extendable@^0.1.0, is-extendable@^0.1.1:
version "0.1.1"
Expand Down Expand Up @@ -7589,9 +7589,9 @@ [email protected], typescript@^3.4:
integrity sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw==

uglify-js@^3.1.4:
version "3.10.0"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.10.0.tgz#397a7e6e31ce820bfd1cb55b804ee140c587a9e7"
integrity sha512-Esj5HG5WAyrLIdYU74Z3JdG2PxdIusvj6IWHMtlyESxc7kcDz7zYlYjpnSokn1UbpV0d/QX9fan7gkCNd/9BQA==
version "3.10.1"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.10.1.tgz#dd14767eb7150de97f2573a5ff210db14fffe4ad"
integrity sha512-RjxApKkrPJB6kjJxQS3iZlf///REXWYxYJxO/MpmlQzVkDWVI3PSnCBWezMecmTU/TRkNxrl8bmsfFQCp+LO+Q==

[email protected]:
version "0.0.6"
Expand Down

0 comments on commit 3e7eb93

Please sign in to comment.