This repository has been archived by the owner on Dec 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
95 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env node | ||
|
||
const p2o = require('./p2o') | ||
|
||
p2o.parse(process.argv.slice(2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,56 @@ | ||
#!/usr/bin/env node | ||
const yargs = require('yargs/yargs'); | ||
const { hideBin } = require('yargs/helpers'); | ||
const postmanToOpenApi = require('../lib/index'); | ||
const fs = require('fs'); | ||
const yargs = require('yargs') | ||
const { readFileSync } = require('fs') | ||
const postmanToOpenApi = require('../lib') | ||
|
||
const args = yargs(hideBin(process.argv)) | ||
.usage('$0 output', 'Convert the input file, usually json, ' + | ||
'with Postman collections to OpenAPI by applying the given options file, also json, ' + | ||
'and save as the output file, usually yml.', (yargs) => { | ||
yargs | ||
.positional('output', { | ||
desc: 'Save the output OpenAPI with this name', | ||
type: 'string', | ||
}) | ||
.options('i', { | ||
alias: 'input', | ||
demandOption: true, | ||
desc: 'Path to the Postman collection json file', | ||
type: 'string', | ||
}) | ||
.options('o', { | ||
alias: 'options', | ||
desc: 'Path to the Options json file', | ||
type: 'string', | ||
}) | ||
}).argv; | ||
// TODO | ||
// - handle error in a homogeneous way | ||
// - add example usage | ||
// - add test for all code here (less cli i think....) | ||
// - check the input field or should be test inside library? | ||
// - silent option to not return in console the result if we have the file... | ||
|
||
const json_str = args.options && fs.readFileSync(args.options, 'utf8'); | ||
const options = json_str && JSON.parse(json_str); | ||
console.log(`Converting ${args.input} to ${args.output}, with options:\n${json_str || 'NONE'}`); | ||
|
||
postmanToOpenApi(args.input, args.output, options) | ||
.then(_ => { | ||
console.log(`Done !`); | ||
const parser = yargs | ||
.scriptName('p2o') | ||
.command('$0 <collection> [options]', | ||
'Convert a Postman collection (json) to OpenAPI spec (yml)', | ||
(yargs) => { | ||
yargs | ||
.positional('collection', { | ||
demandOption: true, | ||
desc: 'Path to the Postman collection json file', | ||
type: 'string' | ||
}) | ||
.options('o', { | ||
alias: 'output', | ||
desc: 'Path to the file where OpenAPI should be saved. If not provided result will be printed in console', | ||
type: 'string' | ||
}) | ||
.options('p', { | ||
alias: 'params', | ||
desc: 'Path to the Options json file with config parameters', | ||
type: 'string' | ||
}) | ||
.epilog('for more information, find our docs at https://joolfe.github.io/postman-to-openapi/') | ||
}, | ||
async ({ collection, output, options }) => { | ||
console.log('Command run') | ||
const result = await postmanToOpenApi(collection, output, options) | ||
console.log(result) | ||
}) | ||
.middleware((argv) => { | ||
if (argv.params) { | ||
argv.options = JSON.parse(readFileSync(argv.params)) | ||
} | ||
}) | ||
.showHelpOnFail(false, 'Specify --help for available options') | ||
.fail(function (msg, err, yargs) { | ||
console.error('You broke it!') | ||
console.error(msg) | ||
process.exit(1) | ||
}) | ||
.catch(err => { | ||
console.log(err); | ||
}); | ||
.help('h') | ||
.alias('h', 'help') | ||
|
||
module.exports = parser | ||
|
||
// p2o ./test/resources/input/v2/SimplePost.json -p ./test/resources/options/info.json |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict' | ||
|
||
const { describe, it } = require('mocha') | ||
const parser = require('../bin/p2o') | ||
const { ok } = require('assert').strict | ||
|
||
describe('Cli specs', function () { | ||
it('help command should work', async function () { | ||
const output = await promiseParse('--help') | ||
ok(output.includes('Convert a Postman collection (json) to OpenAPI spec (yml)')) | ||
}) | ||
|
||
it('help command should work (alias)', async function () { | ||
const output = await promiseParse('-h') | ||
ok(output.includes('Convert a Postman collection (json) to OpenAPI spec (yml)')) | ||
}) | ||
}) | ||
|
||
function promiseParse (command) { | ||
return new Promise((resolve, reject) => { | ||
parser.parse(command, (err, argv, output) => { | ||
if (err) return reject(err) | ||
resolve(output) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"info": { | ||
"title": "Options title", | ||
"version": "6.0.7-beta", | ||
"description": "Description from options", | ||
"termsOfService": "http://tos.myweb.com" | ||
} | ||
} |