options.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. /**
  12. * All about options
  13. */
  14. const fs = require('fs');
  15. const path = require('path');
  16. const _ = require('lodash');
  17. const DEFAULT_DEST = 'doc';
  18. const DEFAULT_SRC = ['./src'];
  19. const DEFAULT_TEMPLATE = 'template';
  20. const defaultOptions = {
  21. src: DEFAULT_SRC,
  22. dest: path.resolve(path.join(__dirname, '..', DEFAULT_DEST)) + path.sep,
  23. template: path.resolve(path.join(__dirname, '..', DEFAULT_TEMPLATE)) + path.sep,
  24. templateSingleFile: path.resolve(__dirname, '../template-single/index.html'),
  25. debug: false,
  26. single: false, // build to single file
  27. silent: false,
  28. verbose: false,
  29. dryRun: false,
  30. colorize: true,
  31. markdown: true,
  32. config: '',
  33. apiprivate: false,
  34. encoding: 'utf8',
  35. };
  36. function process (options) {
  37. // merge given options with defaults
  38. options = _.defaults({}, options, defaultOptions);
  39. // if a config file is given, read it to figure out input and output
  40. if (options.config) {
  41. // make sure that we are provided a config file, not a directory
  42. if (fs.statSync(options.config).isDirectory()) {
  43. throw new Error('[error] Invalid option: --config/-c must be a path to a file. Directory provided.');
  44. }
  45. const configPath = path.resolve(options.config);
  46. const apidocConfig = require(configPath);
  47. // if dest is present in config file, set it in options, but only if it's the default value, as cli options should override config file options
  48. if (apidocConfig.output && options.dest === defaultOptions.dest) {
  49. // keep a trailing slash
  50. options.dest = path.resolve(path.join(apidocConfig.output, path.sep));
  51. }
  52. // do the same for input
  53. if (apidocConfig.input instanceof Array && options.src[0] === DEFAULT_SRC[0]) {
  54. // keep a trailing slash
  55. const input = apidocConfig.input.map(p => path.resolve(p) + path.sep);
  56. options.src = input;
  57. }
  58. }
  59. // add a trailing slash to output destination because it's always a folder
  60. options.dest = path.join(options.dest, path.sep);
  61. options.template = path.join(options.template, path.sep);
  62. // Line-Ending option
  63. if (options.lineEnding) {
  64. if (options.lineEnding === 'CRLF') { // win32
  65. options.lineEnding = '\r\n';
  66. } else if (options.lineEnding === 'CR') { // darwin
  67. options.lineEnding = '\r';
  68. } else { // linux
  69. options.lineEnding = '\n';
  70. }
  71. }
  72. return options;
  73. }
  74. module.exports = {
  75. process: process,
  76. defaultOptions: defaultOptions,
  77. };