interpret-dubbo2js
Version:
apache dubbo & dubbo-js interpret java-jar file to typescript files
102 lines (101 loc) • 3.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const child_process_1 = require("child_process");
const fs_extra_1 = require("fs-extra");
const path_1 = require("path");
const startFlag = 'Output at:';
/**
* 根据配置信息从jar class文件中提取ast信息
*
* @param {IDubboExtInfo} extraParam
* @returns {Promise<{err: Error; res: IExtraResult}>}
*/
async function extra(extraParam) {
await checkConfigPath([extraParam.entryJarPath, extraParam.libDirPath]);
return new Promise(async (resolve, reject) => {
let execCmd = child_process_1.spawn(`java`, [
'-jar',
require.resolve('jexpose/jexpose-1.3.jar'),
extraParam.entry,
extraParam.entryJarPath,
extraParam.libDirPath,
extraParam.providerSuffix || 'Provider',
]);
let err = '';
let jarDir = '';
execCmd.stdout.setEncoding('utf8');
execCmd.stderr.setEncoding('utf8');
execCmd.stdout.on('data', (rowData) => {
let output = rowData.toString('utf8');
if (output.includes(startFlag)) {
jarDir = output.match(/Output at :(.*)(\nelapsed.*?s)?/)[1];
}
});
execCmd.stderr.on('data', (rowData) => {
err += rowData.toString('utf8');
});
execCmd.on('close', code => {
if (jarDir) {
resolve({
jarInfo: path_1.join(jarDir, '/output/deflated.json'),
jarDir,
});
}
if (err) {
console.error(`exitCode:${code} errorInfo:${err}`);
}
else if (!jarDir) {
reject(new Error('解析失败未获取输出文件路径'));
}
else {
}
});
});
}
exports.extra = extra;
/**
* 验证文件路径是否存在
*
* @param fileOrDirPath
*/
async function checkConfigPath(fileOrDirPath) {
let isOk = true;
for (const fileItempath of fileOrDirPath) {
let isExist = await isPathExist(fileItempath);
if (!isExist) {
isOk = false;
console.warn(`文件路径配置不正确:${fileItempath}`);
}
}
return isOk;
}
/**
* 验证文件或文件夹
* @param fileOrDirPath
*/
async function isPathExist(fileOrDirPath) {
return new Promise((resolve, reject) => {
fs_extra_1.pathExists(fileOrDirPath, (err, exists) => {
if (err) {
return resolve(false);
}
resolve(exists);
});
});
}