-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
021b1ec
commit e6fbe7f
Showing
8 changed files
with
125 additions
and
141 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
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,87 +1,77 @@ | ||
import {Except} from 'type-fest'; | ||
import readPkg = require('read-pkg'); | ||
import {readPackageAsync, readPackageSync, Options as ReadPackageOptions, NormalizeOptions as ReadPackageNormalizeOptions, PackageJson, NormalizedPackageJson} from 'read-pkg'; | ||
|
||
declare namespace readPkgUp { | ||
type Options = { | ||
/** | ||
Directory to start looking for a package.json file. | ||
@default process.cwd() | ||
*/ | ||
cwd?: string; | ||
} & Except<readPkg.Options, 'cwd'>; | ||
export type Options = { | ||
/** | ||
Directory to start looking for a package.json file. | ||
type NormalizeOptions = { | ||
/** | ||
Directory to start looking for a package.json file. | ||
@default process.cwd() | ||
*/ | ||
cwd?: string; | ||
} & Except<ReadPackageOptions, 'cwd'>; | ||
|
||
@default process.cwd() | ||
*/ | ||
cwd?: string; | ||
} & Except<readPkg.NormalizeOptions, 'cwd'>; | ||
export type NormalizeOptions = { | ||
/** | ||
Directory to start looking for a package.json file. | ||
type PackageJson = readPkg.PackageJson; | ||
type NormalizedPackageJson = readPkg.NormalizedPackageJson; | ||
@default process.cwd() | ||
*/ | ||
cwd?: string; | ||
} & Except<ReadPackageNormalizeOptions, 'cwd'>; | ||
|
||
interface ReadResult { | ||
packageJson: PackageJson; | ||
path: string; | ||
} | ||
export interface ReadResult { | ||
packageJson: PackageJson; | ||
path: string; | ||
} | ||
|
||
interface NormalizedReadResult { | ||
packageJson: NormalizedPackageJson; | ||
path: string; | ||
} | ||
export interface NormalizedReadResult { | ||
packageJson: NormalizedPackageJson; | ||
path: string; | ||
} | ||
|
||
declare const readPkgUp: { | ||
/** | ||
Read the closest `package.json` file. | ||
export { | ||
PackageJson, | ||
NormalizedPackageJson | ||
}; | ||
|
||
@example | ||
``` | ||
import readPkgUp = require('read-pkg-up'); | ||
/** | ||
Read the closest `package.json` file. | ||
(async () => { | ||
console.log(await readPkgUp()); | ||
// { | ||
// packageJson: { | ||
// 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>; | ||
@example | ||
``` | ||
import {readPackageUpAsync} from 'read-pkg-up'; | ||
/** | ||
Synchronously read the closest `package.json` file. | ||
console.log(await readPackageUpAsync()); | ||
// { | ||
// packageJson: { | ||
// name: 'awesome-package', | ||
// version: '1.0.0', | ||
// … | ||
// }, | ||
// path: '/Users/sindresorhus/dev/awesome-package/package.json' | ||
// } | ||
``` | ||
*/ | ||
export function readPackageUpAsync(options?: NormalizeOptions): Promise<NormalizedReadResult | undefined>; | ||
export function readPackageUpAsync(options: Options): Promise<ReadResult | undefined>; | ||
|
||
@example | ||
``` | ||
import readPkgUp = require('read-pkg-up'); | ||
/** | ||
Synchronously read the closest `package.json` file. | ||
console.log(readPkgUp.sync()); | ||
// { | ||
// packageJson: { | ||
// 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; | ||
}; | ||
@example | ||
``` | ||
import {readPackageUpSync} from 'read-pkg-up'; | ||
export = readPkgUp; | ||
console.log(readPackageUpSync()); | ||
// { | ||
// packageJson: { | ||
// name: 'awesome-package', | ||
// version: '1.0.0', | ||
// … | ||
// }, | ||
// path: '/Users/sindresorhus/dev/awesome-package/package.json' | ||
// } | ||
``` | ||
*/ | ||
export function readPackageUpSync(options?: NormalizeOptions): NormalizedReadResult | undefined; | ||
export function readPackageUpSync(options: Options): ReadResult | undefined; |
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,30 +1,27 @@ | ||
'use strict'; | ||
const path = require('path'); | ||
const findUp = require('find-up'); | ||
const readPkg = require('read-pkg'); | ||
import path from 'path'; | ||
import findUp from 'find-up'; | ||
import {readPackageAsync, readPackageSync} from 'read-pkg'; | ||
|
||
module.exports = async options => { | ||
export async function readPackageUpAsync(options) { | ||
const filePath = await findUp('package.json', options); | ||
|
||
if (!filePath) { | ||
return; | ||
} | ||
|
||
return { | ||
packageJson: await readPkg({...options, cwd: path.dirname(filePath)}), | ||
packageJson: await readPackageAsync({...options, cwd: path.dirname(filePath)}), | ||
path: filePath | ||
}; | ||
}; | ||
} | ||
|
||
module.exports.sync = options => { | ||
export function readPackageUpSync(options) { | ||
const filePath = findUp.sync('package.json', options); | ||
|
||
if (!filePath) { | ||
return; | ||
} | ||
|
||
return { | ||
packageJson: readPkg.sync({...options, cwd: path.dirname(filePath)}), | ||
packageJson: readPackageSync({...options, cwd: path.dirname(filePath)}), | ||
path: filePath | ||
}; | ||
}; | ||
} |
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,36 +1,36 @@ | ||
import {expectType, expectError} from 'tsd'; | ||
import readPkgUp = require('.'); | ||
import {readPackageUpAsync, readPackageUpSync, ReadResult, NormalizedReadResult} from './index.js'; | ||
|
||
expectType<Promise<readPkgUp.NormalizedReadResult | undefined>>(readPkgUp()); | ||
expectType<Promise<readPkgUp.NormalizedReadResult | undefined>>( | ||
readPkgUp({cwd: '.'}) | ||
expectType<Promise<NormalizedReadResult | undefined>>(readPackageUpAsync()); | ||
expectType<Promise<NormalizedReadResult | undefined>>( | ||
readPackageUpAsync({cwd: '.'}) | ||
); | ||
expectType<Promise<readPkgUp.NormalizedReadResult | undefined>>( | ||
readPkgUp({normalize: true}) | ||
expectType<Promise<NormalizedReadResult | undefined>>( | ||
readPackageUpAsync({normalize: true}) | ||
); | ||
expectType<Promise<readPkgUp.NormalizedReadResult | undefined>>( | ||
readPkgUp({cwd: '.', normalize: true}) | ||
expectType<Promise<NormalizedReadResult | undefined>>( | ||
readPackageUpAsync({cwd: '.', normalize: true}) | ||
); | ||
expectType<Promise<readPkgUp.ReadResult | undefined>>( | ||
readPkgUp({normalize: false}) | ||
expectType<Promise<ReadResult | undefined>>( | ||
readPackageUpAsync({normalize: false}) | ||
); | ||
expectError<Promise<readPkgUp.NormalizedReadResult | undefined>>( | ||
readPkgUp({normalize: false}) | ||
expectError<Promise<NormalizedReadResult | undefined>>( | ||
readPackageUpAsync({normalize: false}) | ||
); | ||
|
||
expectType<readPkgUp.NormalizedReadResult | undefined>(readPkgUp.sync()); | ||
expectType<readPkgUp.NormalizedReadResult | undefined>( | ||
readPkgUp.sync({cwd: '.'}) | ||
expectType<NormalizedReadResult | undefined>(readPackageUpSync()); | ||
expectType<NormalizedReadResult | undefined>( | ||
readPackageUpSync({cwd: '.'}) | ||
); | ||
expectType<readPkgUp.NormalizedReadResult | undefined>( | ||
readPkgUp.sync({normalize: true}) | ||
expectType<NormalizedReadResult | undefined>( | ||
readPackageUpSync({normalize: true}) | ||
); | ||
expectType<readPkgUp.NormalizedReadResult | undefined>( | ||
readPkgUp.sync({cwd: '.', normalize: true}) | ||
expectType<NormalizedReadResult | undefined>( | ||
readPackageUpSync({cwd: '.', normalize: true}) | ||
); | ||
expectType<readPkgUp.ReadResult | undefined>( | ||
readPkgUp.sync({normalize: false}) | ||
expectType<ReadResult | undefined>( | ||
readPackageUpSync({normalize: false}) | ||
); | ||
expectError<readPkgUp.NormalizedReadResult | undefined>( | ||
readPkgUp.sync({normalize: false}) | ||
expectError<NormalizedReadResult | undefined>( | ||
readPackageUpSync({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
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,22 +1,22 @@ | ||
import path from 'path'; | ||
import test from 'ava'; | ||
import readPackageUp from '.'; | ||
import {readPackageUpAsync, readPackageUpSync} from './index.js'; | ||
|
||
const cwd = 'fixture'; | ||
const packagePath = path.resolve('.', 'package.json'); | ||
|
||
test('async', async t => { | ||
const result = await readPackageUp({cwd}); | ||
const result = await readPackageUpAsync({cwd}); | ||
t.is(result.packageJson.name, 'read-pkg-up'); | ||
t.is(result.path, packagePath); | ||
|
||
t.is(await readPackageUp({cwd: '/'}), undefined); | ||
t.is(await readPackageUpAsync({cwd: '/'}), undefined); | ||
}); | ||
|
||
test('sync', t => { | ||
const result = readPackageUp.sync({cwd}); | ||
const result = readPackageUpSync({cwd}); | ||
t.is(result.packageJson.name, 'read-pkg-up'); | ||
t.is(result.path, packagePath); | ||
|
||
t.is(readPackageUp.sync({cwd: '/'}), undefined); | ||
t.is(readPackageUpSync({cwd: '/'}), undefined); | ||
}); |