index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * apidoc
  3. * https://apidocjs.com
  4. *
  5. * Authors:
  6. * Peter Rottmann <rottmann@inveris.de>
  7. * Nicolas CARPi @ Deltablot
  8. * Copyright (c) 2013 inveris OHG
  9. * Licensed under the MIT license.
  10. */
  11. const core = require('./core/index');
  12. const defaults = require('./defaults');
  13. const optionsProcessor = require('./options');
  14. const { Reader } = require('./reader');
  15. const Writer = require('./writer');
  16. const app = {
  17. log: {},
  18. markdownParser: null,
  19. options: {},
  20. };
  21. // Display uncaught Exception.
  22. process.on('uncaughtException', err => {
  23. console.error(new Date().toUTCString() + ' uncaughtException:', err.message);
  24. console.error(err.stack);
  25. process.exit(1);
  26. });
  27. /**
  28. * Create the documentation
  29. *
  30. * @param {Object} options See `apidoc --help`
  31. * @returns {Boolean|Object} true = ok, but nothing todo | false = error | Object with parsed data and project-informations.
  32. */
  33. function createDoc (options) {
  34. // process options
  35. try {
  36. app.options = optionsProcessor.process(options);
  37. } catch (e) {
  38. console.error(e.message);
  39. process.exit(1);
  40. }
  41. // get the default logger
  42. app.log = defaults.getLogger(app.options);
  43. // if --warn-error was passed, treat warnings as error and exit with error code
  44. if (options.warnError) {
  45. app.log.warn = msg => {
  46. app.log.error(msg);
  47. process.exit(1);
  48. };
  49. }
  50. // get the markdown parser
  51. app.markdownParser = defaults.getMarkdownParser(app.options);
  52. // make sure input is an array
  53. if (typeof app.options.src === 'string') {
  54. app.log.warn('Provided "src" option is not an array. Converting it to array.');
  55. app.options.src = [app.options.src];
  56. }
  57. try {
  58. // generator information
  59. const pkgjson = require('../package.json');
  60. core.setGeneratorInfos({
  61. name: pkgjson.name,
  62. time: new Date().toString(),
  63. url: pkgjson.homepage,
  64. version: pkgjson.version,
  65. });
  66. core.setLogger(app.log);
  67. core.setMarkdownParser(app.markdownParser);
  68. const reader = new Reader(app);
  69. core.setPackageInfos(reader.read());
  70. // this is holding our results from parsing the source code
  71. const api = core.parse(app.options);
  72. if (api === true) {
  73. app.log.info('Nothing to do.');
  74. return true;
  75. }
  76. if (api === false) {
  77. app.log.error('Error during source code parsing!');
  78. return false;
  79. }
  80. const writer = new Writer(api, app);
  81. writer.write().then(() => app.log.verbose('All done :)'));
  82. return api;
  83. } catch (e) {
  84. app.log.error(e.message);
  85. if (e.stack) { app.log.debug(e.stack); }
  86. return false;
  87. }
  88. }
  89. module.exports = {
  90. createDoc: createDoc,
  91. };