package.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. const logger = require('jsdoc/util/logger');
  2. const stripBom = require('jsdoc/util/stripbom');
  3. /**
  4. * Provides access to information about a JavaScript package.
  5. *
  6. * @module jsdoc/package
  7. * @see https://www.npmjs.org/doc/files/package.json.html
  8. */
  9. // Collect all of the license information from a `package.json` file.
  10. function getLicenses(packageInfo) {
  11. const licenses = packageInfo.licenses ? packageInfo.licenses.slice(0) : [];
  12. if (packageInfo.license) {
  13. licenses.push({ type: packageInfo.license });
  14. }
  15. return licenses;
  16. }
  17. /**
  18. * Information about where to report bugs in the package.
  19. *
  20. * @typedef {Object} module:jsdoc/package.Package~BugInfo
  21. * @property {string} email - The email address for reporting bugs.
  22. * @property {string} url - The URL for reporting bugs.
  23. */
  24. /**
  25. * Information about a package's software license.
  26. *
  27. * @typedef {Object} module:jsdoc/package.Package~LicenseInfo
  28. * @property {string} type - An identifier for the type of license.
  29. * @property {string} url - The URL for the complete text of the license.
  30. */
  31. /**
  32. * Information about a package author or contributor.
  33. *
  34. * @typedef {Object} module:jsdoc/package.Package~PersonInfo
  35. * @property {string} name - The person's full name.
  36. * @property {string} email - The person's email address.
  37. * @property {string} url - The URL of the person's website.
  38. */
  39. /**
  40. * Information about a package's version-control repository.
  41. *
  42. * @typedef {Object} module:jsdoc/package.Package~RepositoryInfo
  43. * @property {string} type - The type of version-control system that the repository uses (for
  44. * example, `git` or `svn`).
  45. * @property {string} url - The URL for the repository.
  46. */
  47. /**
  48. * Information about a JavaScript package. JSDoc can extract package information from
  49. * `package.json` files that follow the
  50. * [npm specification](https://www.npmjs.org/doc/files/package.json.html).
  51. *
  52. * **Note**: JSDoc does not validate or normalize the contents of `package.json` files. If your
  53. * `package.json` file does not follow the npm specification, some properties of the `Package`
  54. * object may not use the format documented here.
  55. */
  56. class Package {
  57. /**
  58. * @param {string} json - The contents of the `package.json` file.
  59. */
  60. constructor(json) {
  61. let packageInfo;
  62. /**
  63. * The string identifier that is shared by all `Package` objects.
  64. *
  65. * @readonly
  66. * @default
  67. * @type {string}
  68. */
  69. this.kind = 'package';
  70. try {
  71. packageInfo = JSON.parse(stripBom.strip(json) || '{}');
  72. }
  73. catch (e) {
  74. logger.error('Unable to parse the package file: %s', e.message);
  75. packageInfo = {};
  76. }
  77. if (packageInfo.name) {
  78. /**
  79. * The package name.
  80. *
  81. * @type {string}
  82. */
  83. this.name = packageInfo.name;
  84. }
  85. /**
  86. * The unique longname for this `Package` object.
  87. *
  88. * @type {string}
  89. */
  90. this.longname = `${this.kind}:${this.name}`;
  91. if (packageInfo.author) {
  92. /**
  93. * The author of this package. Contains either a
  94. * {@link module:jsdoc/package.Package~PersonInfo PersonInfo} object or a string with
  95. * information about the author.
  96. *
  97. * @type {(module:jsdoc/package.Package~PersonInfo|string)}
  98. * @since 3.3.0
  99. */
  100. this.author = packageInfo.author;
  101. }
  102. if (packageInfo.bugs) {
  103. /**
  104. * Information about where to report bugs in the project. May contain a URL, a string, or an
  105. * object with more detailed information.
  106. *
  107. * @type {(string|module:jsdoc/package.Package~BugInfo)}
  108. * @since 3.3.0
  109. */
  110. this.bugs = packageInfo.bugs;
  111. }
  112. if (packageInfo.contributors) {
  113. /**
  114. * The contributors to this package.
  115. *
  116. * @type {Array.<(module:jsdoc/package.Package~PersonInfo|string)>}
  117. * @since 3.3.0
  118. */
  119. this.contributors = packageInfo.contributors;
  120. }
  121. if (packageInfo.dependencies) {
  122. /**
  123. * The dependencies for this package.
  124. *
  125. * @type {Object}
  126. * @since 3.3.0
  127. */
  128. this.dependencies = packageInfo.dependencies;
  129. }
  130. if (packageInfo.description) {
  131. /**
  132. * A brief description of the package.
  133. *
  134. * @type {string}
  135. */
  136. this.description = packageInfo.description;
  137. }
  138. if (packageInfo.devDependencies) {
  139. /**
  140. * The development dependencies for this package.
  141. *
  142. * @type {Object}
  143. * @since 3.3.0
  144. */
  145. this.devDependencies = packageInfo.devDependencies;
  146. }
  147. if (packageInfo.engines) {
  148. /**
  149. * The JavaScript engines that this package supports. Each key is a string that identifies
  150. * the engine (for example, `node`). Each value is a
  151. * [semver](https://www.npmjs.org/doc/misc/semver.html)-compliant version number for the
  152. * engine.
  153. *
  154. * @type {Object}
  155. * @since 3.3.0
  156. */
  157. this.engines = packageInfo.engines;
  158. }
  159. /**
  160. * The source files associated with the package.
  161. *
  162. * New `Package` objects always contain an empty array, regardless of whether the `package.json`
  163. * file includes a `files` property.
  164. *
  165. * After JSDoc parses your input files, it sets this property to a list of paths to your input
  166. * files.
  167. *
  168. * @type {Array.<string>}
  169. */
  170. this.files = [];
  171. if (packageInfo.homepage) {
  172. /**
  173. * The URL for the package's homepage.
  174. *
  175. * @type {string}
  176. * @since 3.3.0
  177. */
  178. this.homepage = packageInfo.homepage;
  179. }
  180. if (packageInfo.keywords) {
  181. /**
  182. * Keywords to help users find the package.
  183. *
  184. * @type {Array.<string>}
  185. * @since 3.3.0
  186. */
  187. this.keywords = packageInfo.keywords;
  188. }
  189. if (packageInfo.license || packageInfo.licenses) {
  190. /**
  191. * The licenses used by this package. Combines information from the `package.json` file's
  192. * `license` property and the deprecated `licenses` property.
  193. *
  194. * @type {Array.<module:jsdoc/package.Package~LicenseInfo>}
  195. */
  196. this.licenses = getLicenses(packageInfo);
  197. }
  198. if (packageInfo.main) {
  199. /**
  200. * The module ID that provides the primary entry point to the package. For example, if your
  201. * package is a CommonJS module, and the value of this property is `foo`, users should be
  202. * able to load your module with `require('foo')`.
  203. *
  204. * @type {string}
  205. * @since 3.3.0
  206. */
  207. this.main = packageInfo.main;
  208. }
  209. if (packageInfo.repository) {
  210. /**
  211. * The version-control repository for the package.
  212. *
  213. * @type {module:jsdoc/package.Package~RepositoryInfo}
  214. * @since 3.3.0
  215. */
  216. this.repository = packageInfo.repository;
  217. }
  218. if (packageInfo.version) {
  219. /**
  220. * The [semver](https://www.npmjs.org/doc/misc/semver.html)-compliant version number of the
  221. * package.
  222. *
  223. * @type {string}
  224. * @since 3.2.0
  225. */
  226. this.version = packageInfo.version;
  227. }
  228. }
  229. }
  230. exports.Package = Package;