Skip to content

Commit b496fe7

Browse files
authoredFeb 8, 2022
Accept URL as cwd (#27)
1 parent 64678da commit b496fe7

File tree

5 files changed

+20
-5
lines changed

5 files changed

+20
-5
lines changed
 

‎index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export interface Options {
77
88
@default process.cwd()
99
*/
10-
readonly cwd?: string;
10+
readonly cwd?: URL | string;
1111

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

‎index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import process from 'node:process';
22
import fs, {promises as fsPromises} from 'node:fs';
33
import path from 'node:path';
4+
import {fileURLToPath} from 'node:url';
45
import parseJson from 'parse-json';
56
import normalizePackageData from 'normalize-package-data';
67

7-
export async function readPackage({cwd = process.cwd(), normalize = true} = {}) {
8+
const toPath = urlOrPath => urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath;
9+
10+
export async function readPackage({cwd, normalize = true} = {}) {
11+
cwd = toPath(cwd) || process.cwd();
812
const filePath = path.resolve(cwd, 'package.json');
913
const json = parseJson(await fsPromises.readFile(filePath, 'utf8'));
1014

@@ -15,7 +19,8 @@ export async function readPackage({cwd = process.cwd(), normalize = true} = {})
1519
return json;
1620
}
1721

18-
export function readPackageSync({cwd = process.cwd(), normalize = true} = {}) {
22+
export function readPackageSync({cwd, normalize = true} = {}) {
23+
cwd = toPath(cwd) || process.cwd();
1924
const filePath = path.resolve(cwd, 'package.json');
2025
const json = parseJson(fs.readFileSync(filePath, 'utf8'));
2126

‎index.test-d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ expectError<Promise<NormalizedPackageJson>>(
1111
readPackage({normalize: false}),
1212
);
1313
expectType<Promise<NormalizedPackageJson>>(readPackage({cwd: '.'}));
14+
expectType<Promise<NormalizedPackageJson>>(readPackage({cwd: new URL('file:///path/to/cwd/')}));
1415

1516
expectType<NormalizedPackageJson>(readPackageSync());
1617
expectType<NormalizedPackageJson>(readPackageSync({normalize: true}));
1718
expectType<PackageJson>(readPackageSync({normalize: false}));
1819
expectError<NormalizedPackageJson>(readPackageSync({normalize: false}));
1920
expectType<NormalizedPackageJson>(readPackageSync({cwd: '.'}));
21+
expectType<NormalizedPackageJson>(readPackageSync({cwd: new URL('file:///path/to/cwd/')}));

‎readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Type: `object`
4141

4242
##### cwd
4343

44-
Type: `string`\
44+
Type: `URL | string`\
4545
Default: `process.cwd()`
4646

4747
Current working directory.

‎test/test.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {fileURLToPath} from 'url';
1+
import {fileURLToPath, pathToFileURL} from 'url';
22
import path from 'path';
33
import test from 'ava';
44
import {readPackage, readPackageSync} from '../index.js';
@@ -16,6 +16,10 @@ test('async', async t => {
1616
test('async - cwd option', async t => {
1717
const package_ = await readPackage({cwd: rootCwd});
1818
t.is(package_.name, 'read-pkg');
19+
t.deepEqual(
20+
await readPackage({cwd: pathToFileURL(rootCwd)}),
21+
package_,
22+
);
1923
});
2024

2125
test('sync', t => {
@@ -27,4 +31,8 @@ test('sync', t => {
2731
test('sync - cwd option', t => {
2832
const package_ = readPackageSync({cwd: rootCwd});
2933
t.is(package_.name, 'read-pkg');
34+
t.deepEqual(
35+
readPackageSync({cwd: pathToFileURL(rootCwd)}),
36+
package_,
37+
);
3038
});

0 commit comments

Comments
 (0)
Please sign in to comment.