pax_global_header00006660000000000000000000000064151421157640014520gustar00rootroot0000000000000052 comment=ae4bbc6588ba8707f931fc141d2b1d3bf0c8c703 sindresorhus-read-pkg-69bc188/000077500000000000000000000000001514211576400163065ustar00rootroot00000000000000sindresorhus-read-pkg-69bc188/.editorconfig000066400000000000000000000002571514211576400207670ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 sindresorhus-read-pkg-69bc188/.gitattributes000066400000000000000000000000231514211576400211740ustar00rootroot00000000000000* text=auto eol=lf sindresorhus-read-pkg-69bc188/.github/000077500000000000000000000000001514211576400176465ustar00rootroot00000000000000sindresorhus-read-pkg-69bc188/.github/security.md000066400000000000000000000002631514211576400220400ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. sindresorhus-read-pkg-69bc188/.github/workflows/000077500000000000000000000000001514211576400217035ustar00rootroot00000000000000sindresorhus-read-pkg-69bc188/.github/workflows/main.yml000066400000000000000000000006451514211576400233570ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 24 - 20 steps: - uses: actions/checkout@v5 - uses: actions/setup-node@v6 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test sindresorhus-read-pkg-69bc188/.gitignore000066400000000000000000000000271514211576400202750ustar00rootroot00000000000000node_modules yarn.lock sindresorhus-read-pkg-69bc188/.npmrc000066400000000000000000000000231514211576400174210ustar00rootroot00000000000000package-lock=false sindresorhus-read-pkg-69bc188/index.d.ts000066400000000000000000000035201514211576400202070ustar00rootroot00000000000000import type {PackageJson as typeFestPackageJson} from 'type-fest'; import type {Package as normalizePackage} from 'normalize-package-data'; export type Options = { /** Current working directory. @default process.cwd() */ readonly cwd?: URL | string; /** [Normalize](https://github.com/npm/normalize-package-data#what-normalization-currently-entails) the package data. @default true */ readonly normalize?: boolean; }; // eslint-disable-next-line @typescript-eslint/naming-convention type _NormalizeOptions = { readonly normalize?: true; }; export type NormalizeOptions = _NormalizeOptions & Options; export type ParseOptions = Omit; export type NormalizeParseOptions = _NormalizeOptions & ParseOptions; export type NormalizedPackageJson = PackageJson & normalizePackage; export type PackageJson = typeFestPackageJson; /** @returns The parsed JSON. @example ``` import {readPackage} from 'read-pkg'; console.log(await readPackage()); //=> {name: 'read-pkg', …} console.log(await readPackage({cwd: 'some-other-directory'}); //=> {name: 'unicorn', …} ``` */ export function readPackage(options?: NormalizeOptions): Promise; export function readPackage(options: Options): Promise; /** @returns The parsed JSON. @example ``` import {readPackageSync} from 'read-pkg'; console.log(readPackageSync()); //=> {name: 'read-pkg', …} console.log(readPackageSync({cwd: 'some-other-directory'}); //=> {name: 'unicorn', …} ``` */ export function readPackageSync(options?: NormalizeOptions): NormalizedPackageJson; export function readPackageSync(options: Options): PackageJson; export function parsePackage(packageFile: PackageJson | string, options?: NormalizeParseOptions): NormalizedPackageJson; export function parsePackage(packageFile: PackageJson | string, options: ParseOptions): PackageJson; sindresorhus-read-pkg-69bc188/index.js000066400000000000000000000025161514211576400177570ustar00rootroot00000000000000import fs from 'node:fs'; import fsPromises from 'node:fs/promises'; import path from 'node:path'; import parseJson from 'parse-json'; import normalizePackageData from 'normalize-package-data'; import {toPath} from 'unicorn-magic/node'; const getPackagePath = cwd => path.resolve(toPath(cwd) ?? '.', 'package.json'); const _readPackage = (file, normalize) => { const json = typeof file === 'string' ? parseJson(file) : file; if (normalize) { normalizePackageData(json); } return json; }; export async function readPackage({cwd, normalize = true} = {}) { const packageFile = await fsPromises.readFile(getPackagePath(cwd), 'utf8'); return _readPackage(packageFile, normalize); } export function readPackageSync({cwd, normalize = true} = {}) { const packageFile = fs.readFileSync(getPackagePath(cwd), 'utf8'); return _readPackage(packageFile, normalize); } export function parsePackage(packageFile, {normalize = true} = {}) { const isObject = packageFile !== null && typeof packageFile === 'object' && !Array.isArray(packageFile); const isString = typeof packageFile === 'string'; if (!isObject && !isString) { throw new TypeError('`packageFile` should be either an `object` or a `string`.'); } const clonedPackageFile = isObject ? structuredClone(packageFile) : packageFile; return _readPackage(clonedPackageFile, normalize); } sindresorhus-read-pkg-69bc188/license000066400000000000000000000021351514211576400176540ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. sindresorhus-read-pkg-69bc188/package.json000066400000000000000000000016411514211576400205760ustar00rootroot00000000000000{ "name": "read-pkg", "version": "10.1.0", "description": "Read a package.json file", "license": "MIT", "repository": "sindresorhus/read-pkg", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "type": "module", "exports": { "types": "./index.d.ts", "default": "./index.js" }, "sideEffects": false, "engines": { "node": ">=20" }, "scripts": { "test": "xo && cd test && node --test test.js" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "json", "read", "parse", "file", "fs", "graceful", "load", "package", "normalize" ], "dependencies": { "@types/normalize-package-data": "^2.4.4", "normalize-package-data": "^8.0.0", "parse-json": "^8.3.0", "type-fest": "^5.4.4", "unicorn-magic": "^0.4.0" }, "devDependencies": { "xo": "^1.2.3" } } sindresorhus-read-pkg-69bc188/readme.md000066400000000000000000000031211514211576400200620ustar00rootroot00000000000000# read-pkg > Read a package.json file ## Why - [Throws more helpful JSON errors](https://github.com/sindresorhus/parse-json) - [Normalizes the data](https://github.com/npm/normalize-package-data#what-normalization-currently-entails) ## Install ```sh npm install read-pkg ``` ## Usage ```js import {readPackage} from 'read-pkg'; console.log(await readPackage()); //=> {name: 'read-pkg', …} console.log(await readPackage({cwd: 'some-other-directory'})); //=> {name: 'unicorn', …} ``` ## API ### readPackage(options?) Returns a `Promise` with the parsed JSON. ### readPackageSync(options?) Returns the parsed JSON. #### options Type: `object` ##### cwd Type: `URL | string`\ Default: `process.cwd()` Current working directory. ##### normalize Type: `boolean`\ Default: `true` [Normalize](https://github.com/npm/normalize-package-data#what-normalization-currently-entails) the package data. ### parsePackage(packageFile, options?) Parses an object or string into JSON. #### packageFile Type: `object | string` An object or a stringified object to be parsed as a package.json. #### options Type: `object` ##### normalize Type: `boolean`\ Default: `true` [Normalize](https://github.com/npm/normalize-package-data#what-normalization-currently-entails) the package data. ## Related - [read-package-up](https://github.com/sindresorhus/read-package-up) - Read the closest package.json file - [write-package](https://github.com/sindresorhus/write-package) - Write a `package.json` file - [load-json-file](https://github.com/sindresorhus/load-json-file) - Read and parse a JSON file sindresorhus-read-pkg-69bc188/test/000077500000000000000000000000001514211576400172655ustar00rootroot00000000000000sindresorhus-read-pkg-69bc188/test/package.json000066400000000000000000000001001514211576400215420ustar00rootroot00000000000000{ "name": "unicorn ", "version": "1.0.0", "type": "module" } sindresorhus-read-pkg-69bc188/test/test.js000066400000000000000000000053471514211576400206130ustar00rootroot00000000000000import {fileURLToPath, pathToFileURL} from 'node:url'; import path from 'node:path'; import {test} from 'node:test'; import assert from 'node:assert/strict'; import {readPackage, readPackageSync, parsePackage} from '../index.js'; const dirname = path.dirname(fileURLToPath(import.meta.url)); const rootCwd = path.join(dirname, '..'); test('async', async () => { const package_ = await readPackage(); assert.strictEqual(package_.name, 'unicorn'); assert.ok(package_._id); }); test('async - cwd option', async () => { const package_ = await readPackage({cwd: rootCwd}); assert.strictEqual(package_.name, 'read-pkg'); assert.deepStrictEqual( await readPackage({cwd: pathToFileURL(rootCwd)}), package_, ); }); test('async - normalize option', async () => { const package_ = await readPackage({normalize: false}); assert.strictEqual(package_.name, 'unicorn '); }); test('sync', () => { const package_ = readPackageSync(); assert.strictEqual(package_.name, 'unicorn'); assert.ok(package_._id); }); test('sync - cwd option', () => { const package_ = readPackageSync({cwd: rootCwd}); assert.strictEqual(package_.name, 'read-pkg'); assert.deepStrictEqual( readPackageSync({cwd: pathToFileURL(rootCwd)}), package_, ); }); test('sync - normalize option', () => { const package_ = readPackageSync({normalize: false}); assert.strictEqual(package_.name, 'unicorn '); }); const pkgJson = { name: 'unicorn ', version: '1.0.0', type: 'module', }; test('parsePackage - json input', () => { const package_ = parsePackage(pkgJson); assert.strictEqual(package_.name, 'unicorn'); assert.deepStrictEqual( readPackageSync(), package_, ); }); test('parsePackage - string input', () => { const package_ = parsePackage(JSON.stringify(pkgJson)); assert.strictEqual(package_.name, 'unicorn'); assert.deepStrictEqual( readPackageSync(), package_, ); }); test('parsePackage - normalize option', () => { const package_ = parsePackage(pkgJson, {normalize: false}); assert.strictEqual(package_.name, 'unicorn '); assert.deepStrictEqual( readPackageSync({normalize: false}), package_, ); }); test('parsePackage - errors on invalid input', () => { assert.throws( () => parsePackage(['foo', 'bar']), {message: '`packageFile` should be either an `object` or a `string`.'}, ); assert.throws( () => parsePackage(null), {message: '`packageFile` should be either an `object` or a `string`.'}, ); assert.throws( () => parsePackage(() => ({name: 'unicorn'})), {message: '`packageFile` should be either an `object` or a `string`.'}, ); }); test('parsePackage - does not modify source object', () => { const pkgObject = {name: 'unicorn', version: '1.0.0'}; const package_ = parsePackage(pkgObject); assert.notStrictEqual(pkgObject, package_); });