refactor: numerous improvements (#32)

This commit is contained in:
sigoden
2024-06-07 21:36:34 +08:00
committed by GitHub
parent e1d895cc9a
commit 63df67acea
11 changed files with 159 additions and 115 deletions
+29 -20
View File
@@ -1,11 +1,13 @@
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const TOOL_ENTRY_FUNC = "run";
function main(isTool = true) {
function main() {
const scriptfile = process.argv[2];
const isTool = path.dirname(scriptfile) == "tools";
const contents = fs.readFileSync(process.argv[2], "utf8");
const functions = extractFunctions(contents, isTool);
let declarations = functions.map(({ funcName, jsdoc }) => {
@@ -56,13 +58,20 @@ function extractFunctions(contents, isTool) {
});
}
} else {
const match = /function *([_A-Za-z]+)/.exec(line);
let match = /^export (async )?function ([A-Za-z0-9_]+)/.exec(line);
let funcName = null;
if (match) {
const funcName = match[1];
if (!funcName.startsWith("_")) {
output.push({ funcName, jsdoc });
funcName = match[2];
}
if (!funcName) {
match = /^exports\.([A-Za-z0-9_]+) = (async )?function /.exec(line);
if (match) {
funcName = match[1];
}
}
if (funcName) {
output.push({ funcName, jsdoc });
}
}
jsdoc = "";
}
@@ -165,21 +174,6 @@ function buildProperty(type, description) {
return property;
}
/**
* @param {string} filePath
*/
function getBasename(filePath) {
const filenameWithExt = filePath.split(/[/\\]/).pop();
const lastDotIndex = filenameWithExt.lastIndexOf(".");
if (lastDotIndex === -1) {
return filenameWithExt;
}
return filenameWithExt.substring(0, lastDotIndex);
}
/**
* @param {string} name
* @param {string} description
@@ -204,4 +198,19 @@ function buildDeclaration(name, description, params) {
return schema;
}
/**
* @param {string} filePath
*/
function getBasename(filePath) {
const filenameWithExt = filePath.split(/[/\\]/).pop();
const lastDotIndex = filenameWithExt.lastIndexOf(".");
if (lastDotIndex === -1) {
return filenameWithExt;
}
return filenameWithExt.substring(0, lastDotIndex);
}
main();