From 2088095c2b76600a78e48f3651237baa81b70379 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 28 Mar 2021 13:58:10 +0700 Subject: [PATCH] Require Node.js 12 and move to ESM Fixes #22 Fixes #21 --- .github/workflows/main.yml | 2 - index.d.ts | 94 +++++++++++++++++--------------------- index.js | 45 ++++++------------ index.test-d.ts | 32 ++++++------- license | 2 +- package.json | 19 ++++---- readme.md | 27 ++++------- test/package.json | 3 +- test/test.js | 18 ++++---- 9 files changed, 106 insertions(+), 136 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 a937926..75e093c 100644 --- a/index.d.ts +++ b/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; - (options: readPkg.Options): Promise; +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; +export function readPackageAsync(options: Options): Promise; - @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; diff --git a/index.js b/index.js index c1243a8..d58bc28 100644 --- a/index.js +++ b/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; -}; +} diff --git a/index.test-d.ts b/index.test-d.ts index fea7cf2..8171697 100644 --- a/index.test-d.ts +++ b/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({}); -expectType({}); +const options: Options = {}; +expectError({}); +expectAssignable({}); -expectType>(readPkg()); -expectType>(readPkg({normalize: true})); -expectType>(readPkg({normalize: false})); -expectError>( - readPkg({normalize: false}) +expectType>(readPackageAsync()); +expectType>(readPackageAsync({normalize: true})); +expectType>(readPackageAsync({normalize: false})); +expectError>( + readPackageAsync({normalize: false}) ); -expectType>(readPkg({cwd: '.'})); +expectType>(readPackageAsync({cwd: '.'})); -expectType(readPkg.sync()); -expectType(readPkg.sync({normalize: true})); -expectType(readPkg.sync({normalize: false})); -expectError(readPkg.sync({normalize: false})); -expectType(readPkg.sync({cwd: '.'})); +expectType(readPackageSync()); +expectType(readPackageSync({normalize: true})); +expectType(readPackageSync({normalize: false})); +expectError(readPackageSync({normalize: false})); +expectType(readPackageSync({cwd: '.'})); 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 05dc532..6f8cbff 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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": [ diff --git a/readme.md b/readme.md index 85c8a45..eda793b 100644 --- a/readme.md +++ b/readme.md @@ -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` with the parsed JSON. -### readPkg.sync(options?) +### readPackageSync(options?) Returns the parsed JSON. @@ -48,26 +41,24 @@ Type: `object` ##### cwd -Type: `string`
+Type: `string`\ Default: `process.cwd()` Current working directory. ##### normalize -Type: `boolean`
+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 - ---
diff --git a/test/package.json b/test/package.json index d580b22..d1753f3 100644 --- a/test/package.json +++ b/test/package.json @@ -1,4 +1,5 @@ { "name": "unicorn", - "version": "1.0.0" + "version": "1.0.0", + "type": "module" } diff --git a/test/test.js b/test/test.js index ce06595..1c2007b 100644 --- a/test/test.js +++ b/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'); });