|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path') |
| 5 | + |
| 6 | +function escapeRegExp(str) { |
| 7 | + return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"); |
| 8 | +} |
| 9 | + |
| 10 | +String.prototype.replaceAll = function (search, replacement) { |
| 11 | + var target = this; |
| 12 | + return target.replace(new RegExp(escapeRegExp(search), 'g'), replacement); |
| 13 | +}; |
| 14 | + |
| 15 | +const [node, app, configFile] = process.argv; |
| 16 | +const config = JSON.parse(fs.readFileSync(configFile)); |
| 17 | + |
| 18 | +const files = read_files(config.source); |
| 19 | + |
| 20 | +var annotations = []; |
| 21 | +files.forEach(({ folder, name }) => { |
| 22 | + const extention = path.extname(name); |
| 23 | + if (extention == '.js') { |
| 24 | + var fileAnnotations = { |
| 25 | + folder: folder, |
| 26 | + name: name, |
| 27 | + annotations: [] |
| 28 | + } |
| 29 | + var fileContents = fs.readFileSync(folder + name, "utf8"); |
| 30 | + const matches = fileContents.match(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm); |
| 31 | + if (matches != null) { |
| 32 | + matches.forEach((matched, index) => { |
| 33 | + fileAnnotations.annotations.push(read_annotation(matched)); |
| 34 | + }); |
| 35 | + } |
| 36 | + annotations.push(fileAnnotations); |
| 37 | + } |
| 38 | +}) |
| 39 | + |
| 40 | +var output = ""; |
| 41 | + |
| 42 | +fs.writeFileSync(config.destination + "enums.md", JSON.stringify(annotations, null, 2)); |
| 43 | +console.log('\x1b[33m%s\x1b[0m', "enum.md"); |
| 44 | + |
| 45 | + |
| 46 | +function read_files(path) { |
| 47 | + var result = []; |
| 48 | + read_folder(path, (name, folder) => { |
| 49 | + result.push({ |
| 50 | + folder, |
| 51 | + name |
| 52 | + }); |
| 53 | + }); |
| 54 | + return result; |
| 55 | +} |
| 56 | + |
| 57 | +function read_folder(folder, onItem) { |
| 58 | + const items = fs.readdirSync(folder); |
| 59 | + const stats = items.reduce((agg, name) => { |
| 60 | + agg[name] = fs.lstatSync(folder + name); |
| 61 | + return agg; |
| 62 | + }, {}); |
| 63 | + items.forEach(name => { |
| 64 | + if (stats[name].isDirectory()) { |
| 65 | + read_folder(folder + name + "\\", onItem); |
| 66 | + } else { |
| 67 | + onItem(name, folder); |
| 68 | + } |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +function read_annotation(annotation) { |
| 73 | + var lines = annotation.split("\n"); |
| 74 | + var result = { |
| 75 | + name: null, |
| 76 | + keyword: null |
| 77 | + }; |
| 78 | + var section = result; |
| 79 | + for (var index = 0; index < lines.length; index += 1) { |
| 80 | + var line = lines[index]; |
| 81 | + switch (line) { |
| 82 | + case "/*": |
| 83 | + case "*/": |
| 84 | + break; |
| 85 | + default: |
| 86 | + { |
| 87 | + var colonIndex = line.indexOf(":"); |
| 88 | + var dashIndex = line.indexOf("-"); |
| 89 | + if (colonIndex >= 0) { |
| 90 | + var name = line.substring(0, colonIndex - 1).trim().toLowerCase(); |
| 91 | + var keyword = line.substring(colonIndex + 1, line.length).trim(); |
| 92 | + switch (name) { |
| 93 | + case "function": |
| 94 | + result.name = name; |
| 95 | + result.keyword = keyword; |
| 96 | + section = result; |
| 97 | + break; |
| 98 | + case "parameters": |
| 99 | + case "returns": |
| 100 | + result[name] = { |
| 101 | + name: name |
| 102 | + } |
| 103 | + section = result[name]; |
| 104 | + break; |
| 105 | + } |
| 106 | + } else if (dashIndex > 0) { |
| 107 | + var name = line.substring(0, dashIndex - 1).trim().toLowerCase(); |
| 108 | + var description = line.substring(dashIndex + 1, line.length).trim(); |
| 109 | + section.items = section.items || []; |
| 110 | + section.items.push({ |
| 111 | + name: name, |
| 112 | + description: description |
| 113 | + }) |
| 114 | + } else { |
| 115 | + section.description = section.description || ""; |
| 116 | + section.description += line.trim(); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + return result; |
| 122 | +} |
0 commit comments