a2r
Version:
A2R Framework
59 lines (58 loc) • 2.67 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const typescript_1 = __importDefault(require("typescript"));
const path_1 = __importDefault(require("path"));
const fs_1 = require("@a2r/fs");
const getMainMethodName_1 = __importDefault(require("./getMainMethodName"));
const getMainMethodNode_1 = __importDefault(require("./getMainMethodNode"));
const getFunctionDocContainer_1 = __importDefault(require("./getFunctionDocContainer"));
const getFunctionReturnTypeInfo_1 = __importDefault(require("./getFunctionReturnTypeInfo"));
const getModelImports_1 = __importDefault(require("./getModelImports"));
const getParamsTypes_1 = __importDefault(require("./getParamsTypes"));
/**
* Gets module info from a file
* @param filePath Module file path
* @param apiSourcePath API source path (used for relative path)
*/
const getModuleInfo = async (filePath, apiSourcePath) => {
const content = await (0, fs_1.readFile)(filePath, 'utf8');
const mainMethodParamNodes = [];
const sourceFile = typescript_1.default.createSourceFile(filePath, content, typescript_1.default.ScriptTarget.Latest, true);
const fileNodes = sourceFile.getChildren();
const mainMethodName = (0, getMainMethodName_1.default)(fileNodes);
const mainMethodNode = (0, getMainMethodNode_1.default)(fileNodes, mainMethodName);
if (!mainMethodNode) {
return null;
}
mainMethodNode.forEachChild((child) => {
if (typescript_1.default.isParameter(child)) {
mainMethodParamNodes.push(child);
}
});
const mainMethodDocs = (0, getFunctionDocContainer_1.default)(mainMethodNode);
const mainMethodReturnTypeInfo = (0, getFunctionReturnTypeInfo_1.default)(mainMethodNode);
if (!mainMethodReturnTypeInfo) {
return null;
}
const usedTypes = mainMethodParamNodes.reduce((t, n) => [...t, ...(0, getParamsTypes_1.default)(n.getChildren(sourceFile), sourceFile)], []);
usedTypes.push(...(0, getParamsTypes_1.default)([mainMethodReturnTypeInfo.typeNode], sourceFile));
const modelImports = (0, getModelImports_1.default)(fileNodes, usedTypes, sourceFile);
const keys = path_1.default
.relative(apiSourcePath, filePath)
.replace(/\.ts$/, '')
.split(path_1.default.sep);
return {
mainMethodDocs,
mainMethodName,
mainMethodNode,
mainMethodParamNodes,
mainMethodReturnTypeInfo,
modelImports,
usedTypes,
keys,
};
};
exports.default = getModuleInfo;