@alova/wormhole
Version:
More modern openAPI generating solution for alova.js
59 lines (58 loc) • 2.33 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.readConfig = readConfig;
exports.getAutoUpdateConfig = getAutoUpdateConfig;
exports.getApiDocs = getApiDocs;
const promises_1 = require("node:fs/promises");
const node_path_1 = __importDefault(require("node:path"));
const esbuild_1 = __importDefault(require("esbuild"));
const helper_1 = require("./helper");
const utils_1 = require("./utils");
/**
* Read the alova.config configuration file and return the parsed configuration object.
* @param projectPath The project path where the configuration file is located. The default value is `process.cwd()`.
* @returns a promise instance that contains configuration object.
*/
async function readConfig(projectPath = process.cwd()) {
const configFile = await (0, utils_1.resolveConfigFile)(projectPath);
if (!configFile) {
throw helper_1.logger.throwError(`Cannot found config file from path ${projectPath}`, {
projectPath,
name: 'readConfig',
});
}
const configTmpFileName = `alova_tmp_${Date.now()}.cjs`;
const outfile = node_path_1.default.join(projectPath, configTmpFileName);
await esbuild_1.default.build({
entryPoints: [configFile],
bundle: true,
format: 'cjs',
platform: 'node',
outfile,
logLevel: 'silent',
});
// eslint-disable-next-line ts/no-require-imports
const module = require(outfile);
(0, promises_1.unlink)(outfile);
const config = module.default || module;
// Read the cache file and save it
await helper_1.configHelper.load(config, projectPath);
return config;
}
async function getAutoUpdateConfig(config) {
await helper_1.configHelper.load(config);
return helper_1.configHelper.autoUpdateConfig();
}
async function getApiDocs(config, projectPath = process.cwd()) {
if (!config || !projectPath) {
return [];
}
await helper_1.configHelper.load(config, projectPath);
return helper_1.configHelper.getOutput().map((output) => {
const templateData = helper_1.TemplateHelper.getData(projectPath, output);
return templateData?.pathApis ?? [];
});
}