Skip to content

Commit

Permalink
feat: add async handlers support
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Aug 3, 2020
1 parent efe3965 commit 71eab4e
Show file tree
Hide file tree
Showing 7 changed files with 380 additions and 381 deletions.
11 changes: 10 additions & 1 deletion src/main/ts/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@

import {run} from './index'

run()
let promise

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

await promise
})()

module.exports = promise
12 changes: 8 additions & 4 deletions src/main/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import fs from 'fs-extra'
import synp from 'synp'
import {invoke} from './invoke'
import {
invoke,
promisify,
asyncForEach,
} from './util'

type TContext = { cwd: string }

Expand Down Expand Up @@ -64,12 +68,12 @@ export const stages: TStage[] = [
],
]

export const run = () => {
export const run = async() => {
const cxt = {cwd: process.cwd()}

stages.forEach(([description, ...cbs]) => {
return asyncForEach(stages, async([description, ...cbs]) => {
console.log(description)

cbs.forEach(cb => cb(cxt))
return asyncForEach(cbs, async(cb) => promisify(cb)(cxt))
})
}
13 changes: 0 additions & 13 deletions src/main/ts/invoke.ts

This file was deleted.

21 changes: 21 additions & 0 deletions src/main/ts/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import cp from 'child_process'

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

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

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

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))
10 changes: 5 additions & 5 deletions src/test/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ describe('yarn-audit-fix', () => {
afterAll(jest.clearAllMocks)

describe('runner', () => {
it('invokes cmd queue with proper args', () => {
it('invokes cmd queue with proper args', async() => {
const expectedOpts = {cwd: process.cwd()}

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

// Generating package-lock.json from yarn.lock...
expect(cp.spawnSync).toHaveBeenCalledWith('yarn', [], expectedOpts)
Expand All @@ -44,16 +44,16 @@ describe('yarn-audit-fix', () => {
expect(fs.removeSync).toHaveBeenCalledWith('package-lock.json')
})

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

expect(run).toThrow('foobar')
await expect(run()).rejects.toThrow('foobar')

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

expect(run).toThrowError()
await expect(run()).rejects.toThrowError()
})
})
})
4 changes: 2 additions & 2 deletions src/test/ts/intergration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
describe('cli', () => {
it('applies cli to self repo', () => {
require('../../main/ts/cli')
it('applies cli to self repo', async() => {
await require('../../main/ts/cli')
})
})
Loading

0 comments on commit 71eab4e

Please sign in to comment.