@alova/wormhole
Version:
More modern openAPI generating solution for alova.js
47 lines (46 loc) • 2.02 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeAlovaJson = writeAlovaJson;
exports.readAlovaJson = readAlovaJson;
exports.getAlovaJsonPath = getAlovaJsonPath;
const promises_1 = __importDefault(require("node:fs/promises"));
const node_path_1 = __importDefault(require("node:path"));
const config_1 = require("../config");
const helper_1 = require("../helper");
const utils_1 = require("../utils");
const DEFAULT_CONFIG = (0, config_1.getGlobalConfig)();
async function writeAlovaJson(data, originPath, name = 'api.json') {
// Convert data to JSON string
const jsonData = await (0, utils_1.format)(JSON.stringify(data, null, 2), { parser: 'json' });
// Define the path and name of the JSON file
const filePath = `${originPath}_${name}`;
const dirPath = filePath.split(/\/|\\/).slice(0, -1).join('/');
if (!(await (0, utils_1.existsPromise)(dirPath))) {
await promises_1.default.mkdir(dirPath, { recursive: true });
}
// Use fs.writeFile to write JSON data to a file
return promises_1.default.writeFile(filePath, jsonData);
}
async function readAlovaJson(originPath, name = 'api.json') {
// Define the path and name of the JSON file
const filePath = `${originPath}_${name}`;
if (!(await (0, utils_1.existsPromise)(filePath))) {
throw helper_1.logger.throwError('alovaJson is not exists');
}
// Read JSON files using fs.readFile
const data = await promises_1.default.readFile(filePath, 'utf8');
let jsonData = {};
try {
jsonData = JSON.parse(data);
}
catch {
jsonData = DEFAULT_CONFIG.templateData.get(originPath) ?? jsonData;
}
return jsonData;
}
function getAlovaJsonPath(workspaceRootDir, outputPath) {
return node_path_1.default.join(workspaceRootDir, DEFAULT_CONFIG.alovaTempPath, outputPath.split(/\/|\\/).join('_'));
}