prasi-api/pkgs/server/parse-args.ts

37 lines
1.0 KiB
TypeScript

import { parseFile } from "@swc/core";
import { readAsync } from "fs-jetpack";
export const parseArgs = async (path: string) => {
const res = await parseFile(path, { syntax: "typescript" });
const args: string[] = [];
for (const e of res.body) {
if (
e.type === "ExportDeclaration" &&
e.declaration.type === "VariableDeclaration"
) {
const declare = e.declaration.declarations[0];
if (
declare &&
declare.type === "VariableDeclarator" &&
declare.id.type === "Identifier" &&
declare.id.value === "_" &&
declare.init?.type === "ObjectExpression"
) {
for (const prop of declare.init.properties) {
if (prop.type === "MethodProperty") {
for (const param of prop.params) {
if (
param.type === "Parameter" &&
param.pat.type === "Identifier"
) {
args.push(param.pat.value);
}
}
}
}
}
}
}
return args;
};