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

Rename pkg property to package, return undefined instead of empty object, add TypeScript definition #10

Merged
merged 4 commits into from
May 12, 2019
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: node_js
node_js:
- '12'
- '10'
- '8'
87 changes: 87 additions & 0 deletions index.d.ts
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;
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ module.exports = async options => {
const filePath = await findUp('package.json', options);

if (!filePath) {
return {};
return;
}

return {
pkg: await readPkg({...options, cwd: path.dirname(filePath)}),
package: await readPkg({...options, cwd: path.dirname(filePath)}),
path: filePath
};
};
Expand All @@ -20,11 +20,11 @@ module.exports.sync = options => {
const filePath = findUp.sync('package.json', options);

if (!filePath) {
return {};
return;
}

return {
pkg: readPkg.sync({...options, cwd: path.dirname(filePath)}),
package: readPkg.sync({...options, cwd: path.dirname(filePath)}),
path: filePath
};
};
36 changes: 36 additions & 0 deletions index.test-d.ts
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})
);
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"json",
Expand All @@ -26,7 +27,6 @@
"fs",
"graceful",
"load",
"pkg",
"package",
"find",
"up",
Expand All @@ -41,17 +41,18 @@
"parents",
"folder",
"directory",
"dir",
"walk",
"walking",
"path"
],
"dependencies": {
"find-up": "^3.0.0",
"read-pkg": "^5.0.0"
"find-up": "^4.0.0",
"read-pkg": "^5.1.1",
"type-fest": "^0.5.0"
},
"devDependencies": {
"ava": "^1.3.1",
"ava": "^1.4.1",
"tsd": "^0.7.3",
"xo": "^0.24.0"
}
}
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const readPkgUp = require('read-pkg-up');
console.log(await readPkgUp());
/*
{
pkg: {
package: {
name: 'awesome-package',
version: '1.0.0',
Expand All @@ -44,15 +44,15 @@ const readPkgUp = require('read-pkg-up');

### readPkgUp([options])

Returns a `Promise` for the result object.
Returns a `Promise<object>` or `undefined` if no `package.json` was found.

### readPkgUp.sync([options])

Returns the result object.
Returns the result object or `undefined` if no `package.json` was found.

#### options

Type: `Object`
Type: `object`

##### cwd

Expand Down
24 changes: 10 additions & 14 deletions test.js
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);
});