Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sindresorhus/read-pkg
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v6.0.0
Choose a base ref
...
head repository: sindresorhus/read-pkg
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v7.0.0
Choose a head ref
  • 3 commits
  • 7 files changed
  • 1 contributor

Commits on Apr 8, 2021

  1. Fix usage example

    Closes #23
    sindresorhus committed Apr 8, 2021
    Copy the full SHA
    e6fdae4 View commit details

Commits on Aug 14, 2021

  1. Copy the full SHA
    cd1bccf View commit details
  2. 7.0.0

    sindresorhus committed Aug 14, 2021
    Copy the full SHA
    64678da View commit details
Showing with 31 additions and 32 deletions.
  1. +2 −3 .github/workflows/main.yml
  2. +5 −5 index.d.ts
  3. +5 −4 index.js
  4. +6 −7 index.test-d.ts
  5. +6 −6 package.json
  6. +4 −4 readme.md
  7. +3 −3 test/test.js
5 changes: 2 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -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
10 changes: 5 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -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);
13 changes: 6 additions & 7 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -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}));
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "read-pkg",
"version": "6.0.0",
"version": "7.0.0",
"description": "Read a package.json file",
"license": "MIT",
"repository": "sindresorhus/read-pkg",
@@ -13,7 +13,7 @@
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=12"
"node": ">=12.20"
},
"scripts": {
"test": "xo && ava && tsd"
@@ -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": [
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
@@ -16,18 +16,18 @@ $ npm install read-pkg
## Usage

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

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

console.log(await readPkg({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.

6 changes: 3 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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');
});