diff --git a/bin/cli.js b/bin/cli.js new file mode 100755 index 0000000..3d0dfd0 --- /dev/null +++ b/bin/cli.js @@ -0,0 +1,5 @@ +#!/usr/bin/env node + +const p2o = require('./p2o') + +p2o.parse(process.argv.slice(2)) diff --git a/bin/p2o.js b/bin/p2o.js index c870af9..4f2eaa8 100755 --- a/bin/p2o.js +++ b/bin/p2o.js @@ -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 [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 diff --git a/package-lock.json b/package-lock.json index 24eead3..b5fe678 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "postman-to-openapi", - "version": "1.6.0", + "version": "1.7.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 5170505..79b9acc 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/cli.spec.js b/test/cli.spec.js new file mode 100644 index 0000000..62a18be --- /dev/null +++ b/test/cli.spec.js @@ -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) + }) + }) +} diff --git a/test/resources/options/info.json b/test/resources/options/info.json new file mode 100644 index 0000000..da192bb --- /dev/null +++ b/test/resources/options/info.json @@ -0,0 +1,8 @@ +{ + "info": { + "title": "Options title", + "version": "6.0.7-beta", + "description": "Description from options", + "termsOfService": "http://tos.myweb.com" + } + } \ No newline at end of file