api.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. const fs = require('fs');
  2. const path = require('path');
  3. const execa = require('execa');
  4. const rimraf = require('rimraf');
  5. const { spawn } = require('child_process');
  6. const { Extractor, ExtractorConfig } = require('@microsoft/api-extractor');
  7. try {
  8. // clear folder
  9. rimraf('./temp/*', () => {
  10. execScript('npm', ['run', 'tsc'], () => {
  11. // generate api
  12. apiExtractor().then(
  13. () => {
  14. apiDocumenter();
  15. },
  16. () => {
  17. apiDocumenter();
  18. },
  19. );
  20. });
  21. });
  22. } catch (error) {
  23. console.log(error);
  24. }
  25. function apiExtractor() {
  26. const apiExtractorJsonPath = path.join(__dirname, './api-extractor.json');
  27. // Load and parse the api-extractor.json file
  28. const extractorConfig = ExtractorConfig.loadFileAndPrepare(apiExtractorJsonPath);
  29. return new Promise((resolve, reject) => {
  30. try {
  31. // Invoke API Extractor
  32. const extractorResult = Extractor.invoke(extractorConfig, {
  33. // Equivalent to the "--local" command-line parameter
  34. localBuild: true,
  35. // Equivalent to the "--verbose" command-line parameter
  36. showVerboseMessages: true,
  37. });
  38. if (extractorResult.succeeded) {
  39. console.log(`API Extractor completed successfully`);
  40. process.exitCode = 0;
  41. resolve();
  42. } else {
  43. console.error(
  44. `API Extractor completed with ${extractorResult.errorCount} errors`
  45. + ` and ${extractorResult.warningCount} warnings`,
  46. );
  47. process.exitCode = 1;
  48. reject();
  49. }
  50. } catch (p) {
  51. console.log(`Exit code: ${p.exitCode}`);
  52. console.log(`Error: ${p.stderr}`);
  53. reject();
  54. }
  55. });
  56. }
  57. async function apiDocumenter() {
  58. // create empty folders
  59. let input = 'temp/input';
  60. let markdown = 'temp/markdown';
  61. // move folders
  62. const oldInputPath = 'temp/sheet-format.api.json';
  63. const newInputPath = 'temp/input/sheet-format.api.json';
  64. const oldMarkdownPath = 'temp/sheet-format.api.md';
  65. const newMarkdownPath = 'temp/markdown/sheet-format.api.md';
  66. const mkmvInput = new Promise((resolve, reject) => {
  67. mkdir(input, () => {
  68. mv(oldInputPath, newInputPath, resolve);
  69. });
  70. });
  71. const mkmvMarkdown = new Promise((resolve, reject) => {
  72. mkdir(markdown, () => {
  73. mv(oldMarkdownPath, newMarkdownPath, resolve);
  74. });
  75. });
  76. Promise.all([mkmvInput, mkmvMarkdown]).then(() => {
  77. process.chdir('temp');
  78. // execute api-documenter
  79. execScript('api-documenter', ['markdown']);
  80. });
  81. }
  82. function mkdir(dir, cb) {
  83. fs.mkdir(path.join(__dirname, dir), (err) => {
  84. if (err) {
  85. return console.error(err);
  86. }
  87. console.log(`Successfully create ${dir}!`);
  88. cb && cb();
  89. });
  90. }
  91. function mv(oldPath, newPath, cb) {
  92. fs.rename(oldPath, newPath, (err) => {
  93. if (err) throw err;
  94. console.log(`Successfully move ${oldPath} to ${newPath}!`);
  95. cb && cb();
  96. });
  97. }
  98. function execScript(cmd, args = [], cb) {
  99. // kick off process of listing files
  100. const child = spawn(cmd, args, { shell: true });
  101. // spit stdout to screen
  102. child.stdout.on('data', (data) => {
  103. process.stdout.write(data.toString());
  104. });
  105. // spit stderr to screen
  106. child.stderr.on('data', (data) => {
  107. process.stdout.write(data.toString());
  108. });
  109. child.on('close', (code) => {
  110. console.log(`Finished with code ${code}`);
  111. cb && cb();
  112. });
  113. }