UNPKG

@antmjs/rapper

Version:

rapper, use http api as a function

176 lines (175 loc) 7.54 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.requestFileParse = void 0; var path = __importStar(require("path")); var ts = __importStar(require("typescript")); var utils_1 = require("./../../utils"); function isNodeExported(node) { return ((ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Export) !== 0 || (!!node.parent && node.parent.kind === ts.SyntaxKind.SourceFile)); } function getRealPath(importPath, alias) { for (var item in alias) { var aliasReg = new RegExp("^".concat(item)); if (aliasReg.test(importPath)) { return importPath.replace(aliasReg, alias[item]); } } return null; } function getAllLeadingComments(node, sourceFile) { var allRanges = []; var nodeText = node.getFullText(sourceFile); var cr = ts.getLeadingCommentRanges(nodeText, 0); if (cr) allRanges.push.apply(allRanges, cr.map(function (c) { return (__assign(__assign({}, c), { text: nodeText.substring(c.pos, c.end) })); })); var synthetic = ts.getSyntheticLeadingComments(node); if (synthetic) allRanges.push.apply(allRanges, synthetic); return allRanges; } function requestFileParse(file, formatFunc, config) { var filePath = file.path, content = file.content; var sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest); var importTypes = []; var exportInterfaceFunc = []; var importType = { importNames: [], importPath: '', }; var fileInfo = { filePath: filePath, fileName: path.basename(filePath).replace(/\.[a-z]+$/, ''), content: content, moduleId: (0, utils_1.getRapModuleId)(content, false), }; ts.forEachChild(sourceFile, function (node) { var _a, _b; if (ts.isImportDeclaration(node) && node.importClause.isTypeOnly) { if (ts.isNamedImports(node.importClause.namedBindings)) { node.importClause.namedBindings.forEachChild(function (node) { importType.importNames.push(node.getText(sourceFile)); }); var importPath = node.moduleSpecifier .getText(sourceFile) .replace(/["']/g, ''); var absolutePath = path.resolve(__dirname, filePath, '..', importPath); if (config.upload.alias) { var realPath = getRealPath(importPath, config.upload.alias); if (realPath) { absolutePath = realPath; } } node.moduleSpecifier.getText(sourceFile); importType.importPath = /ts$/.test(absolutePath) ? absolutePath : absolutePath + '.ts'; node.moduleSpecifier.getText(sourceFile); } importTypes.push(importType); } if (ts.isFunctionDeclaration(node) && isNodeExported(node)) { var comment_1 = ''; node.getChildren(sourceFile).forEach(function (el) { if (ts.isJSDoc(el)) { if (typeof el.comment == 'string') { comment_1 = el.comment; } } }); exportInterfaceFunc.push({ funcName: node.name.getText(sourceFile), comment: comment_1, body: node.getText(sourceFile), funcType: 'FunctionDeclaration', }); return; } if (ts.isVariableStatement(node) && isNodeExported(node)) { var comments = getAllLeadingComments(node, sourceFile) || []; var comment_2 = (_b = (_a = comments[comments.length - 1]) === null || _a === void 0 ? void 0 : _a.text) !== null && _b !== void 0 ? _b : ''; node.getChildren(sourceFile).forEach(function (el) { if (ts.isVariableDeclarationList(el)) { el.forEachChild(function (declarationNode) { if (!ts.isVariableDeclaration(declarationNode)) return; var funcName = declarationNode.name.getText(sourceFile); declarationNode.forEachChild(function (functionNode) { var isCallExpression = ts.isCallExpression(functionNode); if (isCallExpression || ts.isArrowFunction(functionNode)) { exportInterfaceFunc.push({ funcName: funcName, comment: comment_2, body: node.getText(sourceFile), funcType: isCallExpression ? 'CallExpression' : 'ArrowFunction', }); } }); }); } }); } }); var funcTypes = exportInterfaceFunc .map(formatFunc) .map(function (data, index) { if (data) { var _a = exportInterfaceFunc[index], body = _a.body, funcName = _a.funcName, comment = _a.comment; var matchReq = data.reqTypeName.match(/^(\w+)\[['|"](\w+)['|"]\]/); var matchRes = data.resTypeName.match(/^(\w+)\[['|"](\w+)['|"]\]/); var reqTypeName = matchReq ? [matchReq[1], matchReq[2]] : data.reqTypeName; var resTypeName = matchRes ? [matchRes[1], matchRes[2]] : data.resTypeName; return __assign(__assign({}, data), { body: body, funcName: funcName, comment: comment, resTypeName: resTypeName, reqTypeName: reqTypeName }); } return null; }) .filter(function (name) { return name; }); return { importType: importType, funcTypes: funcTypes, fileInfo: fileInfo, }; } exports.requestFileParse = requestFileParse;