Comparing version 5.2.0 to 6.0.0
import * as typeFest from 'type-fest'; | ||
import normalize = require('normalize-package-data'); | ||
import * as normalize from 'normalize-package-data'; | ||
declare namespace readPkg { | ||
interface Options { | ||
/** | ||
[Normalize](https://github.com/npm/normalize-package-data#what-normalization-currently-entails) the package data. | ||
export interface Options { | ||
/** | ||
Current working directory. | ||
@default true | ||
*/ | ||
readonly normalize?: boolean; | ||
@default process.cwd() | ||
*/ | ||
readonly cwd?: string; | ||
/** | ||
Current working directory. | ||
/** | ||
[Normalize](https://github.com/npm/normalize-package-data#what-normalization-currently-entails) the package data. | ||
@default process.cwd() | ||
*/ | ||
readonly cwd?: string; | ||
} | ||
@default true | ||
*/ | ||
readonly normalize?: boolean; | ||
} | ||
interface NormalizeOptions extends Options { | ||
readonly normalize?: true; | ||
} | ||
type NormalizedPackageJson = PackageJson & normalize.Package; | ||
type PackageJson = typeFest.PackageJson; | ||
export interface NormalizeOptions extends Options { | ||
readonly normalize?: true; | ||
} | ||
declare const readPkg: { | ||
/** | ||
@returns The parsed JSON. | ||
export type NormalizedPackageJson = PackageJson & normalize.Package; | ||
export type PackageJson = typeFest.PackageJson; | ||
@example | ||
``` | ||
import readPkg = require('read-pkg'); | ||
/** | ||
@returns The parsed JSON. | ||
(async () => { | ||
console.log(await readPkg()); | ||
//=> {name: 'read-pkg', …} | ||
@example | ||
``` | ||
import {readPackageAsync} from 'read-pkg'; | ||
console.log(await readPkg({cwd: 'some-other-directory'}); | ||
//=> {name: 'unicorn', …} | ||
})(); | ||
``` | ||
*/ | ||
(options?: readPkg.NormalizeOptions): Promise<readPkg.NormalizedPackageJson>; | ||
(options: readPkg.Options): Promise<readPkg.PackageJson>; | ||
console.log(await readPackageAsync()); | ||
//=> {name: 'read-pkg', …} | ||
/** | ||
@returns The parsed JSON. | ||
console.log(await readPackageAsync({cwd: 'some-other-directory'}); | ||
//=> {name: 'unicorn', …} | ||
``` | ||
*/ | ||
export function readPackageAsync(options?: NormalizeOptions): Promise<NormalizedPackageJson>; | ||
export function readPackageAsync(options: Options): Promise<PackageJson>; | ||
@example | ||
``` | ||
import readPkg = require('read-pkg'); | ||
/** | ||
@returns The parsed JSON. | ||
console.log(readPkg.sync()); | ||
//=> {name: 'read-pkg', …} | ||
@example | ||
``` | ||
import {readPackageSync} from 'read-pkg'; | ||
console.log(readPkg.sync({cwd: 'some-other-directory'}); | ||
//=> {name: 'unicorn', …} | ||
``` | ||
*/ | ||
sync(options?: readPkg.NormalizeOptions): readPkg.NormalizedPackageJson; | ||
sync(options: readPkg.Options): readPkg.PackageJson; | ||
}; | ||
console.log(readPackageSync()); | ||
//=> {name: 'read-pkg', …} | ||
export = readPkg; | ||
console.log(readPackageSync({cwd: 'some-other-directory'}); | ||
//=> {name: 'unicorn', …} | ||
``` | ||
*/ | ||
export function readPackageSync(options?: NormalizeOptions): NormalizedPackageJson; | ||
export function readPackageSync(options: Options): PackageJson; |
45
index.js
@@ -1,41 +0,26 @@ | ||
'use strict'; | ||
const {promisify} = require('util'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const parseJson = require('parse-json'); | ||
import fs, {promises as fsAsync} from 'fs'; | ||
import path from 'path'; | ||
import parseJson from 'parse-json'; | ||
import normalizePackageData from 'normalize-package-data'; | ||
const readFileAsync = promisify(fs.readFile); | ||
export async function readPackageAsync({cwd = process.cwd(), normalize = true} = {}) { | ||
const filePath = path.resolve(cwd, 'package.json'); | ||
const json = parseJson(await fsAsync.readFile(filePath, 'utf8')); | ||
module.exports = async options => { | ||
options = { | ||
cwd: process.cwd(), | ||
normalize: true, | ||
...options | ||
}; | ||
const filePath = path.resolve(options.cwd, 'package.json'); | ||
const json = parseJson(await readFileAsync(filePath, 'utf8')); | ||
if (options.normalize) { | ||
require('normalize-package-data')(json); | ||
if (normalize) { | ||
normalizePackageData(json); | ||
} | ||
return json; | ||
}; | ||
} | ||
module.exports.sync = options => { | ||
options = { | ||
cwd: process.cwd(), | ||
normalize: true, | ||
...options | ||
}; | ||
const filePath = path.resolve(options.cwd, 'package.json'); | ||
export function readPackageSync({cwd = process.cwd(), normalize = true} = {}) { | ||
const filePath = path.resolve(cwd, 'package.json'); | ||
const json = parseJson(fs.readFileSync(filePath, 'utf8')); | ||
if (options.normalize) { | ||
require('normalize-package-data')(json); | ||
if (normalize) { | ||
normalizePackageData(json); | ||
} | ||
return json; | ||
}; | ||
} |
{ | ||
"name": "read-pkg", | ||
"version": "5.2.0", | ||
"version": "6.0.0", | ||
"description": "Read a package.json file", | ||
"license": "MIT", | ||
"repository": "sindresorhus/read-pkg", | ||
"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": ">=8" | ||
"node": ">=12" | ||
}, | ||
@@ -35,10 +38,10 @@ "scripts": { | ||
"@types/normalize-package-data": "^2.4.0", | ||
"normalize-package-data": "^2.5.0", | ||
"parse-json": "^5.0.0", | ||
"type-fest": "^0.6.0" | ||
"normalize-package-data": "^3.0.2", | ||
"parse-json": "^5.2.0", | ||
"type-fest": "^1.0.1" | ||
}, | ||
"devDependencies": { | ||
"ava": "^2.2.0", | ||
"tsd": "^0.7.2", | ||
"xo": "^0.24.0" | ||
"ava": "^3.15.0", | ||
"tsd": "^0.14.0", | ||
"xo": "^0.38.2" | ||
}, | ||
@@ -45,0 +48,0 @@ "xo": { |
@@ -1,13 +0,10 @@ | ||
# read-pkg [![Build Status](https://travis-ci.org/sindresorhus/read-pkg.svg?branch=master)](https://travis-ci.org/sindresorhus/read-pkg) | ||
# read-pkg | ||
> Read a package.json file | ||
## Why | ||
- [Gracefully handles filesystem issues](https://github.com/isaacs/node-graceful-fs) | ||
- [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 | ||
@@ -19,25 +16,21 @@ | ||
## Usage | ||
```js | ||
const readPkg = require('read-pkg'); | ||
import {readPackageAsync} from 'read-pkg'; | ||
(async () => { | ||
console.log(await readPkg()); | ||
//=> {name: 'read-pkg', …} | ||
console.log(await readPkg()); | ||
//=> {name: 'read-pkg', …} | ||
console.log(await readPkg({cwd: 'some-other-directory'})); | ||
//=> {name: 'unicorn', …} | ||
})(); | ||
console.log(await readPkg({cwd: 'some-other-directory'})); | ||
//=> {name: 'unicorn', …} | ||
``` | ||
## API | ||
### readPkg(options?) | ||
### readPackageAsync(options?) | ||
Returns a `Promise<object>` with the parsed JSON. | ||
### readPkg.sync(options?) | ||
### readPackageSync(options?) | ||
@@ -52,3 +45,3 @@ Returns the parsed JSON. | ||
Type: `string`<br> | ||
Type: `string`\ | ||
Default: `process.cwd()` | ||
@@ -60,3 +53,3 @@ | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `true` | ||
@@ -66,3 +59,2 @@ | ||
## Related | ||
@@ -74,3 +66,2 @@ | ||
--- | ||
@@ -77,0 +68,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Yes
5770
65
73
+ Addedhosted-git-info@4.1.0(transitive)
+ Addedlru-cache@6.0.0(transitive)
+ Addednormalize-package-data@3.0.3(transitive)
+ Addedsemver@7.6.3(transitive)
+ Addedtype-fest@1.4.0(transitive)
+ Addedyallist@4.0.0(transitive)
- Removedhosted-git-info@2.8.9(transitive)
- Removednormalize-package-data@2.5.0(transitive)
- Removedpath-parse@1.0.7(transitive)
- Removedresolve@1.22.8(transitive)
- Removedsemver@5.7.2(transitive)
- Removedsupports-preserve-symlinks-flag@1.0.0(transitive)
- Removedtype-fest@0.6.0(transitive)
Updatedparse-json@^5.2.0
Updatedtype-fest@^1.0.1