"use strict";
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 path_1 = require("path");
const child_process_1 = require("child_process");
const startFlag = 'Output at:';
/**
 * 根据配置信息从jar class文件中提取ast信息
 *
 * @param {IDubboExtInfo} extraParam
 * @returns {Promise<{err: Error; res: IExtraResult}>}
 */
async function extra(extraParam) {
    return new Promise((resolve, reject) => {
        let execCmd = child_process_1.spawn(`java`, [
            '-jar',
            path_1.join(__dirname, '../../ext/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)) {
                let beginIndex = output.indexOf(startFlag) + startFlag.length;
                jarDir = output.substring(beginIndex).trim();
            }
        });
        execCmd.stderr.on('data', (rowData) => {
            err += rowData.toString("utf8");
        });
        execCmd.on('close', code => {
            if (err) {
                reject(new Error(`exitCode:${code}  errorInfo:${err}`));
            }
            else if (!jarDir) {
                reject(new Error('解析失败未获取输出文件路径'));
            }
            else {
                resolve({
                    jarInfo: path_1.join(jarDir, '/output/deflated.json'),
                    jarDir,
                });
            }
        });
    });
}
exports.extra = extra;
