@lonu/stc
Version:
A tool for converting OpenApi/Swagger/Apifox into code.
43 lines (42 loc) • 1.34 kB
JavaScript
import oxc from "oxc-transform";
import Logs from "../../console.js";
/**
* Generates a declaration file from the given source code.
*
* @param {string} sourceCode - The source code to generate the declaration file from.
* @return {string} The generated declaration file content.
*/
export const generateDeclarationFile = (sourceCode) => {
const { errors, code } = oxc.isolatedDeclaration(`temp_${+new Date()}.ts`, sourceCode, {
sourcemap: false,
});
if (errors.length > 0) {
Logs.error(errors.join("\n"));
return "";
}
return code;
};
/**
* Transforms the given source code into TypeScript, returning an object with the transformed code and declaration.
*
* @param {string} source - The source code to transform.
* @return {{ code: string, declaration: string }} An object with the transformed code and declaration.
*/
export const oxcTransform = (source) => {
const { code, declaration, errors } = oxc.transform(`temp_${+new Date()}.ts`, source, {
typescript: {
allowDeclareFields: false,
declaration: {
sourcemap: false,
},
},
});
if (errors.length > 0) {
Logs.error(errors.join("\n"));
return { code: "", declaration: "" };
}
return {
code,
declaration,
};
};