-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename
pkg
property to package
, return undefined
instead of emp…
…ty object, add TypeScript definition (#10)
- Loading branch information
1 parent
2c1d52f
commit 1c15896
Showing
6 changed files
with
149 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import {Omit} from 'type-fest'; | ||
import readPkg = require('read-pkg'); | ||
|
||
declare namespace readPkgUp { | ||
type Options = { | ||
/** | ||
Directory to start looking for a package.json file. | ||
@default process.cwd() | ||
*/ | ||
cwd?: string; | ||
} & Omit<readPkg.Options, 'cwd'>; | ||
|
||
type NormalizeOptions = { | ||
/** | ||
Directory to start looking for a package.json file. | ||
@default process.cwd() | ||
*/ | ||
cwd?: string; | ||
} & Omit<readPkg.NormalizeOptions, 'cwd'>; | ||
|
||
type PackageJson = readPkg.PackageJson; | ||
type NormalizedPackageJson = readPkg.NormalizedPackageJson; | ||
|
||
interface ReadResult { | ||
package: PackageJson; | ||
path: string; | ||
} | ||
|
||
interface NormalizedReadResult { | ||
package: NormalizedPackageJson; | ||
path: string; | ||
} | ||
} | ||
|
||
declare const readPkgUp: { | ||
/** | ||
Read the closest `package.json` file. | ||
@example | ||
``` | ||
import readPkgUp = require('read-pkg-up'); | ||
(async () => { | ||
console.log(await readPkgUp()); | ||
// { | ||
// package: { | ||
// name: 'awesome-package', | ||
// version: '1.0.0', | ||
// … | ||
// }, | ||
// path: '/Users/sindresorhus/dev/awesome-package/package.json' | ||
// } | ||
})(); | ||
``` | ||
*/ | ||
(options?: readPkgUp.NormalizeOptions): Promise< | ||
readPkgUp.NormalizedReadResult | undefined | ||
>; | ||
(options: readPkgUp.Options): Promise<readPkgUp.ReadResult | undefined>; | ||
|
||
/** | ||
Synchronously read the closest `package.json` file. | ||
@example | ||
``` | ||
import readPkgUp = require('read-pkg-up'); | ||
console.log(readPkgUp.sync()); | ||
// { | ||
// package: { | ||
// name: 'awesome-package', | ||
// version: '1.0.0', | ||
// … | ||
// }, | ||
// path: '/Users/sindresorhus/dev/awesome-package/package.json' | ||
// } | ||
``` | ||
*/ | ||
sync( | ||
options?: readPkgUp.NormalizeOptions | ||
): readPkgUp.NormalizedReadResult | undefined; | ||
sync(options: readPkgUp.Options): readPkgUp.ReadResult | undefined; | ||
}; | ||
|
||
export = readPkgUp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {expectType, expectError} from 'tsd'; | ||
import readPkgUp = require('.'); | ||
|
||
expectType<Promise<readPkgUp.NormalizedReadResult | undefined>>(readPkgUp()); | ||
expectType<Promise<readPkgUp.NormalizedReadResult | undefined>>( | ||
readPkgUp({cwd: '.'}) | ||
); | ||
expectType<Promise<readPkgUp.NormalizedReadResult | undefined>>( | ||
readPkgUp({normalize: true}) | ||
); | ||
expectType<Promise<readPkgUp.NormalizedReadResult | undefined>>( | ||
readPkgUp({cwd: '.', normalize: true}) | ||
); | ||
expectType<Promise<readPkgUp.ReadResult | undefined>>( | ||
readPkgUp({normalize: false}) | ||
); | ||
expectError<Promise<readPkgUp.NormalizedReadResult | undefined>>( | ||
readPkgUp({normalize: false}) | ||
); | ||
|
||
expectType<readPkgUp.NormalizedReadResult | undefined>(readPkgUp.sync()); | ||
expectType<readPkgUp.NormalizedReadResult | undefined>( | ||
readPkgUp.sync({cwd: '.'}) | ||
); | ||
expectType<readPkgUp.NormalizedReadResult | undefined>( | ||
readPkgUp.sync({normalize: true}) | ||
); | ||
expectType<readPkgUp.NormalizedReadResult | undefined>( | ||
readPkgUp.sync({cwd: '.', normalize: true}) | ||
); | ||
expectType<readPkgUp.ReadResult | undefined>( | ||
readPkgUp.sync({normalize: false}) | ||
); | ||
expectError<readPkgUp.NormalizedReadResult | undefined>( | ||
readPkgUp.sync({normalize: false}) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,22 @@ | ||
import path from 'path'; | ||
import test from 'ava'; | ||
import readPkgUp from '.'; | ||
import readPackageUp from '.'; | ||
|
||
const cwd = 'fixture'; | ||
const pkgPath = path.resolve('.', 'package.json'); | ||
const packagePath = path.resolve('.', 'package.json'); | ||
|
||
test('async', async t => { | ||
const result = await readPkgUp({cwd}); | ||
t.is(result.pkg.name, 'read-pkg-up'); | ||
t.is(result.path, pkgPath); | ||
const result = await readPackageUp({cwd}); | ||
t.is(result.package.name, 'read-pkg-up'); | ||
t.is(result.path, packagePath); | ||
|
||
const result2 = await readPkgUp({cwd: '/'}); | ||
t.is(result2.pkg, undefined); | ||
t.is(result2.path, undefined); | ||
t.is(await readPackageUp({cwd: '/'}), undefined); | ||
}); | ||
|
||
test('sync', t => { | ||
const result = readPkgUp.sync({cwd}); | ||
t.is(result.pkg.name, 'read-pkg-up'); | ||
t.is(result.path, pkgPath); | ||
const result = readPackageUp.sync({cwd}); | ||
t.is(result.package.name, 'read-pkg-up'); | ||
t.is(result.path, packagePath); | ||
|
||
const result2 = readPkgUp.sync({cwd: '/'}); | ||
t.is(result2.pkg, undefined); | ||
t.is(result2.path, undefined); | ||
t.is(readPackageUp.sync({cwd: '/'}), undefined); | ||
}); |