Skip to content

Commit

Permalink
Rename the readPackageAsync export to readPackage
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 14, 2021
1 parent e6fdae4 commit cd1bccf
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 31 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
10 changes: 5 additions & 5 deletions index.d.ts
Expand Up @@ -29,17 +29,17 @@ export type PackageJson = typeFest.PackageJson;
@example
```
import {readPackageAsync} from 'read-pkg';
import {readPackage} from 'read-pkg';
console.log(await readPackageAsync());
console.log(await readPackage());
//=> {name: 'read-pkg', …}
console.log(await readPackageAsync({cwd: 'some-other-directory'});
console.log(await readPackage({cwd: 'some-other-directory'});
//=> {name: 'unicorn', …}
```
*/
export function readPackageAsync(options?: NormalizeOptions): Promise<NormalizedPackageJson>;
export function readPackageAsync(options: Options): Promise<PackageJson>;
export function readPackage(options?: NormalizeOptions): Promise<NormalizedPackageJson>;
export function readPackage(options: Options): Promise<PackageJson>;

/**
@returns The parsed JSON.
Expand Down
9 changes: 5 additions & 4 deletions index.js
@@ -1,11 +1,12 @@
import fs, {promises as fsAsync} from 'fs';
import path from 'path';
import process from 'node:process';
import fs, {promises as fsPromises} from 'node:fs';
import path from 'node:path';
import parseJson from 'parse-json';
import normalizePackageData from 'normalize-package-data';

export async function readPackageAsync({cwd = process.cwd(), normalize = true} = {}) {
export async function readPackage({cwd = process.cwd(), normalize = true} = {}) {
const filePath = path.resolve(cwd, 'package.json');
const json = parseJson(await fsAsync.readFile(filePath, 'utf8'));
const json = parseJson(await fsPromises.readFile(filePath, 'utf8'));

if (normalize) {
normalizePackageData(json);
Expand Down
13 changes: 6 additions & 7 deletions index.test-d.ts
@@ -1,17 +1,16 @@
import {expectType, expectError, expectAssignable} from 'tsd';
import {readPackageAsync, readPackageSync, Options, NormalizedPackageJson, PackageJson} from './index.js';
import {readPackage, readPackageSync, NormalizedPackageJson, PackageJson} from './index.js';

const options: Options = {};
expectError<NormalizedPackageJson>({});
expectAssignable<PackageJson>({});

expectType<Promise<NormalizedPackageJson>>(readPackageAsync());
expectType<Promise<NormalizedPackageJson>>(readPackageAsync({normalize: true}));
expectType<Promise<PackageJson>>(readPackageAsync({normalize: false}));
expectType<Promise<NormalizedPackageJson>>(readPackage());
expectType<Promise<NormalizedPackageJson>>(readPackage({normalize: true}));
expectType<Promise<PackageJson>>(readPackage({normalize: false}));
expectError<Promise<NormalizedPackageJson>>(
readPackageAsync({normalize: false})
readPackage({normalize: false}),
);
expectType<Promise<NormalizedPackageJson>>(readPackageAsync({cwd: '.'}));
expectType<Promise<NormalizedPackageJson>>(readPackage({cwd: '.'}));

expectType<NormalizedPackageJson>(readPackageSync());
expectType<NormalizedPackageJson>(readPackageSync({normalize: true}));
Expand Down
10 changes: 5 additions & 5 deletions package.json
Expand Up @@ -13,7 +13,7 @@
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=12"
"node": ">=12.20"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -34,15 +34,15 @@
"normalize"
],
"dependencies": {
"@types/normalize-package-data": "^2.4.0",
"@types/normalize-package-data": "^2.4.1",
"normalize-package-data": "^3.0.2",
"parse-json": "^5.2.0",
"type-fest": "^1.0.1"
"type-fest": "^2.0.0"
},
"devDependencies": {
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
"tsd": "^0.17.0",
"xo": "^0.44.0"
},
"xo": {
"ignores": [
Expand Down
8 changes: 4 additions & 4 deletions readme.md
Expand Up @@ -16,18 +16,18 @@ $ npm install read-pkg
## Usage

```js
import {readPackageAsync} from 'read-pkg';
import {readPackage} from 'read-pkg';

console.log(await readPackageAsync());
console.log(await readPackage());
//=> {name: 'read-pkg', …}

console.log(await readPackageAsync({cwd: 'some-other-directory'}));
console.log(await readPackage({cwd: 'some-other-directory'}));
//=> {name: 'unicorn', …}
```

## API

### readPackageAsync(options?)
### readPackage(options?)

Returns a `Promise<object>` with the parsed JSON.

Expand Down
6 changes: 3 additions & 3 deletions test/test.js
@@ -1,20 +1,20 @@
import {fileURLToPath} from 'url';
import path from 'path';
import test from 'ava';
import {readPackageAsync, readPackageSync} from '../index.js';
import {readPackage, readPackageSync} from '../index.js';

const dirname = path.dirname(fileURLToPath(import.meta.url));
process.chdir(dirname);
const rootCwd = path.join(dirname, '..');

test('async', async t => {
const package_ = await readPackageAsync();
const package_ = await readPackage();
t.is(package_.name, 'unicorn');
t.truthy(package_._id);
});

test('async - cwd option', async t => {
const package_ = await readPackageAsync({cwd: rootCwd});
const package_ = await readPackage({cwd: rootCwd});
t.is(package_.name, 'read-pkg');
});

Expand Down

0 comments on commit cd1bccf

Please sign in to comment.