{"name":"pkg-types","version":"2.3.0","license":"MIT","_id":"pkg-types@2.3.0","maintainers":[{"name":"pi0","email":"pyapar@gmail.com"},{"name":"danielroe","email":"daniel@roe.dev"}],"homepage":"https://github.com/unjs/pkg-types#readme","bugs":{"url":"https://github.com/unjs/pkg-types/issues"},"dist":{"shasum":"037f2c19bd5402966ff6810e32706558cb5b5726","tarball":"http://registry-npm.ywwl.com/pkg-types/download/pkg-types-2.3.0.tgz","fileCount":5,"integrity":"sha512-SIqCzDRg0s9npO5XQ3tNZioRY1uK06lA41ynBC1YmFTmnY6FjUjVt6s4LoADmwoig1qqD0oK8h1p/8mlMx8Oig==","signatures":[{"sig":"MEUCIQCL8jvlK/XET2El8akHJiDpHaRAvJ/+8oY/WE1y0noHvAIgbcqtlTMBVFhKGjYq2sDJ3sFXoEvs8ORqjkd9kmn3V8w=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":44078,"size":12037,"noattachment":false,"key":"/pkg-types/-/pkg-types-2.3.0.tgz"},"types":"./dist/index.d.mts","exports":{"types":"./dist/index.d.mts","default":"./dist/index.mjs"},"gitHead":"7b781ed7cdcb22c7232a57654a5d42ebab004d3c","scripts":{"dev":"vitest --typecheck","lint":"eslint && prettier -c src test","test":"vitest run --typecheck --coverage","build":"unbuild","prepack":"pnpm build","release":"pnpm test && changelogen --release && npm publish && git push --follow-tags","lint:fix":"automd && eslint --fix . && prettier -w src test"},"_npmUser":{"name":"pi0","email":"pyapar@gmail.com"},"repository":{"url":"git+https://github.com/unjs/pkg-types.git","type":"git"},"_npmVersion":"10.9.2","description":"Node.js utilities and TypeScript definitions for `package.json` and `tsconfig.json`","directories":{},"sideEffects":false,"_nodeVersion":"22.14.0","dependencies":{"pathe":"^2.0.3","confbox":"^0.2.2","exsolve":"^1.0.7"},"_hasShrinkwrap":false,"packageManager":"pnpm@10.14.0","devDependencies":{"jiti":"^2.5.1","automd":"^0.4.0","eslint":"^9.33.0","vitest":"^3.2.4","unbuild":"^3.6.1","prettier":"^3.6.2","typescript":"^5.9.2","@types/node":"^24.3.0","changelogen":"^0.6.2","expect-type":"^1.2.2","eslint-config-unjs":"^0.5.0","@vitest/coverage-v8":"^3.2.4"},"_npmOperationalInternal":{"tmp":"tmp/pkg-types_2.3.0_1755643910908_0.8690611657965148","host":"s3://npm-registry-packages-npm-production"},"readme":"# pkg-types\n\n<!-- automd:badges color=yellow codecov -->\n\n[![npm version](https://img.shields.io/npm/v/pkg-types?color=yellow)](https://npmjs.com/package/pkg-types)\n[![npm downloads](https://img.shields.io/npm/dm/pkg-types?color=yellow)](https://npm.chart.dev/pkg-types)\n[![codecov](https://img.shields.io/codecov/c/gh/unjs/pkg-types?color=yellow)](https://codecov.io/gh/unjs/pkg-types)\n\n<!-- /automd -->\n\nNode.js utilities and TypeScript definitions for `package.json`, `tsconfig.json`, and other configuration files.\n\n## Install\n\n<!-- automd:pm-i -->\n\n```sh\n# ✨ Auto-detect\nnpx nypm install pkg-types\n\n# npm\nnpm install pkg-types\n\n# yarn\nyarn add pkg-types\n\n# pnpm\npnpm add pkg-types\n\n# bun\nbun install pkg-types\n\n# deno\ndeno install npm:pkg-types\n```\n\n<!-- /automd -->\n\n## Usage\n\n### Package Configuration\n\n#### `readPackage`\n\nReads any package file format (package.json, package.json5, or package.yaml) with automatic format detection.\n\n```js\nimport { readPackage } from \"pkg-types\";\nconst localPackage = await readPackage();\n// or\nconst pkg = await readPackage(\"/fully/resolved/path/to/folder\");\n```\n\n#### `writePackage`\n\nWrites package data with format detection based on file extension.\n\n```js\nimport { writePackage } from \"pkg-types\";\n\nawait writePackage(\"path/to/package.json\", pkg);\nawait writePackage(\"path/to/package.json5\", pkg);\nawait writePackage(\"path/to/package.yaml\", pkg);\n```\n\n#### `findPackage`\n\nFinds the nearest package file (package.json, package.json5, or package.yaml).\n\n```js\nimport { findPackage } from \"pkg-types\";\nconst filename = await findPackage();\n// or\nconst filename = await findPackage(\"/fully/resolved/path/to/folder\");\n```\n\n#### `readPackageJSON`\n\n```js\nimport { readPackageJSON } from \"pkg-types\";\nconst localPackageJson = await readPackageJSON();\n// or\nconst packageJson = await readPackageJSON(\"/fully/resolved/path/to/folder\");\n```\n\n#### `writePackageJSON`\n\n```js\nimport { writePackageJSON } from \"pkg-types\";\n\nawait writePackageJSON(\"path/to/package.json\", pkg);\n```\n\n#### `resolvePackageJSON`\n\n```js\nimport { resolvePackageJSON } from \"pkg-types\";\nconst filename = await resolvePackageJSON();\n// or\nconst packageJson = await resolvePackageJSON(\"/fully/resolved/path/to/folder\");\n```\n\n#### `updatePackage`\n\nReads a package file and passes a proxied PackageJson to a callback (the callback may mutate it in-place or return a new object). The updated package is then written back using the same file format (`.json`/`.json5`/`.yaml`). The proxy auto-creates common map fields (e.g. `scripts`, `dependencies`) when accessed.\n\n```js\nimport { updatePackage } from \"pkg-types\";\n\nawait updatePackage(\"path/to/package\", (pkg) => {\n  pkg.version = \"1.0.1\";\n  pkg.dependencies.lodash = \"^4.17.21\";\n});\n```\n\n#### `sortPackage`\n\nReturns a new PackageJson that reorders known top-level fields according to the convention and alphabetically sorts certain nested maps (like `dependencies`, `devDependencies`, `optionalDependencies`, `peerDependencies` and `scripts`). Unknown top-level keys retain their original relative order. The input object is not mutated.\n\n```js\nimport { sortPackage } from \"pkg-types\";\n\nconst sorted = sortPackage(pkg);\n```\n\n#### `normalizePackage`\n\nNormalizes a `PackageJson` for stable output: sorts top-level fields and dependency maps, and removes dependency fields (`dependencies`, `devDependencies`, `optionalDependencies`, `peerDependencies`) if they are not plain objects. Returns a new normalized object.\n\n```js\nimport { normalizePackage } from \"pkg-types\";\n\nconst normalized = normalizePackage(pkg);\n```\n\n### TypeScript Configuration\n\n#### `readTSConfig`\n\n```js\nimport { readTSConfig } from \"pkg-types\";\nconst tsconfig = await readTSConfig();\n// or\nconst tsconfig2 = await readTSConfig(\"/fully/resolved/path/to/folder\");\n```\n\n#### `writeTSConfig`\n\n```js\nimport { writeTSConfig } from \"pkg-types\";\n\nawait writeTSConfig(\"path/to/tsconfig.json\", tsconfig);\n```\n\n#### `resolveTSConfig`\n\n```js\nimport { resolveTSConfig } from \"pkg-types\";\nconst filename = await resolveTSConfig();\n// or\nconst tsconfig = await resolveTSConfig(\"/fully/resolved/path/to/folder\");\n```\n\n### File Resolution\n\n#### `findFile`\n\n```js\nimport { findFile } from \"pkg-types\";\nconst filename = await findFile(\"README.md\", {\n  startingFrom: id,\n  rootPattern: /^node_modules$/,\n  test: (filename) => filename.endsWith(\".md\"),\n});\n```\n\n#### `findNearestFile`\n\n```js\nimport { findNearestFile } from \"pkg-types\";\nconst filename = await findNearestFile(\"package.json\");\n```\n\n#### `findFarthestFile`\n\n```js\nimport { findFarthestFile } from \"pkg-types\";\nconst filename = await findFarthestFile(\"package.json\");\n```\n\n#### `resolveLockfile`\n\nFind path to the lock file (`yarn.lock`, `package-lock.json`, `pnpm-lock.yaml`, `npm-shrinkwrap.json`, `bun.lockb`, `bun.lock`, `deno.lock`) or throws an error.\n\n```js\nimport { resolveLockfile } from \"pkg-types\";\nconst lockfile = await resolveLockfile(\".\");\n```\n\n#### `findWorkspaceDir`\n\nTry to detect workspace dir by in order:\n\n1. Farthest workspace file (`pnpm-workspace.yaml`, `lerna.json`, `turbo.json`, `rush.json`, `deno.json`, `deno.jsonc`)\n2. Closest `.git/config` file\n3. Farthest lockfile\n4. Farthest `package.json` file\n\nIf fails, throws an error.\n\n```js\nimport { findWorkspaceDir } from \"pkg-types\";\nconst workspaceDir = await findWorkspaceDir(\".\");\n```\n\n### Git Configuration\n\n#### `resolveGitConfig`\n\nFinds closest `.git/config` file.\n\n```js\nimport { resolveGitConfig } from \"pkg-types\";\n\nconst gitConfig = await resolveGitConfig(\".\");\n```\n\n#### `readGitConfig`\n\nFinds and reads closest `.git/config` file into a JS object.\n\n```js\nimport { readGitConfig } from \"pkg-types\";\n\nconst gitConfigObj = await readGitConfig(\".\");\n```\n\n#### `writeGitConfig`\n\nStringifies git config object into INI text format and writes it to a file.\n\n```js\nimport { writeGitConfig } from \"pkg-types\";\n\nawait writeGitConfig(\".git/config\", gitConfigObj);\n```\n\n#### `parseGitConfig`\n\nParses a git config file in INI text format into a JavaScript object.\n\n```js\nimport { parseGitConfig } from \"pkg-types\";\n\nconst gitConfigObj = parseGitConfig(gitConfigINI);\n```\n\n#### `stringifyGitConfig`\n\nStringifies a git config object into a git config file INI text format.\n\n```js\nimport { stringifyGitConfig } from \"pkg-types\";\n\nconst gitConfigINI = stringifyGitConfig(gitConfigObj);\n```\n\n## Types\n\n- **Note:** In order to make types work, you need to install `typescript` as a devDependency.\n\nYou can directly use typed interfaces:\n\n```ts\nimport type { TSConfig, PackageJson, GitConfig } from \"pkg-types\";\n```\n\n### Define Utilities\n\nYou can use define utilities for type support and auto-completion when working in plain `.js` files. These functions simply return the input object but provide TypeScript type hints.\n\n#### `definePackageJSON`\n\nProvides type safety and auto-completion for package.json objects.\n\n```js\nimport { definePackageJSON } from \"pkg-types\";\n\nconst pkg = definePackageJSON({\n  name: \"my-package\",\n  version: \"1.0.0\",\n  // TypeScript will provide auto-completion here\n});\n```\n\n#### `defineTSConfig`\n\nProvides type safety and auto-completion for tsconfig.json objects.\n\n```js\nimport { defineTSConfig } from \"pkg-types\";\n\nconst tsconfig = defineTSConfig({\n  compilerOptions: {\n    target: \"ES2020\",\n    // TypeScript will provide auto-completion here\n  },\n});\n```\n\n#### `defineGitConfig`\n\nProvides type safety and auto-completion for git config objects.\n\n```js\nimport { defineGitConfig } from \"pkg-types\";\n\nconst gitConfig = defineGitConfig({\n  user: {\n    name: \"John Doe\",\n    email: \"john@example.com\",\n  },\n  // TypeScript will provide auto-completion here\n});\n```\n\n## Alternatives\n\n- [dominikg/tsconfck](https://github.com/dominikg/tsconfck)\n\n## License\n\n<!-- automd:contributors license=MIT author=\"pi0,danielroe\" -->\n\nPublished under the [MIT](https://github.com/unjs/pkg-types/blob/main/LICENSE) license.\nMade by [@pi0](https://github.com/pi0), [@danielroe](https://github.com/danielroe) and [community](https://github.com/unjs/pkg-types/graphs/contributors) 💛\n<br><br>\n<a href=\"https://github.com/unjs/pkg-types/graphs/contributors\">\n<img src=\"https://contrib.rocks/image?repo=unjs/pkg-types\" />\n</a>\n\n<!-- /automd -->\n","publish_time":1755643911134,"_cnpm_publish_time":1755643911134,"dist-tags":{"latest":"2.4.0"}}