{"name":"pkg-types","version":"2.3.2","license":"MIT","_id":"pkg-types@2.3.2","maintainers":[{"name":"pi0","email":"pyapar@gmail.com"},{"name":"danielroe","email":"daniel@roe.dev"}],"dist":{"shasum":"e8495ce3f8e41987cb3298c3d4163a8b30719cb5","tarball":"http://registry-npm.ywwl.com/pkg-types/download/pkg-types-2.3.2.tgz","fileCount":5,"integrity":"sha512-v0sVXzj7oPGysr543YYZLYbcJNJsKikSsp/fFzoxQ12ewY3ZZr7oCPC8y7OlmxfYB3QPvriXmuPD8KZggE1vqg==","signatures":[{"sig":"MEUCIQDZdPlhB7L4k0L6qGVFZd/TS0zFyWG8wol6Y+gcCksZtQIgTY33/WOqwO6tTrn/X/h16VS18DYgMGZ6B9Wj0Zp5ItQ=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"sig":"MEQCIBaZkIMNPl9wjqAaEOdUqj93JLrQPGRxSj7jeN9rJLzhAiAykyp5vytnAVexvg4T9DPESU9Rq6uxE8OaWqLrfTmPIQ==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":49656,"size":14061,"noattachment":false,"key":"/pkg-types/-/pkg-types-2.3.2.tgz"},"main":"./dist/index.mjs","types":"./dist/index.d.mts","exports":{".":"./dist/index.mjs"},"scripts":{"dev":"vitest --typecheck","fmt":"automd && oxlint --fix . && oxfmt","lint":"oxlint && oxfmt --check .","test":"pnpm lint && pnpm typecheck","build":"obuild","release":"pnpm test && pnpm build && changelogen --release && pnpm publish && git push --follow-tags","typecheck":"tsc --noEmit --skipLibCheck"},"_npmUser":{"name":"pi0","email":"pyapar@gmail.com"},"repository":{"url":"unjs/pkg-types","type":"git"},"description":"Node.js utilities and TypeScript definitions for `package.json` and `tsconfig.json`","directories":{},"sideEffects":false,"_nodeVersion":"24.20.0","dependencies":{"pathe":"^2.0.3","confbox":"^0.3.0","exsolve":"^1.1.1"},"_hasShrinkwrap":false,"devDependencies":{"oxfmt":">=0.65.0","automd":"^0.4.3","obuild":"^0.4.39","oxlint":"^1.81.0","vitest":"^4.1.11","prettier":"^3.9.6","typescript":"^7","@types/node":"^26","changelogen":"^0.6.2","expect-type":"^1.4.0","eslint-config-unjs":"^0.6.2","@vitest/coverage-v8":"^4.1.11"},"_npmOperationalInternal":{"tmp":"tmp/pkg-types_2.3.2_1788426704349_0.01569328296059669","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":1788426704461,"_cnpm_publish_time":1788426704461,"dist-tags":{"latest":"2.4.0"}}