@apideck/portman
Version:
Port OpenAPI Spec to Postman Collection, with contract & variation tests included
259 lines • 13.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.promptInit = void 0;
var tslib_1 = require("tslib");
var fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
var prompts_1 = tslib_1.__importDefault(require("prompts"));
var promptInit = function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var config, localPath, consoleLine, choiceExec, postmanApiKey, choiceLocation, local, url, output, location_1, choiceEnv, envFile, choicePortmanConfigFile, portmanConfigFile, choicePostmanConfigFile, postmanConfigFile, includeTests, syncPostman, postmanApiKey, runNewman, portmanCliFilePath, localFolder, postmanApiKey, cleanConfig, portmanCliSettings, envFilePath, envContent, err_1;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
config = {};
localPath = JSON.stringify(process.cwd());
consoleLine = process.stdout.columns ? '='.repeat(process.stdout.columns) : '='.repeat(80);
console.log('\x1b[32m', consoleLine);
console.log(" Portman Configuration Utility");
console.log('\x1b[32m', consoleLine);
return [4, (0, prompts_1.default)({
type: 'select',
name: 'choiceExec',
message: 'Welcome to Portman, how can we help?',
choices: [
{
title: 'Configure my CLI options',
value: 'portmanConfig'
},
{ title: 'Enter my Postman API key', value: 'postmanApiKey' }
],
initial: 0
})];
case 1:
choiceExec = (_a.sent()).choiceExec;
if (!(choiceExec === 'postmanApiKey')) return [3, 3];
return [4, (0, prompts_1.default)({
type: 'text',
name: 'postmanApiKey',
message: 'For more details on how to get your Postman API key see https://github.com/apideck-libraries/portman/tree/main#configure-automatic-upload-to-postman-app\n' +
'Enter your Postman API key:'
})];
case 2:
postmanApiKey = (_a.sent()).postmanApiKey;
config.postmanApiKey = postmanApiKey;
_a.label = 3;
case 3:
if (!(choiceExec === 'portmanConfig')) return [3, 25];
return [4, (0, prompts_1.default)({
type: 'select',
name: 'choiceLocation',
message: 'Where is your OpenAPI specification located?',
choices: [
{
title: 'REMOTE',
value: 'remote',
description: 'The OpenAPI spec is remote, on a publicly available URL'
},
{ title: 'LOCAL', value: 'local', description: 'The OpenAPI spec is a local file' }
],
initial: 0
})];
case 4:
choiceLocation = (_a.sent()).choiceLocation;
if (!(choiceLocation === 'local')) return [3, 6];
return [4, (0, prompts_1.default)({
type: 'text',
name: 'local',
message: 'Enter the local file path to your OpenAPI specification:'
})];
case 5:
local = (_a.sent()).local;
config.local = local;
_a.label = 6;
case 6:
if (!(choiceLocation === 'remote')) return [3, 8];
return [4, (0, prompts_1.default)({
type: 'text',
name: 'url',
message: 'Enter the publicly available URL of your OpenAPI specification:',
initial: 'https://'
})];
case 7:
url = (_a.sent()).url;
config.url = url;
_a.label = 8;
case 8: return [4, (0, prompts_1.default)({
type: 'text',
name: 'output',
message: "Define the path to store the generated the Postman collection, relative to ".concat(localPath, "\n") +
"Enter the output file path:",
initial: "collection.postman.json"
})];
case 9:
output = (_a.sent()).output;
config.output = output;
return [4, (0, prompts_1.default)({
type: 'text',
name: 'location',
message: "Where do you want to save the Portman CLI settings, relative to ".concat(localPath, "\n") +
"Enter the local folder path:",
initial: "portman"
})];
case 10:
location_1 = (_a.sent()).location;
config.localFolder = location_1;
return [4, (0, prompts_1.default)({
type: 'confirm',
name: 'choiceEnv',
message: 'Do you want to use a .ENV file to pass variables to Postman?',
initial: true
})];
case 11:
choiceEnv = (_a.sent()).choiceEnv;
if (!choiceEnv) return [3, 13];
return [4, (0, prompts_1.default)({
type: 'text',
name: 'envFile',
message: "Define the file path for your .env file, relative to ".concat(localPath, "\n") +
"Enter the .env file path:",
initial: config.localFolder + '/.env-portman'
})];
case 12:
envFile = (_a.sent()).envFile;
config.envFile = envFile;
_a.label = 13;
case 13: return [4, (0, prompts_1.default)({
type: 'toggle',
name: 'choicePortmanConfigFile',
message: 'Use a custom Portman configuration file or the default "portman-config.default.json"?',
initial: true,
active: 'custom',
inactive: 'default'
})];
case 14:
choicePortmanConfigFile = (_a.sent()).choicePortmanConfigFile;
if (!choicePortmanConfigFile) return [3, 16];
return [4, (0, prompts_1.default)({
type: 'text',
name: 'portmanConfigFile',
message: "Let's define the file path to your Portman configuration file, relative to ".concat(localPath, "\n") +
"Enter the Portman configuration file path:",
initial: config.localFolder + '/portman-config.json'
})];
case 15:
portmanConfigFile = (_a.sent()).portmanConfigFile;
config.portmanConfigFile = portmanConfigFile;
_a.label = 16;
case 16: return [4, (0, prompts_1.default)({
type: 'toggle',
name: 'choicePostmanConfigFile',
message: 'Use a custom openapi-to-postman configuration file or the default "postman-config.default.json"?',
initial: true,
active: 'default',
inactive: 'custom'
})];
case 17:
choicePostmanConfigFile = (_a.sent()).choicePostmanConfigFile;
if (!!choicePostmanConfigFile) return [3, 19];
return [4, (0, prompts_1.default)({
type: 'text',
name: 'postmanConfigFile',
message: "Define the file path to your custom openapi-to-postman configuration file, relative to ".concat(localPath, "\n") +
"Enter the openapi-to-postman configuration file path:",
initial: config.localFolder + '/openapi-to-postman-config.json'
})];
case 18:
postmanConfigFile = (_a.sent()).postmanConfigFile;
config.postmanConfigFile = postmanConfigFile;
_a.label = 19;
case 19: return [4, (0, prompts_1.default)({
type: 'toggle',
name: 'includeTests',
message: 'Inject automatic contract tests, based on OpenAPI into Postman?',
initial: true,
active: 'yes',
inactive: 'no'
})];
case 20:
includeTests = (_a.sent()).includeTests;
config.includeTests = includeTests;
return [4, (0, prompts_1.default)({
type: 'toggle',
name: 'syncPostman',
message: 'Upload the generated collection to Postman?',
initial: true,
active: 'yes',
inactive: 'no'
})];
case 21:
syncPostman = (_a.sent()).syncPostman;
config.syncPostman = syncPostman;
if (!syncPostman) return [3, 23];
return [4, (0, prompts_1.default)({
type: 'text',
name: 'postmanApiKey',
message: 'For more details on how to get your Postman API key see https://github.com/apideck-libraries/portman/tree/main#configure-automatic-upload-to-postman-app\n' +
'Enter your Postman API key:'
})];
case 22:
postmanApiKey = (_a.sent()).postmanApiKey;
config.postmanApiKey = postmanApiKey;
_a.label = 23;
case 23: return [4, (0, prompts_1.default)({
type: 'toggle',
name: 'runNewman',
message: 'Run Newman after the Portman conversion?',
initial: false,
active: 'yes',
inactive: 'no'
})];
case 24:
runNewman = (_a.sent()).runNewman;
config.runNewman = runNewman;
_a.label = 25;
case 25:
if (config.localFolder && (config.url || config.local)) {
portmanCliFilePath = "".concat(config === null || config === void 0 ? void 0 : config.localFolder, "/portman-cli.json");
localFolder = config.localFolder, postmanApiKey = config.postmanApiKey, cleanConfig = tslib_1.__rest(config, ["localFolder", "postmanApiKey"]);
portmanCliSettings = JSON.stringify(cleanConfig, null, 2);
try {
if (!fs_extra_1.default.existsSync(config.localFolder)) {
fs_extra_1.default.mkdirSync(config.localFolder);
}
fs_extra_1.default.outputFileSync(portmanCliFilePath, portmanCliSettings, 'utf8');
}
catch (err) {
console.error('\x1b[31m', "Output file error - no such directory \"./".concat(config === null || config === void 0 ? void 0 : config.localFolder, "\""));
process.exit(1);
}
console.log('\x1b[32m', consoleLine);
console.log(" Configuration Complete");
console.log('\x1b[32m', consoleLine);
console.log("\nYour Portman configuration has been created in \"".concat(portmanCliFilePath, "\".\n") +
"You can execute Portman by running the following command: ");
console.log('\x1b[34m', "\n portman --cliOptionsFile ".concat(portmanCliFilePath, "\n"));
console.log('\x1b[32m', consoleLine);
}
if (!(config.envFile || config.postmanApiKey)) return [3, 29];
envFilePath = config.envFile || '.env';
envContent = '';
envContent += "### PORTMAN INJECTED VARIABLES\n";
if (config.postmanApiKey)
envContent += "\n### POSTMAN APP\nPOSTMAN_API_KEY=".concat(config.postmanApiKey);
_a.label = 26;
case 26:
_a.trys.push([26, 28, , 29]);
return [4, fs_extra_1.default.appendFile(envFilePath, envContent, { encoding: 'utf8' })];
case 27:
_a.sent();
return [3, 29];
case 28:
err_1 = _a.sent();
console.error('\x1b[31m', "Output file error - no such file directory \"./".concat(envFilePath, "\""));
process.exit(1);
return [3, 29];
case 29: return [2];
}
});
}); };
exports.promptInit = promptInit;
//# sourceMappingURL=promptInit.js.map