index.js 4.9 KB

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