diff --git a/index.d.ts b/index.d.ts index 0181e17..d9ab992 100644 --- a/index.d.ts +++ b/index.d.ts @@ -7,7 +7,7 @@ export interface Options { @default process.cwd() */ - readonly cwd?: string; + readonly cwd?: URL | string; /** [Normalize](https://github.com/npm/normalize-package-data#what-normalization-currently-entails) the package data. diff --git a/index.js b/index.js index c588b85..36ee75b 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,14 @@ import process from 'node:process'; import fs, {promises as fsPromises} from 'node:fs'; import path from 'node:path'; +import {fileURLToPath} from 'node:url'; import parseJson from 'parse-json'; import normalizePackageData from 'normalize-package-data'; -export async function readPackage({cwd = process.cwd(), normalize = true} = {}) { +const toPath = urlOrPath => urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath; + +export async function readPackage({cwd, normalize = true} = {}) { + cwd = toPath(cwd) || process.cwd(); const filePath = path.resolve(cwd, 'package.json'); const json = parseJson(await fsPromises.readFile(filePath, 'utf8')); @@ -15,7 +19,8 @@ export async function readPackage({cwd = process.cwd(), normalize = true} = {}) return json; } -export function readPackageSync({cwd = process.cwd(), normalize = true} = {}) { +export function readPackageSync({cwd, normalize = true} = {}) { + cwd = toPath(cwd) || process.cwd(); const filePath = path.resolve(cwd, 'package.json'); const json = parseJson(fs.readFileSync(filePath, 'utf8')); diff --git a/index.test-d.ts b/index.test-d.ts index 995a081..00940de 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -11,9 +11,11 @@ expectError>( readPackage({normalize: false}), ); expectType>(readPackage({cwd: '.'})); +expectType>(readPackage({cwd: new URL('file:///path/to/cwd/')})); expectType(readPackageSync()); expectType(readPackageSync({normalize: true})); expectType(readPackageSync({normalize: false})); expectError(readPackageSync({normalize: false})); expectType(readPackageSync({cwd: '.'})); +expectType(readPackageSync({cwd: new URL('file:///path/to/cwd/')})); diff --git a/readme.md b/readme.md index f98e933..c15dc92 100644 --- a/readme.md +++ b/readme.md @@ -41,7 +41,7 @@ Type: `object` ##### cwd -Type: `string`\ +Type: `URL | string`\ Default: `process.cwd()` Current working directory. diff --git a/test/test.js b/test/test.js index 116aa9b..1a7636b 100644 --- a/test/test.js +++ b/test/test.js @@ -1,4 +1,4 @@ -import {fileURLToPath} from 'url'; +import {fileURLToPath, pathToFileURL} from 'url'; import path from 'path'; import test from 'ava'; import {readPackage, readPackageSync} from '../index.js'; @@ -16,6 +16,10 @@ test('async', async t => { test('async - cwd option', async t => { const package_ = await readPackage({cwd: rootCwd}); t.is(package_.name, 'read-pkg'); + t.deepEqual( + await readPackage({cwd: pathToFileURL(rootCwd)}), + package_, + ); }); test('sync', t => { @@ -27,4 +31,8 @@ test('sync', t => { test('sync - cwd option', t => { const package_ = readPackageSync({cwd: rootCwd}); t.is(package_.name, 'read-pkg'); + t.deepEqual( + readPackageSync({cwd: pathToFileURL(rootCwd)}), + package_, + ); });