Skip to content

Commit

Permalink
refactor!: rename path to cwd in options
Browse files Browse the repository at this point in the history
For consistency with other packages in the ecosystem, like read-pkg and @npmcli/map-workspaces
  • Loading branch information
voxpelli committed Mar 18, 2024
1 parent abfaf2a commit 9c45b17
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type VersionRangeResult = VersionRangeItem & {
```javascript
import { checkVersionRange, getInstalledData } from 'installed-check-core';

const { installed, pkg } = await getInstalledData(path);
const { installed, pkg } = await getInstalledData(cwd);

const result = await checkVersionRange(
pkg,
Expand Down Expand Up @@ -143,12 +143,12 @@ Is a simple wrapper around [`read-pkg`](https://github.com/sindresorhus/read-pkg
#### Syntax

```ts
getInstalledData(path = '.') => Promise<InstalledData>
getInstalledData(cwd = '.') => Promise<InstalledData>
```

#### Arguments

* `path` – specifies the path to the package to be checked, with its `package.json` expected to be there and its installed `node_modules` as well.
* `cwd` – specifies the path of the package to be checked, with its `package.json` expected to exist in that path and its installed `node_modules` as well.

#### Types

Expand Down Expand Up @@ -202,7 +202,7 @@ type InstalledCheckResult = { errors: string[], warnings: string[] }
#### Options
* `path = '.'` – specifies the path to the package to be checked, with its `package.json` expected to be there and its installed `node_modules` as well.
* `cwd = '.'` – specifies the path to the package to be checked, with its `package.json` expected to be there and its installed `node_modules` as well.
* `ignores = string[]` – names of modules to exclude from checks. Supports [`picomatch`](https://www.npmjs.com/package/picomatch) globbing syntax, eg. `@types/*`. (Not supported by `version` checks)
* `noDev = false` – exclude `devDependencies` from checks. `devDependencies` that are also in `peerDependencies` will not be ignored. (Not supported by `version` checks)
* `strict = false` – converts most warnings into failures
Expand All @@ -213,7 +213,7 @@ type InstalledCheckResult = { errors: string[], warnings: string[] }
import { installedCheck } from 'installed-check-core';
const { errors, warnings } = await installedCheck(['engine', 'version'], {
path: 'path/to/module',
cwd: 'path/to/module',
ignore: ['foo'],
noDev: true,
});
Expand All @@ -234,7 +234,7 @@ performInstalledCheck(checks, pkg, installed, options) => Promise<InstalledCheck
* `checks`: Type `InstalledChecks[]` – same as for [`installedCheck()`](#installedcheck)
* `pkg`: Type `PackageJsonLike` – the content of the `package.json` file to check, see [`getInstalledData()`](#getinstalleddata)
* `installed`: Type `InstalledDependencies` – the installed dependencies to use when checking, see [`getInstalledData()`](#getinstalleddata)
* `options`: Type `InstalledCheckOptions` – same as for [`installedCheck()`](#installedcheck), but without the `path` option
* `options`: Type `InstalledCheckOptions` – same as for [`installedCheck()`](#installedcheck), but without the `cwd` option
## Used by
Expand Down
8 changes: 4 additions & 4 deletions lib/get-installed-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ import { readPackage } from 'read-pkg';

/**
* @throws {Error}
* @param {string} [path] Specifies the path to the package to be checked, with its `package.json` expected to be there and its installed `node_modules` as well
* @param {string} [cwd] Specifies the path to the package to be checked, with its `package.json` expected to be there and its installed `node_modules` as well
* @returns {Promise<InstalledData>}
*/
export async function getInstalledData (path = '.') {
export async function getInstalledData (cwd = '.') {
const [
pkg,
installed,
] = await Promise.all([
readPackage({ cwd: path }).catch(/** @param {Error} err */ err => {
readPackage({ cwd }).catch(/** @param {Error} err */ err => {
throw new ErrorWithCause('Failed to read package.json', { cause: err });
}),
listInstalled(path).catch(/** @param {Error} err */ err => {
listInstalled(cwd).catch(/** @param {Error} err */ err => {
throw new ErrorWithCause('Failed to list installed modules', { cause: err });
}),
]);
Expand Down
6 changes: 3 additions & 3 deletions lib/installed-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { performInstalledCheck } from './perform-installed-check.js';

/**
* @param {import('./perform-installed-check.js').InstalledChecks[]} checks
* @param {import('./perform-installed-check.js').InstalledCheckOptions & { path?: string }} [options]
* @param {import('./perform-installed-check.js').InstalledCheckOptions & { cwd?: string }} [options]
* @returns {Promise<import('./perform-installed-check.js').InstalledCheckResult>}
*/
export async function installedCheck (checks, options) {
const { path = '.', ...checkOptions } = options || {};
const { cwd = '.', ...checkOptions } = options || {};

const { installed, pkg } = await getInstalledData(path);
const { installed, pkg } = await getInstalledData(cwd);

return performInstalledCheck(checks, pkg, installed, checkOptions);
}
2 changes: 1 addition & 1 deletion test/get-installed-data.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('getInstalledData', () => {
.and.have.nested.property('pkg.name', 'installed-check-core');
});

it('should return data from path when specified', async () => {
it('should return data from cwd when specified', async () => {
const result = await getInstalledData(join(import.meta.url, 'fixtures/valid'));

result.should.be.an('object').with.keys('installed', 'pkg');
Expand Down
20 changes: 10 additions & 10 deletions test/installed-check.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ describe('installedCheck()', () => {

it('should error on missing package.json file', async () => {
await installedCheck(['engine', 'version'], {
path: join(import.meta.url, 'fixtures/missing-package-json'),
cwd: join(import.meta.url, 'fixtures/missing-package-json'),
})
.should.be.rejectedWith(/Failed to read package\.json/);
});

it('should error on inability to list installed modules', async () => {
await installedCheck(['engine', 'version'], {
path: join(import.meta.url, 'fixtures/missing-node-modules'),
cwd: join(import.meta.url, 'fixtures/missing-node-modules'),
})
.should.be.rejectedWith(/Failed to list installed modules/);
});
Expand All @@ -43,7 +43,7 @@ describe('installedCheck()', () => {
describe('functionality', () => {
it('should return an empty result on valid setup', async () => {
await installedCheck(['engine', 'version'], {
path: join(import.meta.url, 'fixtures/valid'),
cwd: join(import.meta.url, 'fixtures/valid'),
})
.should.eventually.deep.equal({
errors: [],
Expand All @@ -53,7 +53,7 @@ describe('installedCheck()', () => {

it('should return an empty result on an aliased setup', async () => {
await installedCheck(['engine', 'version'], {
path: join(import.meta.url, 'fixtures/aliased'),
cwd: join(import.meta.url, 'fixtures/aliased'),
})
.should.eventually.deep.equal({
errors: [],
Expand All @@ -63,7 +63,7 @@ describe('installedCheck()', () => {

it('should return errors and warnings on invalid setup', async () => {
await installedCheck(['engine', 'version'], {
path: join(import.meta.url, 'fixtures/invalid'),
cwd: join(import.meta.url, 'fixtures/invalid'),
})
.should.eventually.deep.equal({
'errors': [
Expand Down Expand Up @@ -95,7 +95,7 @@ describe('installedCheck()', () => {

it('should check engine even when no target engines are set', async () => {
await installedCheck(['engine'], {
path: join(import.meta.url, 'fixtures/missing-engines'),
cwd: join(import.meta.url, 'fixtures/missing-engines'),
})
.should.eventually.deep.equal({
'errors': [
Expand All @@ -110,7 +110,7 @@ describe('installedCheck()', () => {

it('should not suggest an engine configuration when engines are incompatible', async () => {
await installedCheck(['engine'], {
path: join(import.meta.url, 'fixtures/incompatible-engines'),
cwd: join(import.meta.url, 'fixtures/incompatible-engines'),
})
.should.eventually.deep.equal({
'errors': [
Expand All @@ -123,7 +123,7 @@ describe('installedCheck()', () => {

it('should handle engine ranges', async () => {
await installedCheck(['engine'], {
path: join(import.meta.url, 'fixtures/engine-ranges'),
cwd: join(import.meta.url, 'fixtures/engine-ranges'),
})
.should.eventually.deep.equal({
'errors': [],
Expand All @@ -133,7 +133,7 @@ describe('installedCheck()', () => {

it('should handle ignores', async () => {
await installedCheck(['engine'], {
path: join(import.meta.url, 'fixtures/invalid'),
cwd: join(import.meta.url, 'fixtures/invalid'),
ignore: ['invalid-alias*', 'invalid-dependency-definition'],
})
.should.eventually.deep.equal({
Expand All @@ -155,7 +155,7 @@ describe('installedCheck()', () => {

it('should check peer dependencies', async () => {
await installedCheck(['peer'], {
path: join(import.meta.url, 'fixtures/peer'),
cwd: join(import.meta.url, 'fixtures/peer'),
})
.should.eventually.deep.equal({
'errors': [
Expand Down

0 comments on commit 9c45b17

Please sign in to comment.