@patchworkdev/pdk
Version:
Patchwork Development Kit
114 lines (113 loc) • 5.3 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.analyzeAPI = analyzeAPI;
const path_1 = __importDefault(require("path"));
const ts = __importStar(require("typescript"));
const error_1 = require("./error");
const logger_1 = require("./logger");
function analyzeAPI(filePath) {
const configPath = ts.findConfigFile(path_1.default.dirname(filePath), ts.sys.fileExists, 'tsconfig.json');
if (!configPath) {
throw new error_1.PDKError(error_1.ErrorCode.FILE_NOT_FOUND, `Could not find a valid 'tsconfig.json' at ${configPath}`);
}
logger_1.logger.debug('Found tsconfig at:', configPath);
const { config } = ts.readConfigFile(configPath, ts.sys.readFile);
const { options, fileNames } = ts.parseJsonConfigFileContent(config, ts.sys, path_1.default.dirname(configPath));
const program = ts.createProgram(fileNames, options);
const sourceFile = program.getSourceFile(filePath);
if (!sourceFile) {
throw new error_1.PDKError(error_1.ErrorCode.FILE_NOT_FOUND, `Could not find source file at ${filePath}`);
}
const apiStructure = {};
logger_1.logger.debug('Starting analysis of file:', filePath);
function analyzeNode(node) {
if (ts.isVariableStatement(node)) {
const declaration = node.declarationList.declarations[0];
if (ts.isIdentifier(declaration.name) && declaration.name.text === 'api') {
logger_1.logger.debug('Found api variable declaration');
if (declaration.initializer && ts.isObjectLiteralExpression(declaration.initializer)) {
extractAPIStructure(declaration.initializer, apiStructure);
}
}
}
ts.forEachChild(node, analyzeNode);
}
function extractAPIStructure(node, structure) {
logger_1.logger.debug('Extracting API structure');
node.properties.forEach((prop) => {
if (ts.isPropertyAssignment(prop) && ts.isIdentifier(prop.name)) {
const routerName = prop.name.text;
logger_1.logger.debug('Found router:', routerName);
if (ts.isCallExpression(prop.initializer) && ts.isIdentifier(prop.initializer.expression) && prop.initializer.expression.text === 'router') {
extractRouterProcedures(prop.initializer, structure, routerName);
}
}
});
}
function extractRouterProcedures(node, structure, routerName) {
logger_1.logger.debug('Extracting procedures for router:', routerName);
if (node.arguments.length > 0 && ts.isObjectLiteralExpression(node.arguments[0])) {
extractProcedures(node.arguments[0], structure, routerName);
}
}
function extractProcedures(node, structure, routerName) {
node.properties.forEach((prop) => {
if (ts.isPropertyAssignment(prop) && ts.isIdentifier(prop.name)) {
const procedureName = prop.name.text;
const fullName = `${routerName}.${procedureName}`;
const procedureType = getProcedureType(prop.initializer);
structure[fullName] = {
name: procedureName,
type: procedureType,
};
logger_1.logger.debug('Added procedure:', fullName, 'with type:', procedureType);
}
});
}
function getProcedureType(node) {
if (ts.isCallExpression(node)) {
let current = node;
while (ts.isCallExpression(current)) {
if (ts.isPropertyAccessExpression(current.expression)) {
const name = current.expression.name.text;
if (name === 'query')
return 'query';
if (name === 'mutation')
return 'mutation';
if (name === 'subscription')
return 'subscription';
}
current = current.expression;
}
}
return 'query';
}
analyzeNode(sourceFile);
return apiStructure;
}