Reference Source Test

src/ESDocCLI.js

  1. #!/usr/bin/env node
  2. import fs from 'fs';
  3. import path from 'path';
  4. import minimist from 'minimist';
  5. import esdoc2 from './ESDoc.js';
  6. import NPMUtil from './Util/NPMUtil.js';
  7. import log from 'npmlog';
  8.  
  9. log.heading = 'ESDoc2';
  10. log.info('using', 'node@%s', process.version);
  11. log.info('using', 'esdoc2@%s', NPMUtil.findPackage().version);
  12. /**
  13. * Command Line Interface for esdoc2.
  14. *
  15. * @example
  16. * let cli = new ESDocCLI(process.argv);
  17. * cli.exec();
  18. */
  19. export default class ESDocCLI {
  20. /**
  21. * Create instance.
  22. * @param {Object} argv - this is node.js argv(``process.argv``)
  23. */
  24. constructor(argv) {
  25. /** @type {ESDocCLIArgv} */
  26. this._argv = minimist(argv.slice(2));
  27.  
  28. if (this._argv.h || this._argv.help) {
  29. this._showHelp();
  30. process.exit(0);
  31. }
  32.  
  33. if (this._argv.v || this._argv.version) {
  34. this._showVersion();
  35. process.exit(0);
  36. }
  37.  
  38. process.on('unhandledRejection', (reason, p) => {
  39. console.error('Unhandled Rejection at: Promise', p, 'reason:', reason);
  40. });
  41. }
  42.  
  43. /**
  44. * execute to generate document.
  45. */
  46. exec() {
  47. let config;
  48.  
  49. const configPath = this._findConfigFilePath();
  50. log.info('config', configPath);
  51. if (configPath) {
  52. config = this._createConfigFromJSONFile(configPath);
  53. } else {
  54. config = this._createConfigFromPackageJSON();
  55. }
  56.  
  57. if (config) {
  58. esdoc2.generate(config);
  59. } else {
  60. this._showHelp();
  61. process.exit(1);
  62. }
  63. }
  64.  
  65. /**
  66. * show help of esdoc2
  67. * @private
  68. */
  69. _showHelp() {
  70. console.log('Usage: esdoc [-c esdoc.json]');
  71. console.log('');
  72. console.log('Options:');
  73. console.log(' -c', 'specify config file');
  74. console.log(' -h', 'output usage information');
  75. console.log(' -v', 'output the version number');
  76. console.log('');
  77. console.log('esdoc2 finds configuration by the order:');
  78. console.log(' 1. `-c your-esdoc.json`');
  79. console.log(' 2. `.esdoc.json` in current directory');
  80. console.log(' 3. `.esdoc.js` in current directory');
  81. console.log(' 4. `esdoc` property in package.json');
  82. }
  83.  
  84. /**
  85. * show version of esdoc2
  86. * @private
  87. */
  88. _showVersion() {
  89. const packageObj = NPMUtil.findPackage();
  90. if (packageObj) {
  91. console.log(packageObj.version);
  92. } else {
  93. console.log('0.0.0');
  94. }
  95. }
  96.  
  97. /**
  98. * find esdoc2 config file.
  99. * @returns {string|null} config file path.
  100. * @private
  101. */
  102. _findConfigFilePath() {
  103. if (this._argv.c) {
  104. return this._argv.c;
  105. }
  106.  
  107. try {
  108. const filePath = path.resolve('./.esdoc.json');
  109. fs.readFileSync(filePath);
  110. return filePath;
  111. } catch (e) {
  112. // ignore
  113. }
  114.  
  115. try {
  116. const filePath = path.resolve('./.esdoc.js');
  117. fs.readFileSync(filePath);
  118. return filePath;
  119. } catch (e) {
  120. // ignore
  121. }
  122.  
  123. return null;
  124. }
  125.  
  126. /**
  127. * create config object from config file.
  128. * @param {string} configFilePath - config file path.
  129. * @return {ESDocConfig} config object.
  130. * @private
  131. */
  132. _createConfigFromJSONFile(configFilePath) {
  133. configFilePath = path.resolve(configFilePath);
  134. const ext = path.extname(configFilePath);
  135. if (ext === '.js') {
  136. /* eslint-disable global-require */
  137. return require(configFilePath);
  138. } else {
  139. const configJSON = fs.readFileSync(configFilePath, {encode: 'utf8'});
  140. const config = JSON.parse(configJSON);
  141. return config;
  142. }
  143. }
  144.  
  145. /**
  146. * create config object from package.json.
  147. * @return {ESDocConfig|null} config object.
  148. * @private
  149. */
  150. _createConfigFromPackageJSON() {
  151. try {
  152. const filePath = path.resolve('./package.json');
  153. const packageJSON = fs.readFileSync(filePath, 'utf8').toString();
  154. const packageObj = JSON.parse(packageJSON);
  155. return packageObj.esdoc2;
  156. } catch (e) {
  157. // ignore
  158. }
  159.  
  160. return null;
  161. }
  162. }
  163.  
  164. // if this file is directory executed, work as CLI.
  165. const executedFilePath = fs.realpathSync(process.argv[1]);
  166. if (executedFilePath === __filename) {
  167. const cli = new ESDocCLI(process.argv);
  168. cli.exec();
  169. }