diff --git a/README.md b/README.md index 02f7ca0..04816cc 100644 --- a/README.md +++ b/README.md @@ -213,6 +213,32 @@ Skips any post-request scripts during conversion $ postman-to-k6 collection.json --skip-pre -o k6-script.js ``` +### CLI options file + +Manage all the CLI options in a separate configuration file and pass them along to the postman-to-k6 command. +To make the CLI usage easier, especially in CI/CD implementations. + +All the available CLI options can be used in the config file. By passing the CLI options as parameters, you can overwrite the defined CLI options defined in the file. + +| Flag | Verbose | Default | +| ---- | -------------------- | ------- | +| | `--cli-options-file` | false | + +```shell +$ postman-to-k6 collection.json --cli-options-file cli-config.json +``` + +Example of JSON CLI config file + +```json +{ + "output": "k6-script.js", + "k6-params": "config/k6-params.json", + "environment": "config/envs/team.env.json", + "separate": true +} +``` + ## Docker Usage Using the Docker image, you execute the tool as follows: diff --git a/bin/postman-to-k6.js b/bin/postman-to-k6.js index adc4b3b..92c4757 100755 --- a/bin/postman-to-k6.js +++ b/bin/postman-to-k6.js @@ -1,6 +1,7 @@ #!/usr/bin/env node const convertFile = require('../lib/convert/file'); +const utils = require('../lib/convert/utils'); const fs = require('fs-extra'); const outputRequests = require('./requests'); const path = require('path'); @@ -19,9 +20,10 @@ program .option('-i, --iterations ', 'Number of iterations.') .option('-g, --global ', 'JSON export of global variables.') .option('-e, --environment ', 'JSON export of environment.') + .option('--cli-options-file ', 'postman-to-k6 CLI options file. Useful for CI/CD integrations.') .option('-c, --csv ', 'CSV data file. Used to fill data variables.') .option('-j, --json ', 'JSON data file. Used to fill data variables.') - .option('--k6-params ', 'K6 param options config file. Used to set the K6 params used during HTTP requests.') + .option('--k6-params ', 'K6 param options config file. Sets K6 params used during HTTP requests.') .option('--skip-pre', 'Skips pre-request scripts') .option('--skip-post', 'Skips post-request scripts') .option('--oauth1-consumer-key ', 'OAuth1 consumer key.') @@ -45,9 +47,27 @@ async function run(...args) { console.error('Provide path to Postman collection'); return; } - const options = args.pop(); + let options = args.pop(); const input = args.shift(); + let cliOptions = {}; + if (options.cliOptionsFile) { + try { + const cliOptionsFilePath = path.resolve(options.cliOptionsFile); + cliOptions = JSON.parse(await fs.readFile(cliOptionsFilePath, 'utf8')); + cliOptions = utils.keysToCamel(cliOptions); + } catch (err) { + console.error( + '\x1b[31m', + `postman-to-k6 CLI options error - no such file or directory "${options.cliOptionsFile}"` + ); + process.exit(1); + } + } + + // Merge CLI configuration file with CLI parameters + options = Object.assign({}, cliOptions, options); + // Convert let main, requests; try { diff --git a/lib/convert/utils.js b/lib/convert/utils.js new file mode 100644 index 0000000..06212a6 --- /dev/null +++ b/lib/convert/utils.js @@ -0,0 +1,41 @@ +function isArray(a) { + return Array.isArray(a); +} + +function isObject(o) { + return o === Object(o) && !isArray(o) && typeof o !== 'function'; +} + +function keysToCamel(o) { + if (isObject(o)) { + const n = {}; + + Object.keys(o).forEach(k => { + n[toCamel(k)] = keysToCamel(o[k]); + }); + + return n; + } else if (isArray(o)) { + return o.map(i => { + return keysToCamel(i); + }); + } + + return o; +} + +function toCamel(s) { + return s.replace(/([-_][a-z])/gi, $1 => { + return $1 + .toUpperCase() + .replace('-', '') + .replace('_', ''); + }); +} + +Object.assign(exports, { + keysToCamel, + isArray, + isObject, + toCamel, +});