@apideck/portman
Version:
Port OpenAPI Spec to Postman Collection, with contract & variation tests included
50 lines • 2.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeCollectionTestScripts = void 0;
var tslib_1 = require("tslib");
var fs_1 = tslib_1.__importDefault(require("fs"));
var postman_collection_1 = require("postman-collection");
var writeCollectionTestScripts = function (collection, scripts) {
var collectionEvents = (collection === null || collection === void 0 ? void 0 : collection.event) || [];
var testEvent = collectionEvents && collectionEvents.find(function (e) { return (e === null || e === void 0 ? void 0 : e.listen) === 'test'; }, null);
if (!testEvent) {
testEvent = {
listen: 'test',
script: {
exec: [],
type: 'text/javascript'
}
};
}
var scriptContents = scripts.map(function (src) {
if (src.startsWith('file:')) {
return getScriptContent(src.replace('file:', ''));
}
else {
return src;
}
});
var script = new postman_collection_1.Script(testEvent.script);
if (script.exec === undefined)
script.exec = [];
var exec = Array.isArray(script.exec)
? [].concat(Array.from(script.exec), Array.from(scriptContents))
: [script.exec].concat(Array.from(scriptContents));
script.update({ exec: exec.filter(function (i) { return Boolean(i); }) });
testEvent.script = script.toJSON();
collection.event = (collection === null || collection === void 0 ? void 0 : collection.event)
? [].concat(Array.from(collection.event), [testEvent])
: [testEvent];
return collection;
};
exports.writeCollectionTestScripts = writeCollectionTestScripts;
function getScriptContent(scriptPath) {
try {
return fs_1.default.readFileSync(scriptPath, { encoding: 'utf8', flag: 'r' });
}
catch (ex) {
console.error('\x1b[31m', "Config collection test script file error - no such file or directory \"".concat(scriptPath, "\""));
process.exit(1);
}
}
//# sourceMappingURL=writeCollectionTestScripts.js.map