Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept URL as cwd #27

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor Author

@fisker fisker Feb 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to use URL directly, but

new URL('package.json', 'file:///a/').href
'file:///a/package.json'
new URL('package.json', 'file:///a').href
'file:///package.json'

So, use path is easier, I guess.

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_,
);
});