index.js 4.4 KB

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