Skip to content

Commit

Permalink
Minor enhancement to convert the parameters to camelCased options for…
Browse files Browse the repository at this point in the history
… easier usage of the CLI options file (#20)

Co-authored-by: Tim <>
  • Loading branch information
thim81 authored Sep 20, 2021
1 parent 271c0f9 commit 47a7c20
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/apideck-libraries/postman-to-k6/compare/v1.8.0...HEAD)

## [1.8.1] - 2020-09-20

### Changed

- Minor enhancement to convert the CLI parameter to camelCased options for easier usage in the CLI options file

## [1.8.0] - 2020-09-18

### Added
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,17 @@ All the available CLI options can be used in the config file. By passing the CLI
$ 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
}
```

## Examples

A collection of Postman examples are located under `example`.
Expand Down
2 changes: 2 additions & 0 deletions bin/postman-to-k6.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -56,6 +57,7 @@ async function run(...args) {
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',
Expand Down
41 changes: 41 additions & 0 deletions lib/convert/utils.js
Original file line number Diff line number Diff line change
@@ -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,
});

0 comments on commit 47a7c20

Please sign in to comment.