From e6fbe7f1ce52cbfb924543ef0e9801775efc38cc Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 28 Mar 2021 14:18:12 +0700 Subject: [PATCH] Require Node.js 12 and move to ESM --- .github/workflows/main.yml | 2 - index.d.ts | 134 +++++++++++++++++-------------------- index.js | 21 +++--- index.test-d.ts | 46 ++++++------- license | 2 +- package.json | 18 ++--- readme.md | 33 +++++---- test.js | 10 +-- 8 files changed, 125 insertions(+), 141 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 18531b3..41fe626 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,8 +12,6 @@ jobs: node-version: - 14 - 12 - - 10 - - 8 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 diff --git a/index.d.ts b/index.d.ts index acb4c8c..43886dc 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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; +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; - @default process.cwd() - */ - cwd?: string; - } & Except; +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; - 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; +@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; +export function readPackageUpAsync(options: Options): Promise; - @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; diff --git a/index.js b/index.js index bf8ae3c..e8181da 100644 --- a/index.js +++ b/index.js @@ -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 }; -}; +} diff --git a/index.test-d.ts b/index.test-d.ts index 4416ecd..8de8347 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,36 +1,36 @@ import {expectType, expectError} from 'tsd'; -import readPkgUp = require('.'); +import {readPackageUpAsync, readPackageUpSync, ReadResult, NormalizedReadResult} from './index.js'; -expectType>(readPkgUp()); -expectType>( - readPkgUp({cwd: '.'}) +expectType>(readPackageUpAsync()); +expectType>( + readPackageUpAsync({cwd: '.'}) ); -expectType>( - readPkgUp({normalize: true}) +expectType>( + readPackageUpAsync({normalize: true}) ); -expectType>( - readPkgUp({cwd: '.', normalize: true}) +expectType>( + readPackageUpAsync({cwd: '.', normalize: true}) ); -expectType>( - readPkgUp({normalize: false}) +expectType>( + readPackageUpAsync({normalize: false}) ); -expectError>( - readPkgUp({normalize: false}) +expectError>( + readPackageUpAsync({normalize: false}) ); -expectType(readPkgUp.sync()); -expectType( - readPkgUp.sync({cwd: '.'}) +expectType(readPackageUpSync()); +expectType( + readPackageUpSync({cwd: '.'}) ); -expectType( - readPkgUp.sync({normalize: true}) +expectType( + readPackageUpSync({normalize: true}) ); -expectType( - readPkgUp.sync({cwd: '.', normalize: true}) +expectType( + readPackageUpSync({cwd: '.', normalize: true}) ); -expectType( - readPkgUp.sync({normalize: false}) +expectType( + readPackageUpSync({normalize: false}) ); -expectError( - readPkgUp.sync({normalize: false}) +expectError( + readPackageUpSync({normalize: false}) ); diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (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: diff --git a/package.json b/package.json index 09a6d49..d9e207c 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" } } diff --git a/readme.md b/readme.md index 9a5f060..8df9c22 100644 --- a/readme.md +++ b/readme.md @@ -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) @@ -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` or `Promise` if no `package.json` was found. -### readPkgUp.sync(options?) +### readPackageUpSync(options?) Returns the result object or `undefined` if no `package.json` was found. diff --git a/test.js b/test.js index d4230ba..9531903 100644 --- a/test.js +++ b/test.js @@ -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); });