Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
Fixes #22
Fixes #21
  • Loading branch information
sindresorhus committed Mar 28, 2021
1 parent 4c0cd9a commit 2088095
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 136 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/main.yml
Expand Up @@ -12,8 +12,6 @@ jobs:
node-version:
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down
94 changes: 43 additions & 51 deletions index.d.ts
@@ -1,67 +1,59 @@
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.
@default true
*/
readonly normalize?: boolean;
export interface Options {
/**
Current working directory.
/**
Current working directory.
@default process.cwd()
*/
readonly cwd?: string;

@default process.cwd()
*/
readonly cwd?: string;
}
/**
[Normalize](https://github.com/npm/normalize-package-data#what-normalization-currently-entails) the package data.
interface NormalizeOptions extends Options {
readonly normalize?: true;
}
@default true
*/
readonly normalize?: boolean;
}

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 changes: 15 additions & 30 deletions index.js
@@ -1,41 +1,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;
};
}
32 changes: 16 additions & 16 deletions index.test-d.ts
@@ -1,20 +1,20 @@
import {expectType, expectError} from 'tsd';
import readPkg = require('.');
import {expectType, expectError, expectAssignable} from 'tsd';
import {readPackageAsync, readPackageSync, Options, NormalizedPackageJson, PackageJson} from './index.js';

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

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

expectType<readPkg.NormalizedPackageJson>(readPkg.sync());
expectType<readPkg.NormalizedPackageJson>(readPkg.sync({normalize: true}));
expectType<readPkg.PackageJson>(readPkg.sync({normalize: false}));
expectError<readPkg.NormalizedPackageJson>(readPkg.sync({normalize: false}));
expectType<readPkg.NormalizedPackageJson>(readPkg.sync({cwd: '.'}));
expectType<NormalizedPackageJson>(readPackageSync());
expectType<NormalizedPackageJson>(readPackageSync({normalize: true}));
expectType<PackageJson>(readPackageSync({normalize: false}));
expectError<NormalizedPackageJson>(readPackageSync({normalize: false}));
expectType<NormalizedPackageJson>(readPackageSync({cwd: '.'}));
2 changes: 1 addition & 1 deletion license
@@ -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:

Expand Down
19 changes: 11 additions & 8 deletions package.json
Expand Up @@ -4,13 +4,16 @@
"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"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -32,14 +35,14 @@
],
"dependencies": {
"@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"
},
"xo": {
"ignores": [
Expand Down
27 changes: 9 additions & 18 deletions readme.md
Expand Up @@ -2,43 +2,36 @@

> 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

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


## 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?)

Returns the parsed JSON.

Expand All @@ -48,26 +41,24 @@ Type: `object`

##### cwd

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

Current working directory.

##### normalize

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

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


## Related

- [read-pkg-up](https://github.com/sindresorhus/read-pkg-up) - Read the closest package.json file
- [write-pkg](https://github.com/sindresorhus/write-pkg) - Write a `package.json` file
- [load-json-file](https://github.com/sindresorhus/load-json-file) - Read and parse a JSON file


---

<div align="center">
Expand Down
3 changes: 2 additions & 1 deletion test/package.json
@@ -1,4 +1,5 @@
{
"name": "unicorn",
"version": "1.0.0"
"version": "1.0.0",
"type": "module"
}
18 changes: 9 additions & 9 deletions test/test.js
@@ -1,30 +1,30 @@
'use strict';
import {fileURLToPath} from 'url';
import path from 'path';
import test from 'ava';
import readPackage from '..';
import {readPackageAsync, readPackageSync} from '../index.js';

process.chdir(__dirname);

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

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

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

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

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

0 comments on commit 2088095

Please sign in to comment.