"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GenTsCommand = void 0;
var tslib_1 = require("tslib");
/**
 * 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 path = require("path");
var files_1 = require("../common/files");
var java_1 = require("../common/java");
var process_1 = require("../common/process");
var protoJs_1 = require("../common/protoJs");
var BaseCommand_1 = require("./BaseCommand");
var DefaultOptions_1 = require("./DefaultOptions");
var GenProtoCommand_1 = require("./GenProtoCommand");
var GenTsCommand = /** @class */ (function (_super) {
    (0, tslib_1.__extends)(GenTsCommand, _super);
    function GenTsCommand() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.protoRegexp = /.+\.proto$/;
        _this.descriptorPathRegexp = /.*google[/\\]+protobuf[/\\]+descriptor.proto|.*interop[/\\]+options.proto$/;
        _this.clientGenArgs = function (opts) { return (0, tslib_1.__spreadArray)(['--type=ts'], _this.optionArgs(opts), true); };
        _this.name = function () { return 'gen-ts'; };
        _this.generalDescription = function () { return 'generate Typescript client and messages definitions for specified entry point'; };
        _this.options = function () { return [
            (0, DefaultOptions_1.baseDir)(),
            (0, DefaultOptions_1.out)(),
            (0, DefaultOptions_1.plexusEntryPoint)(),
            (0, DefaultOptions_1.namespace)(),
            (0, DefaultOptions_1.verbose)(),
            (0, DefaultOptions_1.excludePattern)(),
        ]; };
        return _this;
    }
    GenTsCommand.prototype.action = function (opts) {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
            var protoFilesDir, protoGenCommand, excludePatternString, jsFilePath, protoFiles, tsFileName, javaExecPath, javaLibPath;
            var _this = this;
            return (0, tslib_1.__generator)(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.log('Generating proto definitions');
                        protoFilesDir = path.join(opts.out, 'tmp');
                        (0, files_1.mkdirsSync)(protoFilesDir);
                        protoGenCommand = new GenProtoCommand_1.GenProtoCommand();
                        return [4 /*yield*/, protoGenCommand.action((0, tslib_1.__assign)((0, tslib_1.__assign)({}, opts), { out: protoFilesDir }))];
                    case 1:
                        _a.sent();
                        excludePatternString = opts.excludePattern;
                        this.log('Generating proto messages JS definitions');
                        jsFilePath = path.join(opts.out, 'plexus-messages.js');
                        return [4 /*yield*/, (0, files_1.listFiles)(protoFilesDir, this.protoRegexp)];
                    case 2:
                        protoFiles = (_a.sent())
                            .filter(function (f) { return !_this.isProtoDescriptorPath(f); })
                            .filter(function (f) { return !excludePatternString || excludePatternString.length === 0 || !new RegExp(excludePatternString).test(f); });
                        return [4 /*yield*/, (0, protoJs_1.genJsStaticModule)(jsFilePath, protoFiles, opts.namespace)];
                    case 3:
                        _a.sent();
                        this.log('Deleting proto definitions');
                        (0, files_1.removeSync)(protoFilesDir);
                        this.log('Generating proto messages TS definitions');
                        tsFileName = 'plexus-messages.d.ts';
                        return [4 /*yield*/, (0, protoJs_1.genTsStaticModule)(path.join(opts.out, tsFileName), jsFilePath)];
                    case 4:
                        _a.sent();
                        this.log('Generating interop client');
                        return [4 /*yield*/, (0, java_1.getJavaExecPath)()];
                    case 5:
                        javaExecPath = _a.sent();
                        javaLibPath = (0, java_1.getJavaGenLibPath)();
                        return [4 /*yield*/, (0, process_1.simpleSpawn)(javaExecPath, (0, tslib_1.__spreadArray)(['-jar', javaLibPath], this.clientGenArgs(opts), true), opts.verbose === 'true')];
                    case 6:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    GenTsCommand.prototype.isProtoDescriptorPath = function (testPath) {
        return this.descriptorPathRegexp.test(testPath);
    };
    return GenTsCommand;
}(BaseCommand_1.BaseCommand));
exports.GenTsCommand = GenTsCommand;
//# sourceMappingURL=GenTsCommand.js.map