Skip to content

Commit

Permalink
Rename readPackageUpAsync to readPackageUp
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 13, 2021
1 parent c4f8985 commit 8f870da
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 47 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/main.yml
Expand Up @@ -10,11 +10,10 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
16 changes: 8 additions & 8 deletions index.d.ts
@@ -1,9 +1,9 @@
import {Except} from 'type-fest';
import {readPackageAsync, readPackageSync, Options as ReadPackageOptions, NormalizeOptions as ReadPackageNormalizeOptions, PackageJson, NormalizedPackageJson} from 'read-pkg';
import {readPackage, readPackageSync, Options as ReadPackageOptions, NormalizeOptions as ReadPackageNormalizeOptions, PackageJson, NormalizedPackageJson} from 'read-pkg';

export type Options = {
/**
Directory to start looking for a package.json file.
The directory to start looking for a package.json file.
@default process.cwd()
*/
Expand All @@ -12,7 +12,7 @@ export type Options = {

export type NormalizeOptions = {
/**
Directory to start looking for a package.json file.
The directory to start looking for a package.json file.
@default process.cwd()
*/
Expand All @@ -31,17 +31,17 @@ export interface NormalizedReadResult {

export {
PackageJson,
NormalizedPackageJson
NormalizedPackageJson,
};

/**
Read the closest `package.json` file.
@example
```
import {readPackageUpAsync} from 'read-pkg-up';
import {readPackageUp} from 'read-pkg-up';
console.log(await readPackageUpAsync());
console.log(await readPackageUp());
// {
// packageJson: {
// name: 'awesome-package',
Expand All @@ -52,8 +52,8 @@ console.log(await readPackageUpAsync());
// }
```
*/
export function readPackageUpAsync(options?: NormalizeOptions): Promise<NormalizedReadResult | undefined>;
export function readPackageUpAsync(options: Options): Promise<ReadResult | undefined>;
export function readPackageUp(options?: NormalizeOptions): Promise<NormalizedReadResult | undefined>;
export function readPackageUp(options: Options): Promise<ReadResult | undefined>;

/**
Synchronously read the closest `package.json` file.
Expand Down
16 changes: 8 additions & 8 deletions index.js
@@ -1,27 +1,27 @@
import path from 'path';
import findUp from 'find-up';
import {readPackageAsync, readPackageSync} from 'read-pkg';
import path from 'node:path';
import {findUp, findUpSync} from 'find-up';
import {readPackage, readPackageSync} from 'read-pkg';

export async function readPackageUpAsync(options) {
export async function readPackageUp(options) {
const filePath = await findUp('package.json', options);
if (!filePath) {
return;
}

return {
packageJson: await readPackageAsync({...options, cwd: path.dirname(filePath)}),
path: filePath
packageJson: await readPackage({...options, cwd: path.dirname(filePath)}),
path: filePath,
};
}

export function readPackageUpSync(options) {
const filePath = findUp.sync('package.json', options);
const filePath = findUpSync('package.json', options);
if (!filePath) {
return;
}

return {
packageJson: readPackageSync({...options, cwd: path.dirname(filePath)}),
path: filePath
path: filePath,
};
}
24 changes: 12 additions & 12 deletions index.test-d.ts
@@ -1,36 +1,36 @@
import {expectType, expectError} from 'tsd';
import {readPackageUpAsync, readPackageUpSync, ReadResult, NormalizedReadResult} from './index.js';
import {readPackageUp, readPackageUpSync, ReadResult, NormalizedReadResult} from './index.js';

expectType<Promise<NormalizedReadResult | undefined>>(readPackageUpAsync());
expectType<Promise<NormalizedReadResult | undefined>>(readPackageUp());
expectType<Promise<NormalizedReadResult | undefined>>(
readPackageUpAsync({cwd: '.'})
readPackageUp({cwd: '.'}),
);
expectType<Promise<NormalizedReadResult | undefined>>(
readPackageUpAsync({normalize: true})
readPackageUp({normalize: true}),
);
expectType<Promise<NormalizedReadResult | undefined>>(
readPackageUpAsync({cwd: '.', normalize: true})
readPackageUp({cwd: '.', normalize: true}),
);
expectType<Promise<ReadResult | undefined>>(
readPackageUpAsync({normalize: false})
readPackageUp({normalize: false}),
);
expectError<Promise<NormalizedReadResult | undefined>>(
readPackageUpAsync({normalize: false})
readPackageUp({normalize: false}),
);

expectType<NormalizedReadResult | undefined>(readPackageUpSync());
expectType<NormalizedReadResult | undefined>(
readPackageUpSync({cwd: '.'})
readPackageUpSync({cwd: '.'}),
);
expectType<NormalizedReadResult | undefined>(
readPackageUpSync({normalize: true})
readPackageUpSync({normalize: true}),
);
expectType<NormalizedReadResult | undefined>(
readPackageUpSync({cwd: '.', normalize: true})
readPackageUpSync({cwd: '.', normalize: true}),
);
expectType<ReadResult | undefined>(
readPackageUpSync({normalize: false})
readPackageUpSync({normalize: false}),
);
expectError<NormalizedReadResult | undefined>(
readPackageUpSync({normalize: false})
readPackageUpSync({normalize: false}),
);
12 changes: 6 additions & 6 deletions package.json
Expand Up @@ -13,7 +13,7 @@
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=12"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -49,13 +49,13 @@
"path"
],
"dependencies": {
"find-up": "^5.0.0",
"read-pkg": "^6.0.0",
"type-fest": "^1.0.1"
"find-up": "^6.2.0",
"read-pkg": "^7.0.0",
"type-fest": "^2.5.0"
},
"devDependencies": {
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
"tsd": "^0.18.0",
"xo": "^0.45.0"
}
}
12 changes: 6 additions & 6 deletions readme.md
Expand Up @@ -10,16 +10,16 @@

## Install

```
$ npm install read-pkg-up
```sh
npm install read-pkg-up
```

## Usage

```js
import {readPackageUpAsync} from 'read-pkg-up';
import {readPackageUp} from 'read-pkg-up';

console.log(await readPackageUpAsync());
console.log(await readPackageUp());
/*
{
packageJson: {
Expand All @@ -34,7 +34,7 @@ console.log(await readPackageUpAsync());

## API

### readPackageUpAsync(options?)
### readPackageUp(options?)

Returns a `Promise<object>` or `Promise<undefined>` if no `package.json` was found.

Expand All @@ -51,7 +51,7 @@ Type: `object`
Type: `string`\
Default: `process.cwd()`

Directory to start looking for a package.json file.
The directory to start looking for a package.json file.

##### normalize

Expand Down
8 changes: 4 additions & 4 deletions test.js
@@ -1,16 +1,16 @@
import path from 'path';
import path from 'node:path';
import test from 'ava';
import {readPackageUpAsync, readPackageUpSync} from './index.js';
import {readPackageUp, readPackageUpSync} from './index.js';

const cwd = 'fixture';
const packagePath = path.resolve('.', 'package.json');

test('async', async t => {
const result = await readPackageUpAsync({cwd});
const result = await readPackageUp({cwd});
t.is(result.packageJson.name, 'read-pkg-up');
t.is(result.path, packagePath);

t.is(await readPackageUpAsync({cwd: '/'}), undefined);
t.is(await readPackageUp({cwd: '/'}), undefined);
});

test('sync', t => {
Expand Down

0 comments on commit 8f870da

Please sign in to comment.