main.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (C) 2024-present Puter Technologies Inc.
  3. *
  4. * This file is part of Puter.
  5. *
  6. * Puter is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. const fs = require("fs");
  20. const path_ = require("path");
  21. const rootdir = path_.resolve(process.argv[2] ?? '.');
  22. const parser = require('@babel/parser');
  23. const traverse = require('@babel/traverse').default;
  24. const { ModuleDoc } = require("./defs");
  25. const processors = require("./processors");
  26. const doc_module = new ModuleDoc();
  27. const handle_file = (code, context) => {
  28. const ast = parser.parse(code);
  29. const traverse_callbacks = {};
  30. for ( const processor of processors ) {
  31. if ( processor.match(context) ) {
  32. for ( const key in processor.traverse ) {
  33. if ( ! traverse_callbacks[key] ) {
  34. traverse_callbacks[key] = [];
  35. }
  36. traverse_callbacks[key].push(processor.traverse[key]);
  37. }
  38. }
  39. }
  40. for ( const key in traverse_callbacks ) {
  41. traverse(ast, {
  42. [key] (path) {
  43. context.skip = false;
  44. for ( const callback of traverse_callbacks[key] ) {
  45. callback(path, context);
  46. if ( context.skip ) return;
  47. }
  48. }
  49. });
  50. }
  51. }
  52. // Module and class files
  53. {
  54. const files = fs.readdirSync(rootdir);
  55. for ( const file of files ) {
  56. const stat = fs.statSync(path_.join(rootdir, file));
  57. if ( stat.isDirectory() ) {
  58. continue;
  59. }
  60. if ( ! file.endsWith('.js') ) continue;
  61. const type =
  62. file.endsWith('Service.js') ? 'service' :
  63. file.endsWith('Module.js') ? 'module' :
  64. null;
  65. if ( type === null ) continue;
  66. console.log('file', file);
  67. const code = fs.readFileSync(path_.join(rootdir, file), 'utf8');
  68. const firstLine = code.slice(0, code.indexOf('\n'));
  69. let metadata = {};
  70. const METADATA_PREFIX = '// METADATA // ';
  71. if ( firstLine.startsWith(METADATA_PREFIX) ) {
  72. metadata = JSON.parse(firstLine.slice(METADATA_PREFIX.length));
  73. }
  74. const context = {
  75. metadata,
  76. type,
  77. doc_module,
  78. filename: file,
  79. };
  80. handle_file(code, context);
  81. }
  82. }
  83. // Library files
  84. if ( fs.existsSync(path_.join(rootdir, 'lib')) ) {
  85. const files = fs.readdirSync(path_.join(rootdir, 'lib'));
  86. for ( const file of files ) {
  87. if ( file.startsWith('_') ) continue;
  88. const code = fs.readFileSync(path_.join(rootdir, 'lib', file), 'utf8');
  89. const firstLine = code.slice(0, code.indexOf('\n'));
  90. let metadata = {};
  91. const METADATA_PREFIX = '// METADATA // ';
  92. if ( firstLine.startsWith(METADATA_PREFIX) ) {
  93. metadata = JSON.parse(firstLine.slice(METADATA_PREFIX.length));
  94. }
  95. const doc_item = doc_module.add_lib();
  96. doc_item.name = metadata.def ?? file.slice(0, -3);
  97. const context = {
  98. metadata,
  99. type: 'lib',
  100. doc_module,
  101. doc_item,
  102. filename: file,
  103. };
  104. handle_file(code, context);
  105. }
  106. }
  107. const outfile = path_.join(rootdir, 'README.md');
  108. const out = doc_module.toMarkdown();
  109. fs.writeFileSync(outfile, out);