Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore improvements: add cwd opt, npm v8.0.0 #194

Merged
merged 6 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

Apply `npm audit fix` logic to `yarn.lock`

## Table of Contents
- [Digest](#digest)
- [Problem](#problem)
- [Solution](#solution)
Expand Down Expand Up @@ -96,12 +95,13 @@ success Already up-to-date.
|---|---|---|---|
|`--flow` | Define how `yarn.lock` is modified. `convert` — to compose `npm audit fix` with two-way lockfile conversion (legacy flow). `patch` — to directly inject audit json data | `patch`
|`--audit-level` | Include a vulnerability with a level as defined or higher. Supported values: low, moderate, high, critical | `low`
|`--cwd` | Current working dir | `process.cwd()`
|`--dry-run` | Get an idea of what audit fix will do
|`--force` | Have audit fix install semver-major updates to toplevel dependencies, not just semver-compatible ones | `false`
|`--help/-h`| Print help message |
|`--legacy-peer-deps` | Accept an incorrect (potentially broken) deps resolution | | ✔
|`--loglevel` | Set custom [log level](https://docs.npmjs.com/cli/v7/using-npm/config#loglevel) | | ✔
|`--npm-path` | Declare npm path: switch to system default version of **npm** instead of package's own. `system / local / <custom path>` | `local`
|`--npm-path` | Define npm path: switch to system **npm** version instead of default package's own. Or provide a custom path. `system / local / <custom path>` | `local`
|`--only` | Set package [update scope](https://docs.npmjs.com/cli/v7/using-npm/config#only): `dev`/`prod`
|`--package-lock-only` | Run audit fix without modifying `node_modules`. Highly recommended to **enable**. | `true` | ✔ |
|`--registry` | Custom registry url | | ✔ |
Expand All @@ -120,13 +120,19 @@ All mentioned above CLI options can be replaced with the corresponding env varia
Typedoc: [https://antongolub.github.io/yarn-audit-fix/modules/](https://antongolub.github.io/yarn-audit-fix/modules/)

```ts
import { run } from 'yarn-audit-fix'
import { run, runSync } from 'yarn-audit-fix'

// NOTE actually it's promisified run.sync
// NOTE actually it's promisified `run.sync`
await run({
flow: 'patch',
verbose: true
})

// `runSync` is an alias for `run.sync`
await runSync({
flow: 'patch',
verbose: true
})
```

Build and run custom flows.
Expand Down Expand Up @@ -297,7 +303,7 @@ invoke yarn --update-checksums
Not everything can be repaired, alack.

## Contributing
Feel free to open any issues: for bugs, feature requests or questions.
Feel free to open any issues: bugs, feature requests or other questions.
You're always welcome to suggest a PR. Just fork this repo, write some code, add some tests and push your changes.
Any feedback is appreciated.

Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,31 +77,31 @@
"commander": "^8.2.0",
"fs-extra": "^10.0.0",
"find-cache-dir": "^3.3.2",
"find-up": "^6.1.0",
"find-up": "^6.2.0",
"globby": "^12.0.2",
"lodash-es": "^4.17.21",
"npm": "7.24.2",
"pkg-dir": "^6.0.0",
"npm": "8.0.0",
"pkg-dir": "^6.0.1",
"semver": "^7.3.5",
"synp": "^1.9.7",
"tslib": "^2.3.1"
},
"devDependencies": {
"@jest/globals": "^27.2.4",
"@jest/globals": "^27.2.5",
"@qiwi/libdefkit": "^3.1.1",
"@qiwi/npm-run-all": "^4.1.7",
"@types/jest": "^27.0.2",
"@types/node": "^16.10.2",
"@types/node": "^16.10.3",
"cpy-cli": "^3.1.1",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-config-qiwi": "^1.13.6",
"jest": "^27.2.4",
"eslint-config-qiwi": "^1.13.7",
"jest": "^27.2.5",
"mkdirp": "^1.0.4",
"prettier": "^2.4.1",
"terser": "^5.9.0",
"ts-jest": "^27.0.5",
"tsc-esm-fix": "^2.7.2",
"tsc-esm-fix": "^2.7.3",
"typedoc": "^0.22.5",
"typescript": "4.4.3"
},
Expand Down
5 changes: 5 additions & 0 deletions src/main/ts/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const flags = new Command()
.choices(['low', 'moderate', 'high', 'critical'])
.default(env.YAF_AUDIT_LEVEL),
)
.option(
'--cwd [path]',
'CWD. Defaults to `process.cwd()`',
env.YAF_CWD,
)
.option(
'--dry-run [bool]',
'Get an idea of what audit fix will do',
Expand Down
2 changes: 1 addition & 1 deletion src/main/ts/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getTemp, normalizeFlags, readJson } from './util'
* Build running context.
*/
export const getContext = (flags: TFlags = {}): TContext => {
const cwd = process.cwd()
const cwd = flags.cwd || process.cwd()
const manifest = readJson(join(cwd, 'package.json'))
const temp = getTemp(cwd, flags.temp)
const ctx = {
Expand Down
26 changes: 24 additions & 2 deletions src/test/ts/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type { TContext, TFlow } from '../../main/ts/ifaces'

jest.mock('child_process')
jest.mock('fs-extra')
jest.mock('npm')
jest.mock('synp')

const cp = createRequire(import.meta.url)('child_process')
Expand All @@ -16,7 +15,7 @@ const fs = (await import('fs-extra')).default
const synp = (await import('synp')).default

const lf = (await import('../../main/ts/lockfile'))._internal
const { createSymlinks, run } = await import('../../main/ts')
const { createSymlinks, getContext, run, runSync } = await import('../../main/ts')
const { getNpm, getYarn } = await import('../../main/ts/util')

const __dirname = dirname(fileURLToPath(import.meta.url))
Expand Down Expand Up @@ -331,4 +330,27 @@ describe('yarn-audit-fix', () => {
})
})
})

describe('#getContext', () => {
it('parses flags, returns ctx entry', () => {
const cwd = '/foo/bar'
const bar = 'baz'
const ctx = getContext({
cwd,
bar
})

expect(ctx).toEqual(expect.objectContaining({
cwd,
flags: { cwd, bar },
manifest: { version: '1.0.0' }
}))
})
})

describe('aliases', () => {
it('runSync eq run.sync', () => {
expect(run.sync).toBe(runSync)
})
})
})
Loading