Skip to content

Commit

Permalink
Accept URL as cwd (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Feb 8, 2022
1 parent 64678da commit b496fe7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -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'));

Expand All @@ -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'));

Expand Down
2 changes: 2 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ expectError<Promise<NormalizedPackageJson>>(
readPackage({normalize: false}),
);
expectType<Promise<NormalizedPackageJson>>(readPackage({cwd: '.'}));
expectType<Promise<NormalizedPackageJson>>(readPackage({cwd: new URL('file:///path/to/cwd/')}));

expectType<NormalizedPackageJson>(readPackageSync());
expectType<NormalizedPackageJson>(readPackageSync({normalize: true}));
expectType<PackageJson>(readPackageSync({normalize: false}));
expectError<NormalizedPackageJson>(readPackageSync({normalize: false}));
expectType<NormalizedPackageJson>(readPackageSync({cwd: '.'}));
expectType<NormalizedPackageJson>(readPackageSync({cwd: new URL('file:///path/to/cwd/')}));
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Type: `object`

##### cwd

Type: `string`\
Type: `URL | string`\
Default: `process.cwd()`

Current working directory.
Expand Down
10 changes: 9 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 => {
Expand All @@ -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_,
);
});

0 comments on commit b496fe7

Please sign in to comment.