apidoc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/usr/bin/env node
  2. 'use strict';
  3. /*
  4. * apidoc
  5. * https://apidocjs.com
  6. *
  7. * Authors:
  8. * Peter Rottmann <rottmann@inveris.de>
  9. * Nicolas CARPi @ Deltablot
  10. * Copyright (c) 2013 inveris OHG
  11. * Licensed under the MIT license.
  12. */
  13. const os = require('os');
  14. const path = require('path');
  15. // library for watch mode
  16. const nodemon = require('nodemon');
  17. // library to parse arguments/options
  18. const { Command } = require('commander');
  19. const apidoc = require(path.join(__dirname, '..', 'lib', 'index'));
  20. /**
  21. * Transform parameters to object
  22. *
  23. * @param {String|String[]} filters
  24. * @returns {Object}
  25. */
  26. function transformToObject (filters) {
  27. if (!filters) { return; }
  28. if (typeof filters === 'string') { filters = [filters]; }
  29. const result = {};
  30. filters.forEach(function (filter) {
  31. const splits = filter.split('=');
  32. if (splits.length === 2) {
  33. result[splits[0]] = path.resolve(splits[1], '');
  34. }
  35. });
  36. return result;
  37. }
  38. // init program
  39. const program = new Command();
  40. // set the version from package.json so it is available with --version
  41. const pkgjson = require(path.join('..', 'package.json'));
  42. program.version(pkgjson.version);
  43. program
  44. .option('-c, --config <config>', 'Path to config file (json or javascript).', '')
  45. .option('--no-color', 'Turn off log color.', false)
  46. .option('-d, --debug', 'Show debug messages.', false)
  47. .option('--definitions', 'Include definitions file rather than copying definitions.', false)
  48. .option('--encoding <encoding>', 'Set the encoding of the source code. [utf8].', 'utf8')
  49. .option('-e, --exclude-filters <exclude-filters...>', 'RegEx-Filter to select files / dirs that should not be parsed (many -e can be used). node_modules and apidoc.config.js are always excluded.', [])
  50. .option('-f --file-filters <file-filters...>', 'RegEx-Filter to select files that should be parsed (multiple -f can be used).', [])
  51. .option('--filter-by <tag-filter=value>', 'Filter documentation by tag', '')
  52. .option('-i, --input <input...>', 'Input/source dirname.', ['./src'])
  53. .option('--line-ending <line-ending>', 'Turn off autodetect line-ending. Allowed values: LF, CR, CRLF.', os.EOL)
  54. .option('--log-format <format>', 'Change log format. Allowed values: simple, json.', 'simple')
  55. .option('--markdown [markdown]', 'Turn off default markdown parser or set a file to a custom parser.', true)
  56. .option('-n, --dry-run', 'Parse source files but do not write any output files.', false)
  57. .option('-o, --output <output>', 'Output dirname.', 'doc/')
  58. .option('--write-json', 'Will create api-data.json file with parsed API info.', false)
  59. .option('--parse-filters <parse-filters...>', 'Optional user defined filters. Format name=filename', [])
  60. .option('--parse-languages <parse-languages...>', 'Optional user defined languages. Format name=filename', [])
  61. .option('--parse-parsers <parse-parsers...>', 'Optional user defined parsers. Format name=filename', [])
  62. .option('--parse-workers <parse-workers...>', 'Optional user defined workers. Format name=filename', [])
  63. .option('-p, --private', 'Include private APIs in output.', false)
  64. .option('-q, --quiet', 'Turn all output off.', false)
  65. .option('-S, --single', 'Output to single file.', false)
  66. .option('-t, --template <template>', 'Use template for output files.', path.join(__dirname, '../template/'))
  67. .option('-v, --verbose', 'Verbose output.', false)
  68. .option('--warn-error', 'Treat warnings as error and exit with error code.', false)
  69. .option('-w, --watch', 'Watch input files for changes to rebuild the docs.', false)
  70. .parse(process.argv);
  71. const argv = program.opts();
  72. const defaultIncludeFilters = ['.*\\.(clj|cls|coffee|cpp|cs|dart|erl|exs?|go|groovy|ino?|java|js|jsx|kt|litcoffee|lua|mjs|p|php?|pl|pm|py|rb|scala|ts|tsx|vue)$'];
  73. const options = {
  74. excludeFilters: ['apidoc.config.js', 'node_modules'].concat(argv.excludeFilters.length ? argv.excludeFilters : []),
  75. includeFilters: argv.fileFilters.length ? argv.fileFilters : defaultIncludeFilters,
  76. src: argv.input,
  77. dest: argv.output,
  78. template: argv.template,
  79. config: argv.config,
  80. apiprivate: argv.private,
  81. verbose: argv.verbose,
  82. single: argv.single,
  83. debug: argv.debug,
  84. colorize: argv.color,
  85. filters: transformToObject(argv.parseFilters),
  86. languages: transformToObject(argv.parseLanguages),
  87. parsers: transformToObject(argv.parseParsers),
  88. workers: transformToObject(argv.parseWorkers),
  89. silent: argv.quiet,
  90. dryRun: argv.dryRun,
  91. markdown: argv.markdown,
  92. lineEnding: argv.lineEnding,
  93. encoding: argv.encoding,
  94. copyDefinitions: !argv.definitions,
  95. filterBy: argv.filterBy,
  96. logFormat: argv.logFormat,
  97. warnError: argv.warnError,
  98. writeJson: argv.writeJson,
  99. };
  100. if (options.debug) {
  101. console.debug('[debug] Parsed options:\n');
  102. console.debug(options);
  103. }
  104. // display ascii art in verbose mode (but not for json format)
  105. if (options.verbose && options.logFormat === 'simple') {
  106. const asciiArt = `
  107. _ _
  108. __ _ _ __ (_) __| | ___ ___
  109. / _' | '_ \\| |/ _' |/ _ \\ / __|
  110. | (_| | |_) | | (_| | (_) | (__
  111. \\__,_| .__/|_|\\__,_|\\___/ \\___|
  112. |_| v${pkgjson.version}
  113. `;
  114. console.info(asciiArt);
  115. }
  116. // Watch mode (-w/--watch)
  117. if (argv.watch) {
  118. nodemon({ watch: options.src });
  119. console.info('Watch mode enabled\nListening for changes on input files...');
  120. nodemon.on('quit', function () {
  121. console.info('\nProcess terminated.');
  122. process.exit();
  123. }).on('restart', function () {
  124. console.info('Changes detected, rebuilding...');
  125. apidoc.createDoc(options);
  126. const d = new Date();
  127. console.info(`Build finished at ${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}`);
  128. });
  129. }
  130. // Create documentation
  131. if (apidoc.createDoc(options) === false) {
  132. console.error('[error] apidoc encountered an error during documentation generation!');
  133. process.exit(1);
  134. }