@apideck/portman
Version:
Port OpenAPI Spec to Postman Collection, with contract & variation tests included
602 lines • 39.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Portman = void 0;
var tslib_1 = require("tslib");
var chalk_1 = tslib_1.__importDefault(require("chalk"));
var Either = tslib_1.__importStar(require("fp-ts/lib/Either"));
var fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
var node_emoji_1 = tslib_1.__importDefault(require("node-emoji"));
var path_1 = tslib_1.__importDefault(require("path"));
var postman_collection_1 = require("postman-collection");
var application_1 = require("./application");
var lib_1 = require("./lib");
var oas_1 = require("./oas");
var postman_1 = require("./postman");
var services_1 = require("./services");
var types_1 = require("./types");
var PortmanConfig_validator_1 = require("./utils/PortmanConfig.validator");
var PortmanError_1 = require("./utils/PortmanError");
var openapi_format_1 = require("openapi-format");
var lodash_1 = tslib_1.__importDefault(require("lodash"));
var Portman = (function () {
function Portman(options) {
var _this = this;
this.displayMissingTargets = function (track) {
var _a = _this, consoleLine = _a.consoleLine, warn = _a.options.warn;
if (warn === false) {
return;
}
var uniqueOperationIds = Array.from(new Set(track.openApiOperationIds));
var uniqueOperations = Array.from(new Set(track.openApiOperations));
if (uniqueOperationIds.length > 0 || uniqueOperations.length > 0) {
console.log(chalk_1.default.yellow("WARNING: The following targets are missing from the OpenAPI specification."));
if (uniqueOperationIds.length > 0) {
var idsList = uniqueOperationIds.join(', ');
console.log(chalk_1.default.yellow("operationId:\t\t".concat(idsList)));
}
if (uniqueOperations.length > 0) {
var opsList = uniqueOperations.join(', ');
console.log(chalk_1.default.yellow("openApiOperation:\t".concat(opsList)));
}
console.log(chalk_1.default.yellow(consoleLine));
}
};
this.options = options;
this.consoleLine = process.stdout.columns ? '='.repeat(process.stdout.columns) : '='.repeat(80);
}
Portman.prototype.run = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var err_1, message;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.before()];
case 1:
_a.sent();
if (!this.config)
return [2];
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4, this.parseOpenApiSpec()];
case 3:
_a.sent();
return [3, 5];
case 4:
err_1 = _a.sent();
message = err_1.toString();
console.error('\x1b[31m', "OAS File Error - Unable to process the OpenAPI file.");
console.error('\x1b[31m', message);
console.error('\x1b[31m', this.consoleLine);
process.exit(1);
return [3, 5];
case 5: return [4, this.convertToPostmanCollection()];
case 6:
_a.sent();
this.injectTestSuite();
this.injectVariationTests();
this.injectVariationOverwrites();
this.injectIntegrationTests();
this.moveContractTestsToFolder();
this.writePortmanCollectionToFile();
return [4, this.runNewmanSuite()];
case 7:
_a.sent();
return [4, this.syncCollectionToPostman()];
case 8:
_a.sent();
return [4, this.after()];
case 9: return [2, _a.sent()];
}
});
});
};
Portman.prototype.uploadOnly = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var localPostman, postmanJson, err_2;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
localPostman = this.options.output || '';
if (localPostman === '') {
throw new Error("Loading ".concat(localPostman, " failed."));
}
this.options.syncPostman = true;
return [4, this.before()];
case 1:
_a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
postmanJson = path_1.default.resolve(localPostman);
this.portmanCollection = new postman_collection_1.Collection(JSON.parse(fs_extra_1.default.readFileSync(postmanJson, 'utf8').toString()));
return [4, this.syncCollectionToPostman()];
case 3:
_a.sent();
return [3, 5];
case 4:
err_2 = _a.sent();
throw new Error("Loading ".concat(localPostman, " failed."));
case 5: return [2];
}
});
});
};
Portman.prototype.before = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var _a, consoleLine, _b, oaUrl, oaLocal, output, cliOptionsFile, collectionName, portmanConfigFile, portmanConfigPath, postmanConfigFile, filterFile, oaOutput, envFile, ignoreCircularRefs, includeTests, bundleContractTests, runNewman, newmanIterationData, syncPostman, configData, config;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
_a = this, consoleLine = _a.consoleLine, _b = _a.options, oaUrl = _b.oaUrl, oaLocal = _b.oaLocal, output = _b.output, cliOptionsFile = _b.cliOptionsFile, collectionName = _b.collectionName, portmanConfigFile = _b.portmanConfigFile, portmanConfigPath = _b.portmanConfigPath, postmanConfigFile = _b.postmanConfigFile, filterFile = _b.filterFile, oaOutput = _b.oaOutput, envFile = _b.envFile, ignoreCircularRefs = _b.ignoreCircularRefs, includeTests = _b.includeTests, bundleContractTests = _b.bundleContractTests, runNewman = _b.runNewman, newmanIterationData = _b.newmanIterationData, syncPostman = _b.syncPostman;
console.log(chalk_1.default.red(consoleLine));
oaUrl && console.log((0, chalk_1.default)(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["{cyan Remote Url: } \t\t{green ", "}"], ["{cyan Remote Url: } \\t\\t{green ", "}"])), oaUrl));
oaLocal && console.log((0, chalk_1.default)(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["{cyan Local Path: } \t\t{green ", "}"], ["{cyan Local Path: } \\t\\t{green ", "}"])), oaLocal));
output && console.log((0, chalk_1.default)(templateObject_3 || (templateObject_3 = tslib_1.__makeTemplateObject(["{cyan Output Path: } \t\t{green ", "}"], ["{cyan Output Path: } \\t\\t{green ", "}"])), output));
oaOutput && console.log((0, chalk_1.default)(templateObject_4 || (templateObject_4 = tslib_1.__makeTemplateObject(["{cyan OpenAPI Output Path: } \t{green ", "}"], ["{cyan OpenAPI Output Path: } \\t{green ", "}"])), oaOutput));
cliOptionsFile && console.log((0, chalk_1.default)(templateObject_5 || (templateObject_5 = tslib_1.__makeTemplateObject(["{cyan Portman CLI Config: } \t{green ", "}"], ["{cyan Portman CLI Config: } \\t{green ", "}"])), cliOptionsFile));
console.log((0, chalk_1.default)(templateObject_6 || (templateObject_6 = tslib_1.__makeTemplateObject(["{cyan Portman Config: } \t{green ", "}"], ["{cyan Portman Config: } \\t{green ", "}"])), portmanConfigFile ? portmanConfigFile : 'portman-config.default.json'));
console.log((0, chalk_1.default)(templateObject_7 || (templateObject_7 = tslib_1.__makeTemplateObject(["{cyan Postman Config: } \t{green ", "}"], ["{cyan Postman Config: } \\t{green ", "}"])), postmanConfigFile ? postmanConfigFile : 'postman-config.default.json'));
filterFile && console.log((0, chalk_1.default)(templateObject_8 || (templateObject_8 = tslib_1.__makeTemplateObject(["{cyan Filter Config: } \t{green ", "}"], ["{cyan Filter Config: } \\t{green ", "}"])), filterFile));
console.log((0, chalk_1.default)(templateObject_9 || (templateObject_9 = tslib_1.__makeTemplateObject(["{cyan Environment: } \t\t{green ", "}"], ["{cyan Environment: } \\t\\t{green ", "}"])), envFile));
console.log((0, chalk_1.default)(templateObject_10 || (templateObject_10 = tslib_1.__makeTemplateObject(["{cyan Inject Tests: } \t{green ", "}"], ["{cyan Inject Tests: } \\t{green ", "}"])), includeTests));
bundleContractTests &&
console.log((0, chalk_1.default)(templateObject_11 || (templateObject_11 = tslib_1.__makeTemplateObject(["{cyan Bundle Tests: } \t{green ", "}"], ["{cyan Bundle Tests: } \\t{green ", "}"])), bundleContractTests));
console.log((0, chalk_1.default)(templateObject_12 || (templateObject_12 = tslib_1.__makeTemplateObject(["{cyan Run Newman: } \t\t{green ", "}"], ["{cyan Run Newman: } \\t\\t{green ", "}"])), !!runNewman));
console.log((0, chalk_1.default)(templateObject_13 || (templateObject_13 = tslib_1.__makeTemplateObject(["{cyan Newman Iteration Data: }{green ", "}"], ["{cyan Newman Iteration Data: }{green ", "}"])), newmanIterationData ? newmanIterationData : false));
console.log((0, chalk_1.default)(templateObject_14 || (templateObject_14 = tslib_1.__makeTemplateObject(["{cyan Upload to Postman: } \t{green ", "} "], ["{cyan Upload to Postman: } \\t{green ", "} "])), syncPostman));
collectionName && console.log((0, chalk_1.default)(templateObject_15 || (templateObject_15 = tslib_1.__makeTemplateObject(["{cyan OpenAPI title: } \t{green ", "}"], ["{cyan OpenAPI title: } \\t{green ", "}"])), collectionName));
console.log(chalk_1.default.red(consoleLine));
if (ignoreCircularRefs) {
console.log(node_emoji_1.default.get(':see_no_evil:'), chalk_1.default.red("Ignoring circular references in OpenAPI Spec. Response validation is disabled for invalid schemas!"), node_emoji_1.default.get(':see_no_evil:'));
console.log(chalk_1.default.red(consoleLine));
}
return [4, fs_extra_1.default.ensureDir('./tmp/working/')];
case 1:
_c.sent();
return [4, fs_extra_1.default.ensureDir('./tmp/converted/')];
case 2:
_c.sent();
return [4, fs_extra_1.default.ensureDir('./tmp/newman/')];
case 3:
_c.sent();
return [4, (0, lib_1.getConfig)(portmanConfigPath)];
case 4:
configData = _c.sent();
if (Either.isLeft(configData)) {
return [2, PortmanError_1.PortmanError.render(configData.left)];
}
config = (0, PortmanConfig_validator_1.validate)(configData.right);
if (Either.isLeft(config)) {
console.log((0, chalk_1.default)(templateObject_16 || (templateObject_16 = tslib_1.__makeTemplateObject(["{red Invalid Portman Config: } \t\t{green ", "}"], ["{red Invalid Portman Config: } \\t\\t{green ", "}"])), portmanConfigPath));
console.log(config.left);
console.log(chalk_1.default.red(consoleLine));
process.exit(1);
}
else {
this.config = config.right;
}
return [2];
}
});
});
};
Portman.prototype.after = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var _a, consoleLine, collectionFile;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = this, consoleLine = _a.consoleLine, collectionFile = _a.collectionFile;
return [4, (0, lib_1.clearTmpDirectory)()];
case 1:
_b.sent();
console.log(chalk_1.default.green(consoleLine));
console.log(node_emoji_1.default.get(':rocket:'), (0, chalk_1.default)(templateObject_17 || (templateObject_17 = tslib_1.__makeTemplateObject(["{cyan Collection written to:} {green ", "}"], ["{cyan Collection written to:} {green ", "}"])), collectionFile), node_emoji_1.default.get(':rocket:'));
console.log(chalk_1.default.green(consoleLine));
this.displayMissingTargets(this.testSuite.track);
return [2];
}
});
});
};
Portman.prototype.parseOpenApiSpec = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var _a, oaLocal, oaUrl, filterFile, oaOutput, ignoreCircularRefs, collectionName, oasFormatter, openApiSpec, openApiObj, fileName, err_3, oaLocalPath, specExists, openApiSpecPath, err_4, oasParser, err_5, message, circularRef, docLink;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = this.options, oaLocal = _a.oaLocal, oaUrl = _a.oaUrl, filterFile = _a.filterFile, oaOutput = _a.oaOutput, ignoreCircularRefs = _a.ignoreCircularRefs, collectionName = _a.collectionName;
oasFormatter = new oas_1.OpenApiFormatter();
if (!oaUrl) return [3, 5];
_b.label = 1;
case 1:
_b.trys.push([1, 4, , 5]);
return [4, oasFormatter.parseFile(oaUrl)];
case 2:
openApiObj = _b.sent();
fileName = oaUrl.replace(/\/$/, '').split('?')[0].split('/').pop();
openApiSpec = "./tmp/".concat(fileName);
return [4, oasFormatter.writeFile(openApiSpec, openApiObj, { format: 'yaml' })];
case 3:
_b.sent();
return [3, 5];
case 4:
err_3 = _b.sent();
console.error('\x1b[31m', "OAS URL error - There is a problem with the url: \"".concat(oaUrl, "\""));
console.error('\x1b[31m', err_3);
process.exit(1);
return [3, 5];
case 5:
if (oaLocal) {
oaLocalPath = path_1.default.resolve(oaLocal);
if (fs_extra_1.default.existsSync(oaLocalPath)) {
openApiSpec = oaLocalPath;
}
else {
console.error('\x1b[31m', 'Local OAS error - no such file or directory "' + oaLocal + '"');
}
}
if (!openApiSpec) {
throw new Error("Error initializing OpenApi Spec.");
}
return [4, fs_extra_1.default.pathExists(openApiSpec)];
case 6:
specExists = _b.sent();
if (!specExists) {
throw new Error("".concat(openApiSpec, " doesn't exist. "));
}
if (!filterFile) return [3, 10];
_b.label = 7;
case 7:
_b.trys.push([7, 9, , 10]);
openApiSpecPath = oaOutput ? oaOutput : './tmp/converted/filtered.yml';
fs_extra_1.default.outputFileSync(openApiSpecPath, '', 'utf8');
return [4, oasFormatter.filter({
inputFile: openApiSpec,
filterFile: filterFile,
outputFile: openApiSpecPath
})];
case 8:
_b.sent();
openApiSpec = openApiSpecPath;
return [3, 10];
case 9:
err_4 = _b.sent();
throw new Error("Filter file error - ".concat(filterFile, " doesn't exist. "));
case 10:
oasParser = new oas_1.OpenApiParser();
_b.label = 11;
case 11:
_b.trys.push([11, 13, , 14]);
return [4, oasParser.convert({
inputFile: openApiSpec,
ignoreCircularRefs: ignoreCircularRefs
})];
case 12:
_b.sent();
return [3, 14];
case 13:
err_5 = _b.sent();
message = err_5.toString();
circularRef = message.includes('Circular $ref');
console.error('\x1b[31m', "OAS File Error - There is an issue with the OpenAPI file, preventing it from being parsed properly.");
console.error('\x1b[31m', message);
if (circularRef) {
docLink = 'https://github.com/apideck-libraries/portman/tree/main/docs/ERRORS.md';
console.error('\x1b[31m', "\nPlease see ".concat(docLink, " for more information about this error."));
}
console.error('\x1b[31m', this.consoleLine);
process.exit(1);
return [3, 14];
case 14:
this.oasParser = oasParser;
if (collectionName) {
this.oasParser.oas = (0, application_1.renamePostmanCollection)(this.oasParser.oas, this.options);
}
return [2];
}
});
});
};
Portman.prototype.convertToPostmanCollection = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var _a, postmanConfigPath, localPostman, oaToPostman, oasCopy, oaToPostmanConfig, postmanObj, postmanJson;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = this.options, postmanConfigPath = _a.postmanConfigPath, localPostman = _a.localPostman;
oaToPostman = new services_1.OpenApiToPostmanService();
oasCopy = lodash_1.default.cloneDeep(this.oasParser.oas);
oaToPostmanConfig = {
openApiObj: oasCopy,
outputFile: "".concat(process.cwd(), "/tmp/working/tmpCollection.json"),
configFile: postmanConfigPath
};
if (!localPostman) return [3, 1];
try {
postmanJson = path_1.default.resolve(localPostman);
postmanObj = JSON.parse(fs_extra_1.default.readFileSync(postmanJson, 'utf8').toString());
}
catch (err) {
throw new Error("Loading ".concat(localPostman, " failed."));
}
return [3, 3];
case 1: return [4, oaToPostman.convert(oaToPostmanConfig).catch(function (err) {
console.error('Postman Collection generation failed: ', err.toString());
process.exit(1);
})];
case 2:
postmanObj = _b.sent();
_b.label = 3;
case 3: return [4, this.runPortmanOverrides(postmanObj)];
case 4:
_b.sent();
this.postmanParser = new postman_1.PostmanParser({
collection: this.postmanCollection,
oasParser: this.oasParser
});
this.portmanCollection = this.postmanParser.collection.toJSON();
return [2];
}
});
});
};
Portman.prototype.injectTestSuite = function () {
var _a = this, config = _a.config, options = _a.options, oasParser = _a.oasParser, postmanParser = _a.postmanParser;
var testSuite = new application_1.TestSuite({ oasParser: oasParser, postmanParser: postmanParser, config: config, options: options });
if (options === null || options === void 0 ? void 0 : options.includeTests) {
testSuite.generateContractTests();
testSuite.injectContentTests();
}
testSuite.injectAssignVariables();
testSuite.injectExtendedTests();
testSuite.injectOverwrites();
testSuite.injectPreRequestScripts();
this.testSuite = testSuite;
this.portmanCollection = testSuite.collection.toJSON();
};
Portman.prototype.injectVariationTests = function () {
var _a, _b;
var _c = this, includeTests = _c.options.includeTests, testSuite = _c.testSuite;
if (includeTests &&
testSuite &&
((_a = testSuite === null || testSuite === void 0 ? void 0 : testSuite.variationTests) === null || _a === void 0 ? void 0 : _a.length) &&
((_b = testSuite === null || testSuite === void 0 ? void 0 : testSuite.variationTests) === null || _b === void 0 ? void 0 : _b.length) > 0) {
this.variationWriter = new application_1.VariationWriter({
testSuite: testSuite,
variationFolderName: 'Variation Tests'
});
testSuite.variationWriter = this.variationWriter;
testSuite.generateVariationTests();
this.portmanCollection = testSuite.collection.toJSON();
}
};
Portman.prototype.runPortmanOverrides = function (postmanCollection) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var _a, config, options, collectionWriter;
return tslib_1.__generator(this, function (_b) {
_a = this, config = _a.config, options = _a.options;
collectionWriter = new application_1.CollectionWriter(config, options, postmanCollection);
collectionWriter.execute();
this.postmanCollection = new postman_collection_1.Collection(collectionWriter.collection);
return [2];
});
});
};
Portman.prototype.injectIntegrationTests = function () {
var _a = this, includeTests = _a.options.includeTests, testSuite = _a.testSuite;
if (includeTests && testSuite) {
this.integrationTestWriter = new application_1.IntegrationTestWriter({
testSuite: testSuite,
integrationTestFolderName: 'Integration Tests'
});
testSuite.integrationTestWriter = this.integrationTestWriter;
testSuite.generateIntegrationTests();
this.portmanCollection = testSuite.collection.toJSON();
}
};
Portman.prototype.injectVariationOverwrites = function () {
var _this = this;
var _a = this, testSuite = _a.testSuite, variationWriter = _a.variationWriter;
if (!variationWriter || !testSuite)
return;
this.postmanParser.map(this.portmanCollection);
Object.entries(variationWriter.overwriteMap).map(function (_a) {
var _b = tslib_1.__read(_a, 2), id = _b[0], overwrites = _b[1];
var pmOperation = _this.postmanParser.getOperationByItemId(id);
pmOperation && testSuite.injectOverwrites([pmOperation], overwrites);
});
this.portmanCollection = this.postmanParser.collection.toJSON();
};
Portman.prototype.moveContractTestsToFolder = function () {
var _this = this;
if (!this.options.bundleContractTests)
return;
var pmOpsWithContractTest = [];
pmOpsWithContractTest = this.testSuite.requestTestTypes
.filter(function (obj) { return obj.reqTestType === types_1.PortmanTestTypes.contract; })
.map(function (obj) { return obj.postmanItemId; });
if (!pmOpsWithContractTest)
return;
var contractTestFolder = new postman_collection_1.ItemGroup({
name: "Contract Tests"
});
pmOpsWithContractTest.map(function (id) {
var pmOperation = _this.postmanParser.getOperationByItemId(id);
var target;
if (pmOperation) {
var parent_1 = pmOperation.getParent();
if (parent_1) {
parent_1 === null || parent_1 === void 0 ? void 0 : parent_1.items.remove(function (item) { return item.id === id; }, {});
if ((parent_1 === null || parent_1 === void 0 ? void 0 : parent_1.items.count()) === 0) {
_this.postmanParser.collection.items.remove(function (item) { return item.id === parent_1.id; }, {});
}
if (!postman_collection_1.Collection.isCollection(parent_1)) {
var folderName = parent_1.name;
var folder = contractTestFolder.oneDeep(folderName);
if (folder) {
target = folder;
}
else {
var newFolder = new postman_collection_1.ItemGroup({
name: folderName
});
contractTestFolder.items.add(newFolder);
target = newFolder;
}
}
else {
target = contractTestFolder;
}
target.items.add(pmOperation.item);
}
}
});
this.postmanParser.collection.items.prepend(contractTestFolder);
this.portmanCollection = this.postmanParser.collection.toJSON();
};
Portman.prototype.writePortmanCollectionToFile = function () {
var _a, _b, _c, _d, _e, _f;
var output = this.options.output;
var globals = this.config.globals;
var fileName = ((_b = (_a = this === null || this === void 0 ? void 0 : this.portmanCollection) === null || _a === void 0 ? void 0 : _a.info) === null || _b === void 0 ? void 0 : _b.name) || 'portman-collection';
var postmanCollectionFile = "./tmp/converted/".concat((0, openapi_format_1.changeCase)(fileName, 'camelCase'), ".json");
if (output) {
postmanCollectionFile = output;
if (!postmanCollectionFile.includes('.json')) {
console.error('\x1b[31m', 'Output file error - Only .json filenames are allowed for "' + postmanCollectionFile + '"');
process.exit(1);
}
}
try {
if (((_e = (_d = (_c = this.oasParser) === null || _c === void 0 ? void 0 : _c.oas) === null || _d === void 0 ? void 0 : _d.info) === null || _e === void 0 ? void 0 : _e.version) && ((_f = this.postmanParser) === null || _f === void 0 ? void 0 : _f.collection)) {
var postmanVersion = new postman_collection_1.Version(this.oasParser.oas.info.version);
if (postmanVersion.major !== undefined &&
postmanVersion.minor !== undefined &&
postmanVersion.patch !== undefined) {
this.postmanParser.collection.version = postmanVersion;
}
}
this.portmanCollection = this.postmanParser.collection.toJSON();
if (globals === null || globals === void 0 ? void 0 : globals.stripResponseExamples) {
this.portmanCollection = (0, application_1.stripResponseExamples)(this.portmanCollection);
}
var collectionString = JSON.stringify(this.portmanCollection, null, 2);
if (globals === null || globals === void 0 ? void 0 : globals.portmanReplacements) {
collectionString = (0, application_1.writeRawReplacements)(collectionString, globals.portmanReplacements);
this.portmanCollection = new postman_collection_1.Collection(JSON.parse(collectionString)).toJSON();
}
fs_extra_1.default.outputFileSync(postmanCollectionFile, collectionString, 'utf8');
this.collectionFile = postmanCollectionFile;
}
catch (err) {
console.error('\x1b[31m', 'Output file error - no such file or directory "' + postmanCollectionFile + '"');
process.exit(1);
}
};
Portman.prototype.runNewmanSuite = function () {
var _a, _b;
return tslib_1.__awaiter(this, void 0, void 0, function () {
var _c, consoleLine, _d, runNewman, baseUrl, newmanIterationData, _e, newmanRunOptions, fileName, newmanEnvFile, error_1;
return tslib_1.__generator(this, function (_f) {
switch (_f.label) {
case 0:
_c = this, consoleLine = _c.consoleLine, _d = _c.options, runNewman = _d.runNewman, baseUrl = _d.baseUrl, newmanIterationData = _d.newmanIterationData, _e = _d.newmanRunOptions, newmanRunOptions = _e === void 0 ? {} : _e;
if (!runNewman) return [3, 4];
fileName = ((_b = (_a = this === null || this === void 0 ? void 0 : this.portmanCollection) === null || _a === void 0 ? void 0 : _a.info) === null || _b === void 0 ? void 0 : _b.name) || 'portman-collection';
newmanEnvFile = "./tmp/newman/".concat(fileName, "-env.json");
(0, application_1.writeNewmanEnv)(this.portmanCollection, newmanEnvFile);
_f.label = 1;
case 1:
_f.trys.push([1, 3, , 4]);
console.log(chalk_1.default.green(consoleLine));
console.log((0, chalk_1.default)(templateObject_18 || (templateObject_18 = tslib_1.__makeTemplateObject(["{cyan Run Newman against: } {green ", "}"], ["{cyan Run Newman against: } {green ", "}"])), baseUrl));
console.log(chalk_1.default.green(consoleLine));
return [4, (0, application_1.runNewmanWith)(this.collectionFile, newmanEnvFile, newmanIterationData, newmanRunOptions)];
case 2:
_f.sent();
return [3, 4];
case 3:
error_1 = _f.sent();
console.log("\n");
console.log(chalk_1.default.red(consoleLine));
console.log(chalk_1.default.red("Newman run failed with: "));
console.log(error_1 === null || error_1 === void 0 ? void 0 : error_1.message);
process.exit(1);
return [3, 4];
case 4: return [2];
}
});
});
};
Portman.prototype.syncCollectionToPostman = function () {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
return tslib_1.__awaiter(this, void 0, void 0, function () {
var _l, portmanCollection, _m, syncPostman, postmanRefreshCache, postmanFastSync, syncPostmanCollectionIds, postmanUid, postmanWorkspaceName, postmanRefreshCacheValue, consoleLine, response, error_2, _o, status, data;
return tslib_1.__generator(this, function (_p) {
switch (_p.label) {
case 0:
_l = this, portmanCollection = _l.portmanCollection, _m = _l.options, syncPostman = _m.syncPostman, postmanRefreshCache = _m.postmanRefreshCache, postmanFastSync = _m.postmanFastSync, syncPostmanCollectionIds = _m.syncPostmanCollectionIds;
if (!syncPostman)
return [2];
postmanUid = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.postmanUid)
? this.options.postmanUid
: process.env.POSTMAN_COLLECTION_UID || '';
postmanWorkspaceName = ((_b = this.options) === null || _b === void 0 ? void 0 : _b.postmanWorkspaceName)
? this.options.postmanWorkspaceName
: process.env.POSTMAN_WORKSPACE_NAME || '';
postmanRefreshCacheValue = postmanRefreshCache;
consoleLine = process.stdout.columns ? '='.repeat(process.stdout.columns) : '='.repeat(80);
if (postmanUid !== '') {
postmanRefreshCacheValue = true;
}
this.postmanSyncService = new services_1.PostmanSyncService({
postmanUid: postmanUid,
postmanWorkspaceName: postmanWorkspaceName,
portmanCollection: portmanCollection,
postmanRefreshCache: postmanRefreshCacheValue,
postmanFastSync: postmanFastSync,
syncPostmanCollectionIds: syncPostmanCollectionIds
});
_p.label = 1;
case 1:
_p.trys.push([1, 3, , 4]);
return [4, this.postmanSyncService.sync()];
case 2:
response = _p.sent();
return [3, 4];
case 3:
error_2 = _p.sent();
console.log("\n");
console.log(chalk_1.default.red(consoleLine));
console.log(chalk_1.default.red("Postman sync failed with: "));
console.log(node_emoji_1.default.get(':cold_sweat:'), chalk_1.default.yellow((error_2 === null || error_2 === void 0 ? void 0 : error_2.message) || error_2));
console.log(chalk_1.default.red(consoleLine));
process.exit(1);
return [3, 4];
case 4:
_o = JSON.parse(response), status = _o.status, data = _o.data;
if (status === 'success') {
((_c = this.postmanSyncService) === null || _c === void 0 ? void 0 : _c.postmanWorkspaceName) &&
console.log((0, chalk_1.default)(templateObject_19 || (templateObject_19 = tslib_1.__makeTemplateObject(["{cyan -> Postman Workspace: }{green ", "}"], ["{cyan -> Postman Workspace: }{green ", "}"])), (_d = this.postmanSyncService) === null || _d === void 0 ? void 0 : _d.postmanWorkspaceName));
console.log((0, chalk_1.default)(templateObject_20 || (templateObject_20 = tslib_1.__makeTemplateObject(["{cyan -> Postman Name: } \t {green ", "}"], ["{cyan -> Postman Name: } \\t {green ", "}"])), ((_e = data === null || data === void 0 ? void 0 : data.collection) === null || _e === void 0 ? void 0 : _e.name) || ((_f = this.postmanSyncService) === null || _f === void 0 ? void 0 : _f.collectionName)));
console.log((0, chalk_1.default)(templateObject_21 || (templateObject_21 = tslib_1.__makeTemplateObject(["{cyan -> Postman UID: } \t {green ", "}"], ["{cyan -> Postman UID: } \\t {green ", "}"])), ((_g = data === null || data === void 0 ? void 0 : data.collection) === null || _g === void 0 ? void 0 : _g.uid) || this.postmanSyncService.postmanUid));
}
if (status === 'fail') {
console.log((0, chalk_1.default)(templateObject_22 || (templateObject_22 = tslib_1.__makeTemplateObject(["{red -> Postman Name: } \t", ""], ["{red -> Postman Name: } \\t", ""])), ((_h = portmanCollection === null || portmanCollection === void 0 ? void 0 : portmanCollection.info) === null || _h === void 0 ? void 0 : _h.name) || ((_j = this.postmanSyncService) === null || _j === void 0 ? void 0 : _j.collectionName)));
console.log((0, chalk_1.default)(templateObject_23 || (templateObject_23 = tslib_1.__makeTemplateObject(["{red -> Postman UID: } \t", ""], ["{red -> Postman UID: } \\t", ""])), ((_k = data === null || data === void 0 ? void 0 : data.collection) === null || _k === void 0 ? void 0 : _k.uid) || this.postmanSyncService.postmanUid));
console.log(data === null || data === void 0 ? void 0 : data.error);
console.log("\n");
console.log(chalk_1.default.red(consoleLine));
process.exit(1);
}
return [2];
}
});
});
};
return Portman;
}());
exports.Portman = Portman;
var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6, templateObject_7, templateObject_8, templateObject_9, templateObject_10, templateObject_11, templateObject_12, templateObject_13, templateObject_14, templateObject_15, templateObject_16, templateObject_17, templateObject_18, templateObject_19, templateObject_20, templateObject_21, templateObject_22, templateObject_23;
//# sourceMappingURL=Portman.js.map