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-package-up
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: ecb07950755ab17f51d9dfda9b3048bb0b0c667e
Choose a base ref
...
head repository: sindresorhus/read-package-up
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: c4f8985c36dacac551e72580c8571d88feffe3c9
Choose a head ref
  • 17 commits
  • 11 files changed
  • 5 contributors

Commits on Nov 1, 2018

  1. Upgrade read-pkg to v4 (#7)

    dudeofawesome authored and sindresorhus committed Nov 1, 2018
    Copy the full SHA
    5bb0a79 View commit details

Commits on Mar 21, 2019

  1. Require Node.js 8

    sindresorhus committed Mar 21, 2019
    Copy the full SHA
    4412fe2 View commit details
  2. 5.0.0

    sindresorhus committed Mar 21, 2019
    Copy the full SHA
    d8d5100 View commit details

Commits on Apr 27, 2019

  1. Add Node.js 12 to testing (#9)

    coreyfarrell authored and sindresorhus committed Apr 27, 2019
    Copy the full SHA
    2c1d52f View commit details

Commits on May 12, 2019

  1. Rename pkg property to package, return undefined instead of emp…

    …ty object, add TypeScript definition (#10)
    BendingBender authored and sindresorhus committed May 12, 2019
    Copy the full SHA
    1c15896 View commit details
  2. 6.0.0

    sindresorhus committed May 12, 2019
    Copy the full SHA
    5267672 View commit details

Commits on May 28, 2019

  1. Create funding.yml

    sindresorhus authored May 28, 2019
    Copy the full SHA
    e473d2a View commit details

Commits on May 31, 2019

  1. Tidelift tasks

    sindresorhus committed May 31, 2019
    Copy the full SHA
    9ecaa3e View commit details

Commits on Sep 27, 2019

  1. Rename .package to .packageJson

    Since `package` is a reserved keyword, it meant that the result could previously not be easily destructured.
    
    Fixes #11
    sindresorhus committed Sep 27, 2019
    Copy the full SHA
    8140c73 View commit details
  2. Update dependencies

    sindresorhus committed Sep 27, 2019
    Copy the full SHA
    4406073 View commit details
  3. 7.0.0

    sindresorhus committed Sep 27, 2019
    Copy the full SHA
    83476ad View commit details

Commits on Oct 30, 2019

  1. Tidelift tasks

    sindresorhus committed Oct 30, 2019
    Copy the full SHA
    69983fc View commit details

Commits on Dec 6, 2019

  1. Readme tweaks

    It no longer strips the BOM, see: sindresorhus/read-pkg@0cd09a0
    
    Fixes #14
    sindresorhus committed Dec 6, 2019
    Copy the full SHA
    d96ce60 View commit details
  2. 7.0.1

    sindresorhus committed Dec 6, 2019
    Copy the full SHA
    c92040f View commit details

Commits on Jan 2, 2021

  1. Copy the full SHA
    021b1ec View commit details

Commits on Mar 28, 2021

  1. Copy the full SHA
    e6fbe7f View commit details
  2. 8.0.0

    sindresorhus committed Mar 28, 2021
    Copy the full SHA
    c4f8985 View commit details
Showing with 215 additions and 78 deletions.
  1. +4 −0 .github/funding.yml
  2. +3 −0 .github/security.md
  3. +21 −0 .github/workflows/main.yml
  4. +0 −5 .travis.yml
  5. +77 −0 index.d.ts
  6. +20 −19 index.js
  7. +36 −0 index.test-d.ts
  8. +1 −1 license
  9. +15 −11 package.json
  10. +26 −34 readme.md
  11. +12 −8 test.js
4 changes: 4 additions & 0 deletions .github/funding.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github: sindresorhus
open_collective: sindresorhus
tidelift: npm/read-pkg-up
custom: https://sindresorhus.com/donate
3 changes: 3 additions & 0 deletions .github/security.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Security Policy

To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
21 changes: 21 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 14
- 12
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

77 changes: 77 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import {Except} from 'type-fest';
import {readPackageAsync, readPackageSync, Options as ReadPackageOptions, NormalizeOptions as ReadPackageNormalizeOptions, PackageJson, NormalizedPackageJson} from 'read-pkg';

export type Options = {
/**
Directory to start looking for a package.json file.
@default process.cwd()
*/
cwd?: string;
} & Except<ReadPackageOptions, 'cwd'>;

export type NormalizeOptions = {
/**
Directory to start looking for a package.json file.
@default process.cwd()
*/
cwd?: string;
} & Except<ReadPackageNormalizeOptions, 'cwd'>;

export interface ReadResult {
packageJson: PackageJson;
path: string;
}

export interface NormalizedReadResult {
packageJson: NormalizedPackageJson;
path: string;
}

export {
PackageJson,
NormalizedPackageJson
};

/**
Read the closest `package.json` file.
@example
```
import {readPackageUpAsync} from 'read-pkg-up';
console.log(await readPackageUpAsync());
// {
// packageJson: {
// name: 'awesome-package',
// version: '1.0.0',
// …
// },
// path: '/Users/sindresorhus/dev/awesome-package/package.json'
// }
```
*/
export function readPackageUpAsync(options?: NormalizeOptions): Promise<NormalizedReadResult | undefined>;
export function readPackageUpAsync(options: Options): Promise<ReadResult | undefined>;

/**
Synchronously read the closest `package.json` file.
@example
```
import {readPackageUpSync} from 'read-pkg-up';
console.log(readPackageUpSync());
// {
// packageJson: {
// name: 'awesome-package',
// version: '1.0.0',
// …
// },
// path: '/Users/sindresorhus/dev/awesome-package/package.json'
// }
```
*/
export function readPackageUpSync(options?: NormalizeOptions): NormalizedReadResult | undefined;
export function readPackageUpSync(options: Options): ReadResult | undefined;
39 changes: 20 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
'use strict';
const findUp = require('find-up');
const readPkg = require('read-pkg');
import path from 'path';
import findUp from 'find-up';
import {readPackageAsync, readPackageSync} from 'read-pkg';

module.exports = options => {
return findUp('package.json', options).then(fp => {
if (!fp) {
return {};
}

return readPkg(fp, options).then(pkg => ({pkg, path: fp}));
});
};
export async function readPackageUpAsync(options) {
const filePath = await findUp('package.json', options);
if (!filePath) {
return;
}

module.exports.sync = options => {
const fp = findUp.sync('package.json', options);
return {
packageJson: await readPackageAsync({...options, cwd: path.dirname(filePath)}),
path: filePath
};
}

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

return {
pkg: readPkg.sync(fp, options),
path: fp
packageJson: readPackageSync({...options, cwd: path.dirname(filePath)}),
path: filePath
};
};
}
36 changes: 36 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {expectType, expectError} from 'tsd';
import {readPackageUpAsync, readPackageUpSync, ReadResult, NormalizedReadResult} from './index.js';

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

expectType<NormalizedReadResult | undefined>(readPackageUpSync());
expectType<NormalizedReadResult | undefined>(
readPackageUpSync({cwd: '.'})
);
expectType<NormalizedReadResult | undefined>(
readPackageUpSync({normalize: true})
);
expectType<NormalizedReadResult | undefined>(
readPackageUpSync({cwd: '.', normalize: true})
);
expectType<ReadResult | undefined>(
readPackageUpSync({normalize: false})
);
expectError<NormalizedReadResult | undefined>(
readPackageUpSync({normalize: false})
);
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

26 changes: 15 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
{
"name": "read-pkg-up",
"version": "4.0.0",
"version": "8.0.0",
"description": "Read the closest package.json file",
"license": "MIT",
"repository": "sindresorhus/read-pkg-up",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=6"
"node": ">=12"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"json",
@@ -26,7 +30,6 @@
"fs",
"graceful",
"load",
"pkg",
"package",
"find",
"up",
@@ -41,17 +44,18 @@
"parents",
"folder",
"directory",
"dir",
"walk",
"walking",
"path"
],
"dependencies": {
"find-up": "^3.0.0",
"read-pkg": "^3.0.0"
"find-up": "^5.0.0",
"read-pkg": "^6.0.0",
"type-fest": "^1.0.1"
},
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
}
}
60 changes: 26 additions & 34 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,82 +1,74 @@
# read-pkg-up [![Build Status](https://travis-ci.org/sindresorhus/read-pkg-up.svg?branch=master)](https://travis-ci.org/sindresorhus/read-pkg-up)
# read-pkg-up

> Read the closest package.json file

## Why

- [Finds the closest package.json](https://github.com/sindresorhus/find-up)
- [Gracefully handles filesystem issues](https://github.com/isaacs/node-graceful-fs)
- [Strips UTF-8 BOM](https://github.com/sindresorhus/strip-bom)
- [Throws more helpful JSON errors](https://github.com/sindresorhus/parse-json)
- [Normalizes the data](https://github.com/npm/normalize-package-data#what-normalization-currently-entails)


## Install

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


## Usage

```js
const readPkgUp = require('read-pkg-up');

(async () => {
console.log(await readPkgUp());
/*
{
pkg: {
name: 'awesome-package',
version: '1.0.0',
},
path: '/Users/sindresorhus/dev/awesome-package/package.json'
}
*/
})();
import {readPackageUpAsync} from 'read-pkg-up';

console.log(await readPackageUpAsync());
/*
{
packageJson: {
name: 'awesome-package',
version: '1.0.0',
},
path: '/Users/sindresorhus/dev/awesome-package/package.json'
}
*/
```


## API

### readPkgUp([options])
### readPackageUpAsync(options?)

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

### readPkgUp.sync([options])
### readPackageUpSync(options?)

Returns the result object.
Returns the result object or `undefined` if no `package.json` was found.

#### options

Type: `Object`
Type: `object`

##### cwd

Type: `string`<br>
Type: `string`\
Default: `process.cwd()`

Directory to start looking for a package.json file.

##### normalize

Type: `boolean`<br>
Type: `boolean`\
Default: `true`

[Normalize](https://github.com/npm/normalize-package-data#what-normalization-currently-entails) the package data.

## read-pkg-up for enterprise

Available as part of the Tidelift Subscription.

The maintainers of read-pkg-up and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-read-pkg-up?utm_source=npm-read-pkg-up&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)

## Related

- [read-pkg](https://github.com/sindresorhus/read-pkg) - Read a package.json file
- [pkg-up](https://github.com/sindresorhus/pkg-up) - Find the closest package.json file
- [find-up](https://github.com/sindresorhus/find-up) - Find a file by walking up parent directories
- [pkg-conf](https://github.com/sindresorhus/pkg-conf) - Get namespaced config from the closest package.json


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
Loading