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