index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { readFileSync } from 'node:fs';
  2. import { dirname, resolve, sep } from 'node:path';
  3. import { fileURLToPath } from 'node:url';
  4. const NM = `${sep}node_modules${sep}`;
  5. const STORE = `.store${sep}`;
  6. const PKG = `${sep}package${sep}`;
  7. const DIST = `${sep}dist${sep}`;
  8. /**
  9. * Find the package.json file, either from a TypeScript file somewhere not
  10. * in a 'dist' folder, or a built and/or installed 'dist' folder.
  11. *
  12. * Note: this *only* works if you build your code into `'./dist'`, and that the
  13. * source path does not also contain `'dist'`! If you don't build into
  14. * `'./dist'`, or if you have files at `./src/dist/dist.ts`, then this will
  15. * not work properly!
  16. *
  17. * The default `pathFromSrc` option assumes that the calling code lives one
  18. * folder below the root of the package. Otherwise, it must be specified.
  19. *
  20. * Example:
  21. *
  22. * ```ts
  23. * // src/index.ts
  24. * import { findPackageJson } from 'package-json-from-dist'
  25. *
  26. * const pj = findPackageJson(import.meta.url)
  27. * console.log(`package.json found at ${pj}`)
  28. * ```
  29. *
  30. * If the caller is deeper within the project source, then you must provide
  31. * the appropriate fallback path:
  32. *
  33. * ```ts
  34. * // src/components/something.ts
  35. * import { findPackageJson } from 'package-json-from-dist'
  36. *
  37. * const pj = findPackageJson(import.meta.url, '../../package.json')
  38. * console.log(`package.json found at ${pj}`)
  39. * ```
  40. *
  41. * When running from CommmonJS, use `__filename` instead of `import.meta.url`
  42. *
  43. * ```ts
  44. * // src/index.cts
  45. * import { findPackageJson } from 'package-json-from-dist'
  46. *
  47. * const pj = findPackageJson(__filename)
  48. * console.log(`package.json found at ${pj}`)
  49. * ```
  50. */
  51. export const findPackageJson = (from, pathFromSrc = '../package.json') => {
  52. const f = typeof from === 'object' || from.startsWith('file://') ?
  53. fileURLToPath(from)
  54. : from;
  55. const __dirname = dirname(f);
  56. const nms = __dirname.lastIndexOf(NM);
  57. if (nms !== -1) {
  58. // inside of node_modules. find the dist directly under package name.
  59. const nm = __dirname.substring(0, nms + NM.length);
  60. const pkgDir = __dirname.substring(nms + NM.length);
  61. // affordance for yarn berry, which puts package contents in
  62. // '.../node_modules/.store/${id}-${hash}/package/...'
  63. if (pkgDir.startsWith(STORE)) {
  64. const pkg = pkgDir.indexOf(PKG, STORE.length);
  65. if (pkg) {
  66. return resolve(nm, pkgDir.substring(0, pkg + PKG.length), 'package.json');
  67. }
  68. }
  69. const pkgName = pkgDir.startsWith('@') ?
  70. pkgDir.split(sep, 2).join(sep)
  71. : String(pkgDir.split(sep)[0]);
  72. return resolve(nm, pkgName, 'package.json');
  73. }
  74. else {
  75. // see if we are in a dist folder.
  76. const d = __dirname.lastIndexOf(DIST);
  77. if (d !== -1) {
  78. return resolve(__dirname.substring(0, d), 'package.json');
  79. }
  80. else {
  81. return resolve(__dirname, pathFromSrc);
  82. }
  83. }
  84. };
  85. /**
  86. * Load the package.json file, either from a TypeScript file somewhere not
  87. * in a 'dist' folder, or a built and/or installed 'dist' folder.
  88. *
  89. * Note: this *only* works if you build your code into `'./dist'`, and that the
  90. * source path does not also contain `'dist'`! If you don't build into
  91. * `'./dist'`, or if you have files at `./src/dist/dist.ts`, then this will
  92. * not work properly!
  93. *
  94. * The default `pathFromSrc` option assumes that the calling code lives one
  95. * folder below the root of the package. Otherwise, it must be specified.
  96. *
  97. * Example:
  98. *
  99. * ```ts
  100. * // src/index.ts
  101. * import { loadPackageJson } from 'package-json-from-dist'
  102. *
  103. * const pj = loadPackageJson(import.meta.url)
  104. * console.log(`Hello from ${pj.name}@${pj.version}`)
  105. * ```
  106. *
  107. * If the caller is deeper within the project source, then you must provide
  108. * the appropriate fallback path:
  109. *
  110. * ```ts
  111. * // src/components/something.ts
  112. * import { loadPackageJson } from 'package-json-from-dist'
  113. *
  114. * const pj = loadPackageJson(import.meta.url, '../../package.json')
  115. * console.log(`Hello from ${pj.name}@${pj.version}`)
  116. * ```
  117. *
  118. * When running from CommmonJS, use `__filename` instead of `import.meta.url`
  119. *
  120. * ```ts
  121. * // src/index.cts
  122. * import { loadPackageJson } from 'package-json-from-dist'
  123. *
  124. * const pj = loadPackageJson(__filename)
  125. * console.log(`Hello from ${pj.name}@${pj.version}`)
  126. * ```
  127. */
  128. export const loadPackageJson = (from, pathFromSrc = '../package.json') => JSON.parse(readFileSync(findPackageJson(from, pathFromSrc), 'utf8'));
  129. //# sourceMappingURL=index.js.map