-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
4c0cd9a
commit 2088095
Showing
9 changed files
with
106 additions
and
136 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,67 +1,59 @@ | ||
import * as typeFest from 'type-fest'; | ||
import normalize = require('normalize-package-data'); | ||
import * as normalize from 'normalize-package-data'; | ||
|
||
declare namespace readPkg { | ||
interface Options { | ||
/** | ||
[Normalize](https://github.com/npm/normalize-package-data#what-normalization-currently-entails) the package data. | ||
@default true | ||
*/ | ||
readonly normalize?: boolean; | ||
export interface Options { | ||
/** | ||
Current working directory. | ||
/** | ||
Current working directory. | ||
@default process.cwd() | ||
*/ | ||
readonly cwd?: string; | ||
|
||
@default process.cwd() | ||
*/ | ||
readonly cwd?: string; | ||
} | ||
/** | ||
[Normalize](https://github.com/npm/normalize-package-data#what-normalization-currently-entails) the package data. | ||
interface NormalizeOptions extends Options { | ||
readonly normalize?: true; | ||
} | ||
@default true | ||
*/ | ||
readonly normalize?: boolean; | ||
} | ||
|
||
type NormalizedPackageJson = PackageJson & normalize.Package; | ||
type PackageJson = typeFest.PackageJson; | ||
export interface NormalizeOptions extends Options { | ||
readonly normalize?: true; | ||
} | ||
|
||
declare const readPkg: { | ||
/** | ||
@returns The parsed JSON. | ||
export type NormalizedPackageJson = PackageJson & normalize.Package; | ||
export type PackageJson = typeFest.PackageJson; | ||
|
||
@example | ||
``` | ||
import readPkg = require('read-pkg'); | ||
/** | ||
@returns The parsed JSON. | ||
(async () => { | ||
console.log(await readPkg()); | ||
//=> {name: 'read-pkg', …} | ||
@example | ||
``` | ||
import {readPackageAsync} from 'read-pkg'; | ||
console.log(await readPkg({cwd: 'some-other-directory'}); | ||
//=> {name: 'unicorn', …} | ||
})(); | ||
``` | ||
*/ | ||
(options?: readPkg.NormalizeOptions): Promise<readPkg.NormalizedPackageJson>; | ||
(options: readPkg.Options): Promise<readPkg.PackageJson>; | ||
console.log(await readPackageAsync()); | ||
//=> {name: 'read-pkg', …} | ||
/** | ||
@returns The parsed JSON. | ||
console.log(await readPackageAsync({cwd: 'some-other-directory'}); | ||
//=> {name: 'unicorn', …} | ||
``` | ||
*/ | ||
export function readPackageAsync(options?: NormalizeOptions): Promise<NormalizedPackageJson>; | ||
export function readPackageAsync(options: Options): Promise<PackageJson>; | ||
|
||
@example | ||
``` | ||
import readPkg = require('read-pkg'); | ||
/** | ||
@returns The parsed JSON. | ||
console.log(readPkg.sync()); | ||
//=> {name: 'read-pkg', …} | ||
@example | ||
``` | ||
import {readPackageSync} from 'read-pkg'; | ||
console.log(readPkg.sync({cwd: 'some-other-directory'}); | ||
//=> {name: 'unicorn', …} | ||
``` | ||
*/ | ||
sync(options?: readPkg.NormalizeOptions): readPkg.NormalizedPackageJson; | ||
sync(options: readPkg.Options): readPkg.PackageJson; | ||
}; | ||
console.log(readPackageSync()); | ||
//=> {name: 'read-pkg', …} | ||
export = readPkg; | ||
console.log(readPackageSync({cwd: 'some-other-directory'}); | ||
//=> {name: 'unicorn', …} | ||
``` | ||
*/ | ||
export function readPackageSync(options?: NormalizeOptions): NormalizedPackageJson; | ||
export function readPackageSync(options: Options): PackageJson; |
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,41 +1,26 @@ | ||
'use strict'; | ||
const {promisify} = require('util'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const parseJson = require('parse-json'); | ||
import fs, {promises as fsAsync} from 'fs'; | ||
import path from 'path'; | ||
import parseJson from 'parse-json'; | ||
import normalizePackageData from 'normalize-package-data'; | ||
|
||
const readFileAsync = promisify(fs.readFile); | ||
export async function readPackageAsync({cwd = process.cwd(), normalize = true} = {}) { | ||
const filePath = path.resolve(cwd, 'package.json'); | ||
const json = parseJson(await fsAsync.readFile(filePath, 'utf8')); | ||
|
||
module.exports = async options => { | ||
options = { | ||
cwd: process.cwd(), | ||
normalize: true, | ||
...options | ||
}; | ||
|
||
const filePath = path.resolve(options.cwd, 'package.json'); | ||
const json = parseJson(await readFileAsync(filePath, 'utf8')); | ||
|
||
if (options.normalize) { | ||
require('normalize-package-data')(json); | ||
if (normalize) { | ||
normalizePackageData(json); | ||
} | ||
|
||
return json; | ||
}; | ||
|
||
module.exports.sync = options => { | ||
options = { | ||
cwd: process.cwd(), | ||
normalize: true, | ||
...options | ||
}; | ||
} | ||
|
||
const filePath = path.resolve(options.cwd, 'package.json'); | ||
export function readPackageSync({cwd = process.cwd(), normalize = true} = {}) { | ||
const filePath = path.resolve(cwd, 'package.json'); | ||
const json = parseJson(fs.readFileSync(filePath, 'utf8')); | ||
|
||
if (options.normalize) { | ||
require('normalize-package-data')(json); | ||
if (normalize) { | ||
normalizePackageData(json); | ||
} | ||
|
||
return json; | ||
}; | ||
} |
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,20 +1,20 @@ | ||
import {expectType, expectError} from 'tsd'; | ||
import readPkg = require('.'); | ||
import {expectType, expectError, expectAssignable} from 'tsd'; | ||
import {readPackageAsync, readPackageSync, Options, NormalizedPackageJson, PackageJson} from './index.js'; | ||
|
||
const options: readPkg.Options = {}; | ||
expectError<readPkg.NormalizedPackageJson>({}); | ||
expectType<readPkg.PackageJson>({}); | ||
const options: Options = {}; | ||
expectError<NormalizedPackageJson>({}); | ||
expectAssignable<PackageJson>({}); | ||
|
||
expectType<Promise<readPkg.NormalizedPackageJson>>(readPkg()); | ||
expectType<Promise<readPkg.NormalizedPackageJson>>(readPkg({normalize: true})); | ||
expectType<Promise<readPkg.PackageJson>>(readPkg({normalize: false})); | ||
expectError<Promise<readPkg.NormalizedPackageJson>>( | ||
readPkg({normalize: false}) | ||
expectType<Promise<NormalizedPackageJson>>(readPackageAsync()); | ||
expectType<Promise<NormalizedPackageJson>>(readPackageAsync({normalize: true})); | ||
expectType<Promise<PackageJson>>(readPackageAsync({normalize: false})); | ||
expectError<Promise<NormalizedPackageJson>>( | ||
readPackageAsync({normalize: false}) | ||
); | ||
expectType<Promise<readPkg.NormalizedPackageJson>>(readPkg({cwd: '.'})); | ||
expectType<Promise<NormalizedPackageJson>>(readPackageAsync({cwd: '.'})); | ||
|
||
expectType<readPkg.NormalizedPackageJson>(readPkg.sync()); | ||
expectType<readPkg.NormalizedPackageJson>(readPkg.sync({normalize: true})); | ||
expectType<readPkg.PackageJson>(readPkg.sync({normalize: false})); | ||
expectError<readPkg.NormalizedPackageJson>(readPkg.sync({normalize: false})); | ||
expectType<readPkg.NormalizedPackageJson>(readPkg.sync({cwd: '.'})); | ||
expectType<NormalizedPackageJson>(readPackageSync()); | ||
expectType<NormalizedPackageJson>(readPackageSync({normalize: true})); | ||
expectType<PackageJson>(readPackageSync({normalize: false})); | ||
expectError<NormalizedPackageJson>(readPackageSync({normalize: false})); | ||
expectType<NormalizedPackageJson>(readPackageSync({cwd: '.'})); |
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,4 +1,5 @@ | ||
{ | ||
"name": "unicorn", | ||
"version": "1.0.0" | ||
"version": "1.0.0", | ||
"type": "module" | ||
} |
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,30 @@ | ||
'use strict'; | ||
import {fileURLToPath} from 'url'; | ||
import path from 'path'; | ||
import test from 'ava'; | ||
import readPackage from '..'; | ||
import {readPackageAsync, readPackageSync} from '../index.js'; | ||
|
||
process.chdir(__dirname); | ||
|
||
const rootCwd = path.join(__dirname, '..'); | ||
const dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
process.chdir(dirname); | ||
const rootCwd = path.join(dirname, '..'); | ||
|
||
test('async', async t => { | ||
const package_ = await readPackage(); | ||
const package_ = await readPackageAsync(); | ||
t.is(package_.name, 'unicorn'); | ||
t.truthy(package_._id); | ||
}); | ||
|
||
test('async - cwd option', async t => { | ||
const package_ = await readPackage({cwd: rootCwd}); | ||
const package_ = await readPackageAsync({cwd: rootCwd}); | ||
t.is(package_.name, 'read-pkg'); | ||
}); | ||
|
||
test('sync', t => { | ||
const package_ = readPackage.sync(); | ||
const package_ = readPackageSync(); | ||
t.is(package_.name, 'unicorn'); | ||
t.truthy(package_._id); | ||
}); | ||
|
||
test('sync - cwd option', t => { | ||
const package_ = readPackage.sync({cwd: rootCwd}); | ||
const package_ = readPackageSync({cwd: rootCwd}); | ||
t.is(package_.name, 'read-pkg'); | ||
}); |