Skip to content

Commit c08048d

Browse files
author
Vishal Shingala
committed
Added options support through cli args
1 parent 6cca6be commit c08048d

File tree

2 files changed

+57
-14
lines changed

2 files changed

+57
-14
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,8 @@ function (err, result) {
8989
```
9090

9191
### Options:
92-
* `'schemaFaker'(boolean)`: whether to use json-schema-faker for schema conversion. Default: `true`
93-
* `'requestNameSource'(string)`: The strategy to use to generate request names. url: use the request's URL as the name, fallback: Use the summary/operationId/URL (in that order) Default: `fallback`
94-
* `'indentCharacter' (string)`: The character to use per level of indentation for JSON/XML data. Default: `' '(space)`
92+
93+
Check out complete list of options and their usage at [OPTIONS.md](/OPTIONS.md)
9594

9695
### ConversionResult
9796

@@ -169,9 +168,12 @@ The converter can be used as a CLI tool as well. The following [command line opt
169168
- `-p`, `--pretty`
170169
Used to pretty print the collection object while writing to a file
171170

172-
- `-c`, `--config`
171+
- `-O`, `--options`
173172
Used to supply options to the converter, for complete options details see [here](/OPTIONS.md)
174173

174+
- `-c`, `--options-config`
175+
Used to supply options to the converter through config file, for complete options details see [here](/OPTIONS.md)
176+
175177
- `-h`, `--help`
176178
Specifies all the options along with a few usage examples on the terminal
177179

@@ -181,9 +183,9 @@ The converter can be used as a CLI tool as well. The following [command line opt
181183
**Sample usage examples of the converter CLI**
182184

183185

184-
- Takes a specification (spec.yaml) as an input and writes to a file (collection.json) with pretty printing
186+
- Takes a specification (spec.yaml) as an input and writes to a file (collection.json) with pretty printing and using provided options
185187
```terminal
186-
$ openapi2postmanv2 -s spec.yaml -o collection.json -p
188+
$ openapi2postmanv2 -s spec.yaml -o collection.json -p -O folderStrategy=Tags,includeAuthInfoInExample=false
187189
```
188190

189191
- Testing the converter

bin/openapi2postmanv2.js

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,57 @@
11
#!/usr/bin/env node
2-
var program = require('commander'),
2+
var _ = require('lodash'),
3+
program = require('commander'),
34
Converter = require('../index.js'),
45
fs = require('fs'),
56
path = require('path'),
7+
availableOptions = require('../lib/options').getOptions('use', { usage: ['CONVERSION'] }),
68
inputFile,
79
outputFile,
810
prettyPrintFlag,
911
configFile,
12+
definedOptions,
1013
testFlag,
1114
swaggerInput,
1215
swaggerData;
1316

17+
/**
18+
* Parses comma separated options mentioned in command args and generates JSON object
19+
*
20+
* @param {String} value - User defined options value
21+
* @returns {Object} - Parsed option in format of JSON object
22+
*/
23+
function parseOptions (value) {
24+
let definedOptions = value.split(','),
25+
parsedOptions = {};
26+
27+
_.forEach(definedOptions, (definedOption) => {
28+
let option = definedOption.split('=');
29+
30+
if (option.length === 2 && _.includes(_.keys(availableOptions), option[0])) {
31+
try {
32+
// parse parsable data types (e.g. boolean, integer etc)
33+
parsedOptions[option[0]] = JSON.parse(option[1]);
34+
}
35+
catch (e) {
36+
// treat value as string if can not be parsed
37+
parsedOptions[option[0]] = option[1];
38+
}
39+
}
40+
else {
41+
console.warn('\x1b[33m%s\x1b[0m', 'Warning: Invalid defined option ', option[0]);
42+
}
43+
});
44+
return parsedOptions;
45+
}
46+
1447
program
1548
.version(require('../package.json').version, '-v, --version')
1649
.option('-s, --spec <spec>', 'Convert given OPENAPI 3.0.0 spec to Postman Collection v2.0')
1750
.option('-o, --output <output>', 'Write the collection to an output file')
1851
.option('-t, --test', 'Test the OPENAPI converter')
1952
.option('-p, --pretty', 'Pretty print the JSON file')
20-
.option('-c, --config <config>', 'JSON file containing Converter options');
21-
53+
.option('-C, --options-config <optionsConfig>', 'JSON file containing Converter options')
54+
.option('-O, --options <options>', 'comma separated list of options', parseOptions);
2255

2356
program.on('--help', function() {
2457
/* eslint-disable */
@@ -44,6 +77,7 @@ outputFile = program.output || false;
4477
testFlag = program.test || false;
4578
prettyPrintFlag = program.pretty || false;
4679
configFile = program.config || false;
80+
definedOptions = program.options || {};
4781
swaggerInput;
4882
swaggerData;
4983

@@ -58,14 +92,16 @@ swaggerData;
5892
function writetoFile(prettyPrintFlag, file, collection) {
5993
if (prettyPrintFlag) {
6094
fs.writeFile(file, JSON.stringify(collection, null, 4), (err) => {
61-
if (err) { console.log('Could not write to file', err); }
62-
console.log('Conversion successful', 'Collection written to file');
95+
if (err) { console.log('Could not write to file', err); } // eslint-disable-line no-console
96+
// eslint-disable-next-line no-console
97+
console.log('\x1b[32m%s\x1b[0m', 'Conversion successful, collection written to file');
6398
});
6499
}
65100
else {
66101
fs.writeFile(file, JSON.stringify(collection), (err) => {
67-
if (err) { console.log('Could not write to file', err); }
68-
console.log('Conversion successful', 'Collection written to file');
102+
if (err) { console.log('Could not write to file', err); } // eslint-disable-line no-console
103+
// eslint-disable-next-line no-console
104+
console.log('\x1b[32m%s\x1b[0m', 'Conversion successful, collection written to file');
69105
});
70106
}
71107
}
@@ -81,10 +117,15 @@ function convert(swaggerData) {
81117
// apply options from config file if present
82118
if (configFile) {
83119
configFile = path.resolve(configFile);
84-
console.log('Config file: ', configFile);
120+
console.log('Options Config file: ', configFile); // eslint-disable-line no-console
85121
options = JSON.parse(fs.readFileSync(configFile, 'utf8'));
86122
}
87123

124+
// override options provided via cli
125+
if (definedOptions) {
126+
options = definedOptions;
127+
}
128+
88129
Converter.convert({
89130
type: 'string',
90131
data: swaggerData

0 commit comments

Comments
 (0)