24d01ebcbd68775681aef50c151b338e6b7f69942b9afe1e4a725e4973aea81ac9057d1f046ab873cab6e1cfe2307d7c4177dd5f88562fe9ae316e1b8fc690 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # pkg-types
  2. <!-- automd:badges color=yellow codecov -->
  3. [![npm version](https://img.shields.io/npm/v/pkg-types?color=yellow)](https://npmjs.com/package/pkg-types)
  4. [![npm downloads](https://img.shields.io/npm/dm/pkg-types?color=yellow)](https://npm.chart.dev/pkg-types)
  5. [![codecov](https://img.shields.io/codecov/c/gh/unjs/pkg-types?color=yellow)](https://codecov.io/gh/unjs/pkg-types)
  6. <!-- /automd -->
  7. Node.js utilities and TypeScript definitions for `package.json` and `tsconfig.json`.
  8. ## Install
  9. <!-- automd:pm-i -->
  10. ```sh
  11. # ✨ Auto-detect
  12. npx nypm install pkg-types
  13. # npm
  14. npm install pkg-types
  15. # yarn
  16. yarn add pkg-types
  17. # pnpm
  18. pnpm install pkg-types
  19. # bun
  20. bun install pkg-types
  21. # deno
  22. deno install pkg-types
  23. ```
  24. <!-- /automd -->
  25. ## Usage
  26. ### `readPackageJSON`
  27. ```js
  28. import { readPackageJSON } from "pkg-types";
  29. const localPackageJson = await readPackageJSON();
  30. // or
  31. const packageJson = await readPackageJSON("/fully/resolved/path/to/folder");
  32. ```
  33. ### `writePackageJSON`
  34. ```js
  35. import { writePackageJSON } from "pkg-types";
  36. await writePackageJSON("path/to/package.json", pkg);
  37. ```
  38. ### `resolvePackageJSON`
  39. ```js
  40. import { resolvePackageJSON } from "pkg-types";
  41. const filename = await resolvePackageJSON();
  42. // or
  43. const packageJson = await resolvePackageJSON("/fully/resolved/path/to/folder");
  44. ```
  45. ### `readTSConfig`
  46. ```js
  47. import { readTSConfig } from "pkg-types";
  48. const tsconfig = await readTSConfig();
  49. // or
  50. const tsconfig2 = await readTSConfig("/fully/resolved/path/to/folder");
  51. ```
  52. ### `writeTSConfig`
  53. ```js
  54. import { writeTSConfig } from "pkg-types";
  55. await writeTSConfig("path/to/tsconfig.json", tsconfig);
  56. ```
  57. ### `resolveTSConfig`
  58. ```js
  59. import { resolveTSConfig } from "pkg-types";
  60. const filename = await resolveTSConfig();
  61. // or
  62. const tsconfig = await resolveTSConfig("/fully/resolved/path/to/folder");
  63. ```
  64. ### `resolveFile`
  65. ```js
  66. import { resolveFile } from "pkg-types";
  67. const filename = await resolveFile("README.md", {
  68. startingFrom: id,
  69. rootPattern: /^node_modules$/,
  70. matcher: (filename) => filename.endsWith(".md"),
  71. });
  72. ```
  73. ### `resolveLockFile`
  74. Find path to the lock file (`yarn.lock`, `package-lock.json`, `pnpm-lock.yaml`, `npm-shrinkwrap.json`) or throws an error.
  75. ```js
  76. import { resolveLockFile } from "pkg-types";
  77. const lockfile = await resolveLockFile(".");
  78. ```
  79. ### `findWorkspaceDir`
  80. Try to detect workspace dir by in order:
  81. 1. Nearest `.git` directory
  82. 2. Farthest lockfile
  83. 3. Farthest `package.json` file
  84. If fails, throws an error.
  85. ```js
  86. import { findWorkspaceDir } from "pkg-types";
  87. const workspaceDir = await findWorkspaceDir(".");
  88. ```
  89. ## Types
  90. **Note:** In order to make types working, you need to install `typescript` as a devDependency.
  91. You can directly use typed interfaces:
  92. ```ts
  93. import type { TSConfig, PackageJSON } from "pkg-types";
  94. ```
  95. You can also use define utils for type support for using in plain `.js` files and auto-complete in IDE.
  96. ```js
  97. import type { definePackageJSON } from 'pkg-types'
  98. const pkg = definePackageJSON({})
  99. ```
  100. ```js
  101. import type { defineTSConfig } from 'pkg-types'
  102. const pkg = defineTSConfig({})
  103. ```
  104. ## Alternatives
  105. - [dominikg/tsconfck](https://github.com/dominikg/tsconfck)
  106. ## License
  107. <!-- automd:contributors license=MIT author="pi0,danielroe" -->
  108. Published under the [MIT](https://github.com/unjs/pkg-types/blob/main/LICENSE) license.
  109. Made by [@pi0](https://github.com/pi0), [@danielroe](https://github.com/danielroe) and [community](https://github.com/unjs/pkg-types/graphs/contributors) 💛
  110. <br><br>
  111. <a href="https://github.com/unjs/pkg-types/graphs/contributors">
  112. <img src="https://contrib.rocks/image?repo=unjs/pkg-types" />
  113. </a>
  114. <!-- /automd -->