index.js 4.0 KB

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