sardines-compile-time-tools
Version:
sardines.compile-time-tools.js is part of the sardines.io project
59 lines (58 loc) • 3.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.genService = exports.getServiceName = exports.genProxyCode = void 0;
var path = require("path");
exports.genProxyCode = function (appName, item, serviceInfo) {
var line = "\nexport const " + item.name + " = " + (item.isAsync ? 'async' : '') + " (" + (item.param ? item.param.map(function (x) { return x.text; }).join(', ') : '') + ") => {\n if (Core.isRemote('" + appName + "', '" + serviceInfo.module + "', '" + serviceInfo.name + "')) {\n " + (item.isAsync ? 'return await' : 'return new Promise((resolve, reject) => {\n ') + " Core.invoke({\n " + (item.isAsync ? '' : ' ') + " identity: {\n " + (item.isAsync ? ' ' : ' ') + " application: '" + appName + "',\n " + (item.isAsync ? ' ' : ' ') + " module: '" + serviceInfo.module + "',\n " + (item.isAsync ? ' ' : ' ') + " name: '" + serviceInfo.name + "',\n " + (item.isAsync ? ' ' : ' ') + " version: '*'\n " + (item.isAsync ? ' ' : ' ') + "},\n " + (item.isAsync ? ' ' : ' ') + "entries: []\n " + (item.isAsync ? '' : ' ') + "}" +
("" + (item.param && item.param.length > 0 ? ', ' : '')) +
((item.param ? item.param.map(function (x) { return x.name; }).join(', ') : '') + ")") +
((item.isAsync ? '' : '.then(res => resolve(res)).catch(e => reject(e))\n })') + "\n } else {\n return " + (item.isAsync ? 'await' : '') + " origin." + item.name + "(" + (item.param ? item.param.map(function (x) { return x.name; }).join(', ') : '') + ")\n }\n}\n");
return line;
};
exports.getServiceName = function (s) {
return s.module + ":" + s.name;
};
exports.genService = function (item, fileName, sourceFilePath) {
var dirname = path.dirname(sourceFilePath);
var moduleName = (fileName === 'index') ? dirname : dirname + "/" + fileName;
if (moduleName[0] !== '/')
moduleName = '/' + moduleName;
if (moduleName.toLowerCase() === '/src')
moduleName = '/';
else if (moduleName.toLowerCase().indexOf('/src/') >= 0) {
moduleName = moduleName.substr(moduleName.toLowerCase().indexOf('/src/') + 4);
}
var filepath = sourceFilePath;
if (filepath.indexOf('src') === 0)
filepath = filepath.substr(3);
else if (filepath.indexOf('/src') === 0)
filepath = filepath.substr(4);
else if (filepath.indexOf('./src') === 0)
filepath = filepath.substr(5);
if (filepath === '')
filepath = '/';
var serviceInfo = {
name: item.name,
module: moduleName,
arguments: [],
returnType: 'any',
isAsync: item.isAsync,
filepath: filepath
};
if (item.returnType)
serviceInfo.returnType = item.returnType;
if (item.param) {
for (var _i = 0, _a = item.param; _i < _a.length; _i++) {
var p = _a[_i];
var _b = p.text.replace(/ /g, '').split('='), def = _b[0], defaultValue = _b[1];
var _c = def.split(':'), name_1 = _c[0], type = _c[1];
var arg = {
name: name_1, type: type
};
if (defaultValue)
arg.default = defaultValue;
serviceInfo.arguments.push(arg);
}
}
return serviceInfo;
};