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

Commit

Permalink
feat: cli added to library
Browse files Browse the repository at this point in the history
  • Loading branch information
joolfe committed Apr 29, 2021
1 parent 1c440da commit 5df5d9b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
17 changes: 9 additions & 8 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ const { version } = require('../package.json')
const postmanToOpenApi = require('../lib')
const { promises: { readFile } } = require('fs')

// TODO
// - handle error in a homogeneous way, put messages more clear or where is happening the error maybe?
// - check the input field or should be test inside library?
// - Errors should be more descriptive i think, for exmaple i dont know where the collection fail or the output fail?
// - check the coverage

const additionalHelp = `
Example calls:
Expand All @@ -32,11 +26,10 @@ program
.option('-o, --options <options>', 'Relative path to json file that contain the optional parameters for the transformation.')
.action(async (collection, { file, options }, command) => {
try {
const parsedOptions = options ? JSON.parse(await readFile(options)) : {}
const parsedOptions = await parseOptions(options)
const result = await postmanToOpenApi(collection, file, parsedOptions)
console.info(result)
} catch (err) {
// TODO normalize errors here
throw new Error(err)
}
})
Expand All @@ -47,3 +40,11 @@ program
process.exitCode = 1
console.error(err.message)
})

async function parseOptions (optionsPath) {
try {
return optionsPath ? JSON.parse(await readFile(optionsPath)) : {}
} catch (err) {
throw new Error(`invalid "options" parameter -> ${err.message}`)
}
}
18 changes: 9 additions & 9 deletions package-lock.json

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

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postman-to-openapi",
"version": "1.7.3",
"version": "1.8.0",
"description": "Convert postman collection to OpenAPI spec",
"main": "lib/index.js",
"bin": {
Expand Down Expand Up @@ -41,7 +41,7 @@
"@commitlint/cli": "^12.1.1",
"@commitlint/config-conventional": "^12.1.1",
"conventional-changelog-cli": "^2.1.1",
"eslint": "^7.24.0",
"eslint": "^7.25.0",
"eslint-config-standard": "^16.0.2",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
Expand All @@ -60,7 +60,8 @@
"all": true,
"include": [
"lib/**/*.js",
"test/**/*.js"
"test/**/*.js",
"bin/**/*.js"
],
"exclude": [],
"reporter": [
Expand Down
6 changes: 3 additions & 3 deletions test/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,18 @@ describe('Cli specs', function () {
equal(stdout, EXPECTED_INFO_OPTS)
})

it('should print an error when options file doesn\'t exist', async function () {
it('should print an error when "options" file doesn\'t exist', async function () {
await rejects(execa('node', [cliPath, COLLECTION_SIMPLE, '-o', './no_exist/options.json']), {
name: 'Error',
stderr: "Error: ENOENT: no such file or directory, open './no_exist/options.json'",
stderr: "Error: invalid \"options\" parameter -> ENOENT: no such file or directory, open './no_exist/options.json'",
exitCode: 1
})
})

it('should print an error when options file is not a valid json', async function () {
await rejects(execa('node', [cliPath, COLLECTION_SIMPLE, '-o', INVALID_OPTIONS_INFO]), {
name: 'Error',
stderr: 'SyntaxError: Unexpected token i in JSON at position 6',
stderr: 'Error: invalid "options" parameter -> Unexpected token i in JSON at position 6',
exitCode: 1
})
})
Expand Down

0 comments on commit 5df5d9b

Please sign in to comment.