Skip to content
This repository has been archived by the owner on Dec 27, 2024. It is now read-only.

Commit

Permalink
feat(wip): first version of cli
Browse files Browse the repository at this point in the history
  • Loading branch information
joolfe committed Dec 22, 2020
1 parent 4090ea1 commit a259287
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 39 deletions.
5 changes: 5 additions & 0 deletions bin/cli.js
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))
89 changes: 53 additions & 36 deletions bin/p2o.js
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "postman-to-openapi",
"version": "1.6.1",
"version": "1.7.0",
"description": "Convert postman collection to OpenAPI spec",
"main": "lib/index.js",
"bin": {
"p2o": "./bin/p2o.js"
"p2o": "./bin/cli.js"
},
"scripts": {
"lint": "standard **/*.js",
Expand Down
26 changes: 26 additions & 0 deletions test/cli.spec.js
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)
})
})
}
8 changes: 8 additions & 0 deletions test/resources/options/info.json
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"
}
}

0 comments on commit a259287

Please sign in to comment.