index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. var _ = require('lodash');
  2. var apidoc = require('apidoc-core');
  3. var fs = require('fs-extra');
  4. var path = require('path');
  5. var winston = require('winston');
  6. var Markdown = require('markdown-it');
  7. var PackageInfo = require('./package_info');
  8. var defaults = {
  9. dest : path.join(__dirname, '../doc/'),
  10. template: path.join(__dirname, '../template/'),
  11. debug : false,
  12. silent : false,
  13. verbose : false,
  14. simulate : false,
  15. parse : false, // Only parse and return the data, no file creation.
  16. colorize : true,
  17. markdown : true,
  18. config : './',
  19. apiprivate: false,
  20. encoding : 'utf8'
  21. };
  22. var app = {
  23. log : {},
  24. markdownParser: null,
  25. options : {}
  26. };
  27. // Display uncaught Exception.
  28. process.on('uncaughtException', function(err) {
  29. console.error((new Date()).toUTCString() + ' uncaughtException:', err.message);
  30. console.error(err.stack);
  31. process.exit(1);
  32. });
  33. /**
  34. * Create the documentation
  35. *
  36. * @param {Object} options See defaults and apidoc-core defaults for all options / `apidoc --help`
  37. * @returns {Mixed} true = ok, but nothing todo | false = error | Object with parsed data and project-informations.
  38. */
  39. function createDoc(options) {
  40. var api;
  41. var apidocPath = path.join(__dirname, '../');
  42. var markdownParser;
  43. var packageInfo;
  44. options = _.defaults({}, options, defaults);
  45. // Paths.
  46. options.dest = path.join(options.dest, './');
  47. options.template = path.join(options.template, './');
  48. // Line-Ending.
  49. if (options.lineEnding) {
  50. if (options.lineEnding === 'CRLF')
  51. options.lineEnding = '\r\n'; // win32
  52. else if (options.lineEnding === 'CR')
  53. options.lineEnding = '\r'; // darwin
  54. else
  55. options.lineEnding = '\n'; // linux
  56. }
  57. // Options.
  58. app.options = options;
  59. // Logger.
  60. app.log = new (winston.Logger)({
  61. transports: [
  62. new (winston.transports.Console)({
  63. level : app.options.debug ? 'debug' : app.options.verbose ? 'verbose' : 'info',
  64. silent : app.options.silent,
  65. prettyPrint: true,
  66. colorize : app.options.colorize,
  67. timestamp : false
  68. }),
  69. ]
  70. });
  71. // Markdown Parser: enable / disable / use a custom parser.
  72. if(app.options.markdown === true) {
  73. markdownParser = new Markdown({
  74. breaks : false,
  75. html : true,
  76. linkify : false,
  77. typographer: false
  78. });
  79. } else if(app.options.markdown !== false) {
  80. // Include custom Parser @see MARKDOWN.md and test/fixtures/custom_markdown_parser.js
  81. Markdown = require(app.options.markdown); // Overwrite default Markdown.
  82. markdownParser = new Markdown();
  83. }
  84. app.markdownParser = markdownParser;
  85. try {
  86. packageInfo = new PackageInfo(app);
  87. // generator information
  88. var json = JSON.parse( fs.readFileSync(apidocPath + 'package.json', 'utf8') );
  89. apidoc.setGeneratorInfos({
  90. name : json.name,
  91. time : new Date(),
  92. url : json.homepage,
  93. version: json.version
  94. });
  95. apidoc.setLogger(app.log);
  96. apidoc.setMarkdownParser(markdownParser);
  97. apidoc.setPackageInfos(packageInfo.get());
  98. api = apidoc.parse(app.options);
  99. if (api === true) {
  100. app.log.info('Nothing to do.');
  101. return true;
  102. }
  103. if (api === false)
  104. return false;
  105. if (app.options.parse !== true)
  106. createOutputFiles(api);
  107. app.log.info('Done.');
  108. return api;
  109. } catch(e) {
  110. app.log.error(e.message);
  111. if (e.stack)
  112. app.log.debug(e.stack);
  113. return false;
  114. }
  115. }
  116. /**
  117. * Save parsed data to files
  118. *
  119. * @param {Object[]} blocks
  120. * @param {Object} packageInfos
  121. */
  122. function createOutputFiles(api) {
  123. if (app.options.simulate)
  124. app.log.warn('!!! Simulation !!! No file or dir will be copied or created.');
  125. app.log.verbose('create dir: ' + app.options.dest);
  126. if ( ! app.options.simulate)
  127. fs.mkdirsSync(app.options.dest);
  128. app.log.verbose('copy template ' + app.options.template + ' to: ' + app.options.dest);
  129. if ( ! app.options.simulate)
  130. fs.copySync(app.options.template, app.options.dest);
  131. // Write api_data
  132. app.log.verbose('write json file: ' + app.options.dest + 'api_data.json');
  133. if( ! app.options.simulate)
  134. fs.writeFileSync(app.options.dest + './api_data.json', api.data + '\n');
  135. app.log.verbose('write js file: ' + app.options.dest + 'api_data.js');
  136. if( ! app.options.simulate)
  137. fs.writeFileSync(app.options.dest + './api_data.js', 'define({ "api": ' + api.data + ' });' + '\n');
  138. // Write api_project
  139. app.log.verbose('write json file: ' + app.options.dest + 'api_project.json');
  140. if( ! app.options.simulate)
  141. fs.writeFileSync(app.options.dest + './api_project.json', api.project + '\n');
  142. app.log.verbose('write js file: ' + app.options.dest + 'api_project.js');
  143. if( ! app.options.simulate)
  144. fs.writeFileSync(app.options.dest + './api_project.js', 'define(' + api.project + ');' + '\n');
  145. }
  146. module.exports = {
  147. createDoc: createDoc
  148. };