"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.simpleSpawn = void 0;
/**
 * Copyright 2017-2022 Plexus Interop Deutsche Bank AG
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed 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.
 */
var child_process_1 = require("child_process");
function simpleSpawn(execPath, args, printOutput) {
    if (args === void 0) { args = []; }
    if (printOutput === void 0) { printOutput = false; }
    return new Promise(function (resolve, reject) {
        var errorsBuffer = [];
        var child = (0, child_process_1.spawn)(execPath, args, {
            detached: true,
        });
        child.stdout.on('data', function (data) {
            if (printOutput) {
                console.log("" + data);
            }
        });
        child.stderr.on('data', function (data) {
            if (printOutput) {
                console.error("" + data);
            }
            else {
                errorsBuffer.push("" + data);
            }
        });
        child.on('exit', function (code) {
            if (code !== 0) {
                if (errorsBuffer.length > 0) {
                    console.error(errorsBuffer.join('\n'));
                }
                reject(new Error("Child process completed with error code: " + code + ", please use --verbose flag to see whole output"));
            }
            else {
                resolve();
            }
        });
        child.on('error', function (error) {
            console.error('Process finished with error', error);
            reject(error);
        });
    });
}
exports.simpleSpawn = simpleSpawn;
//# sourceMappingURL=process.js.map