Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Mar 28, 2021
1 parent 021b1ec commit e6fbe7f
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 141 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
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
134 changes: 62 additions & 72 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,87 +1,77 @@
import {Except} from 'type-fest';
import readPkg = require('read-pkg');
import {readPackageAsync, readPackageSync, Options as ReadPackageOptions, NormalizeOptions as ReadPackageNormalizeOptions, PackageJson, NormalizedPackageJson} from 'read-pkg';

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

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

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

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

declare const readPkgUp: {
/**
Read the closest `package.json` file.
export {
PackageJson,
NormalizedPackageJson
};

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

@example
```
import readPkgUp = require('read-pkg-up');
/**
Synchronously read the closest `package.json` file.
console.log(readPkgUp.sync());
// {
// packageJson: {
// name: 'awesome-package',
// version: '1.0.0',
// …
// },
// path: '/Users/sindresorhus/dev/awesome-package/package.json'
// }
```
*/
sync(
options?: readPkgUp.NormalizeOptions
): readPkgUp.NormalizedReadResult | undefined;
sync(options: readPkgUp.Options): readPkgUp.ReadResult | undefined;
};
@example
```
import {readPackageUpSync} from 'read-pkg-up';
export = readPkgUp;
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;
21 changes: 9 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
'use strict';
const path = require('path');
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 = async options => {
export async function readPackageUpAsync(options) {
const filePath = await findUp('package.json', options);

if (!filePath) {
return;
}

return {
packageJson: await readPkg({...options, cwd: path.dirname(filePath)}),
packageJson: await readPackageAsync({...options, cwd: path.dirname(filePath)}),
path: filePath
};
};
}

module.exports.sync = options => {
export function readPackageUpSync(options) {
const filePath = findUp.sync('package.json', options);

if (!filePath) {
return;
}

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

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

expectType<readPkgUp.NormalizedReadResult | undefined>(readPkgUp.sync());
expectType<readPkgUp.NormalizedReadResult | undefined>(
readPkgUp.sync({cwd: '.'})
expectType<NormalizedReadResult | undefined>(readPackageUpSync());
expectType<NormalizedReadResult | undefined>(
readPackageUpSync({cwd: '.'})
);
expectType<readPkgUp.NormalizedReadResult | undefined>(
readPkgUp.sync({normalize: true})
expectType<NormalizedReadResult | undefined>(
readPackageUpSync({normalize: true})
);
expectType<readPkgUp.NormalizedReadResult | undefined>(
readPkgUp.sync({cwd: '.', normalize: true})
expectType<NormalizedReadResult | undefined>(
readPackageUpSync({cwd: '.', normalize: true})
);
expectType<readPkgUp.ReadResult | undefined>(
readPkgUp.sync({normalize: false})
expectType<ReadResult | undefined>(
readPackageUpSync({normalize: false})
);
expectError<readPkgUp.NormalizedReadResult | undefined>(
readPkgUp.sync({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:

Expand Down
18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
"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 Down Expand Up @@ -47,13 +49,13 @@
"path"
],
"dependencies": {
"find-up": "^4.1.0",
"read-pkg": "^5.2.0",
"type-fest": "^0.8.1"
"find-up": "^5.0.0",
"read-pkg": "^6.0.0",
"type-fest": "^1.0.1"
},
"devDependencies": {
"ava": "^2.4.0",
"tsd": "^0.9.0",
"xo": "^0.25.3"
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
}
}
33 changes: 15 additions & 18 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
## Why

- [Finds the closest package.json](https://github.com/sindresorhus/find-up)
- [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)

Expand All @@ -18,30 +17,28 @@ $ npm install read-pkg-up
## Usage

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

(async () => {
console.log(await readPkgUp());
/*
{
packageJson: {
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<object>` or `Promise<undefined>` if no `package.json` was found.

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

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

Expand Down
10 changes: 5 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import path from 'path';
import test from 'ava';
import readPackageUp from '.';
import {readPackageUpAsync, readPackageUpSync} from './index.js';

const cwd = 'fixture';
const packagePath = path.resolve('.', 'package.json');

test('async', async t => {
const result = await readPackageUp({cwd});
const result = await readPackageUpAsync({cwd});
t.is(result.packageJson.name, 'read-pkg-up');
t.is(result.path, packagePath);

t.is(await readPackageUp({cwd: '/'}), undefined);
t.is(await readPackageUpAsync({cwd: '/'}), undefined);
});

test('sync', t => {
const result = readPackageUp.sync({cwd});
const result = readPackageUpSync({cwd});
t.is(result.packageJson.name, 'read-pkg-up');
t.is(result.path, packagePath);

t.is(readPackageUp.sync({cwd: '/'}), undefined);
t.is(readPackageUpSync({cwd: '/'}), undefined);
});

0 comments on commit e6fbe7f

Please sign in to comment.