Skip to content

Commit

Permalink
feat(runner): provide custom flows execution
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jun 6, 2021
1 parent 1c82469 commit bf8be9c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Fortunately, there are several workarounds:
2. Fetch `yarn/npm audit --json` and patch lockfile inners (kudos to [G. Kosev](https://github.com/spion), [code reference](https://github.com/hfour/yarn-audit-fix-ng/blob/main/src/index.ts)). `yarn-audit-fix --flow=patch`

## Key features
* `convert` and `patch` flows for `yarn.lock` at your choice
* A couple of strategies to fix security issues
* Mac / Linux / Windows support
* CLI / JS API
* TS and flow typings
Expand Down Expand Up @@ -112,6 +112,31 @@ await run({
})
```

Build and run custom flows.
```ts
import {
clear,
exit,
patchLockfile,
yarnInstall
} from 'yarn-audit-fix'

export const flow: TFlow = {
main: [
[
'Patching yarn.lock with audit data...',
patchLockfile,
(...args) => {console.log('Smth interesting:', ...args)},
yarnInstall,
],
['Done'],
],
fallback: [['Failure!', exit]],
}

await run({}, flow)
```

## Troubleshooting
### yarn-audit-fix version x.x.x is out of date
```
Expand Down
6 changes: 3 additions & 3 deletions src/main/ts/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import chalk from 'chalk'
import { join } from 'path'

import { getFlow } from './flows'
import { TCallback, TContext, TFlags, TStage } from './ifaces'
import {TCallback, TContext, TFlags, TFlow, TStage} from './ifaces'
import { getTemp, normalizeFlags, readJson } from './util'

/**
Expand Down Expand Up @@ -39,10 +39,10 @@ export const exec = (stages: TStage[], ctx: TContext): void => {
/**
* Public static void main.
*/
export const run = async (_flags: TFlags = {}): Promise<void> => {
export const run = async (_flags: TFlags = {}, _flow?: TFlow): Promise<void> => {
const flags = normalizeFlags(_flags)
const ctx = getContext(flags)
const flow = getFlow(flags.flow)
const flow = _flow || getFlow(flags.flow)

try {
exec(flow.main, ctx)
Expand Down
13 changes: 12 additions & 1 deletion src/test/ts/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { factory as iop } from 'inside-out-promise'
import { basename, join, resolve } from 'path'
import synp from 'synp'

import { createSymlinks, run, TContext } from '../../main/ts'
import {createSymlinks, run, TContext, TFlow} from '../../main/ts'
import * as lf from '../../main/ts/lockfile'
import { getNpm, getYarn } from '../../main/ts/util'

Expand Down Expand Up @@ -181,6 +181,17 @@ describe('yarn-audit-fix', () => {
expect(fs.emptyDirSync).toHaveBeenCalledWith(expect.stringMatching(temp))
}

it('executes custom flows', async () => {
const handler = jest.fn(noop)
const flow: TFlow = {
main: [['Test', handler]],
fallback: []
}
await run({}, flow)

expect(handler).toHaveBeenCalledTimes(1)
})

it('throws error on unsupported flow', async () =>
expect(run({ flow: 'unknown' })).rejects.toEqual(
new Error('Unsupported flow: unknown'),
Expand Down

0 comments on commit bf8be9c

Please sign in to comment.