Skip to content

Commit

Permalink
Accept URL as cwd (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Feb 8, 2022
1 parent 85144a6 commit 840607e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type Options = {
@default process.cwd()
*/
cwd?: string;
cwd?: URL | string;
} & Except<ReadPackageOptions, 'cwd'>;

export type NormalizeOptions = {
Expand All @@ -16,7 +16,7 @@ export type NormalizeOptions = {
@default process.cwd()
*/
cwd?: string;
cwd?: URL | string;
} & Except<ReadPackageNormalizeOptions, 'cwd'>;

export interface ReadResult {
Expand Down
12 changes: 12 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ expectType<Promise<NormalizedReadResult | undefined>>(readPackageUp());
expectType<Promise<NormalizedReadResult | undefined>>(
readPackageUp({cwd: '.'}),
);
expectType<Promise<NormalizedReadResult | undefined>>(
readPackageUp({cwd: new URL('file:///path/to/cwd/')}),
);
expectType<Promise<NormalizedReadResult | undefined>>(
readPackageUp({normalize: true}),
);
expectType<Promise<NormalizedReadResult | undefined>>(
readPackageUp({cwd: '.', normalize: true}),
);
expectType<Promise<NormalizedReadResult | undefined>>(
readPackageUp({cwd: new URL('file:///path/to/cwd/'), normalize: true}),
);
expectType<Promise<ReadResult | undefined>>(
readPackageUp({normalize: false}),
);
Expand All @@ -22,12 +28,18 @@ expectType<NormalizedReadResult | undefined>(readPackageUpSync());
expectType<NormalizedReadResult | undefined>(
readPackageUpSync({cwd: '.'}),
);
expectType<NormalizedReadResult | undefined>(
readPackageUpSync({cwd: new URL('file:///path/to/cwd/')}),
);
expectType<NormalizedReadResult | undefined>(
readPackageUpSync({normalize: true}),
);
expectType<NormalizedReadResult | undefined>(
readPackageUpSync({cwd: '.', normalize: true}),
);
expectType<NormalizedReadResult | undefined>(
readPackageUpSync({cwd: new URL('file:///path/to/cwd/'), normalize: true}),
);
expectType<ReadResult | undefined>(
readPackageUpSync({normalize: false}),
);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
"path"
],
"dependencies": {
"find-up": "^6.2.0",
"read-pkg": "^7.0.0",
"find-up": "^6.3.0",
"read-pkg": "^7.1.0",
"type-fest": "^2.5.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Type: `object`

##### cwd

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

The directory to start looking for a package.json file.
Expand Down
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ test('async', async t => {
const result = await readPackageUp({cwd});
t.is(result.packageJson.name, 'read-pkg-up');
t.is(result.path, packagePath);
t.deepEqual(
await readPackageUp({cwd: new URL(cwd, import.meta.url)}),
result,
);

t.is(await readPackageUp({cwd: '/'}), undefined);
});
Expand All @@ -17,6 +21,10 @@ test('sync', t => {
const result = readPackageUpSync({cwd});
t.is(result.packageJson.name, 'read-pkg-up');
t.is(result.path, packagePath);
t.deepEqual(
readPackageUpSync({cwd: new URL(cwd, import.meta.url)}),
result,
);

t.is(readPackageUpSync({cwd: '/'}), undefined);
});

0 comments on commit 840607e

Please sign in to comment.