From ddb5316a6568b433dd3d713a8d3ddfad35bb11f4 Mon Sep 17 00:00:00 2001 From: James Taracevicz Date: Mon, 30 Aug 2021 19:32:12 -0700 Subject: [PATCH 1/5] feat(bin): added js that parses cli or stdin for file and cli for expression then runs the expression on the parsed object --- bin/index.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100755 bin/index.js diff --git a/bin/index.js b/bin/index.js new file mode 100755 index 00000000..5406fbfc --- /dev/null +++ b/bin/index.js @@ -0,0 +1,52 @@ +#!/usr/bin/env node + +const path = require('path'); +const fs = require('fs'); + +const jsonata = require('../src/jsonata'); + +/** + * cli usage: + * jsonata + * cat my-file.json | jsonata + * @returns {{expression: string, object: {}}} + */ +function parseArgs() { + const args = process.argv.slice(2); + + if (args.length === 2) { // file and expression given + const [jsonFile, expression] = args; + + let object; + try { + object = require(path.resolve(jsonFile)); + } catch (error) { + // eslint-disable-next-line no-console + console.error(`failed to load json file ${jsonFile}`, error); + process.exit(1); + } + + return { object, expression }; + } else if (args.length === 1) { // expression given, read file contents from stdin + const fileData = fs.readFileSync(0).toString(); + + let object; + try { + object = JSON.parse(fileData); + } catch (error) { + // eslint-disable-next-line no-console + console.error(`failed to load json string from stdin`, error); + process.exit(2); + } + + return { object, expression: args[0] }; + } else { + // eslint-disable-next-line no-console + console.error('invalid number of arguments', process.argv); + process.exit(3); + } +} + +const { expression, object } = parseArgs(); +// eslint-disable-next-line no-console +console.log(jsonata(expression).evaluate(object)); From a9f14065c63e48156ceea58143a6096208711b59 Mon Sep 17 00:00:00 2001 From: James Taracevicz Date: Mon, 30 Aug 2021 19:34:28 -0700 Subject: [PATCH 2/5] build: added `jsonata` cli command that runs bin/index.js --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index c72ab13f..2a8e05fa 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,9 @@ "type": "git", "url": "https://github.com/jsonata-js/jsonata.git" }, + "bin": { + "jsonata": "bin/" + }, "scripts": { "pretest": "npm run lint", "mocha": "node ./node_modules/istanbul/lib/cli.js cover --report cobertura --report html ./node_modules/mocha/bin/_mocha -- \"test/**/*.js\"", From d68a906d5542a75a916d3bd559409ed10a5bc4f3 Mon Sep 17 00:00:00 2001 From: James Taracevicz Date: Mon, 30 Aug 2021 19:53:01 -0700 Subject: [PATCH 3/5] build: fixed `jsonata` cli path (needs to point directly to the js file as this becomes what some sym linked files link to) --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2a8e05fa..5a39d7c1 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "url": "https://github.com/jsonata-js/jsonata.git" }, "bin": { - "jsonata": "bin/" + "jsonata": "bin/index.js" }, "scripts": { "pretest": "npm run lint", From e1927146a84733b6c7da587c4bad8b5cc0bf896e Mon Sep 17 00:00:00 2001 From: James Taracevicz Date: Mon, 30 Aug 2021 19:53:22 -0700 Subject: [PATCH 4/5] build: ignored bin/ folder for jsdoc build config --- jsdoc.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsdoc.json b/jsdoc.json index 748152de..9a2d220e 100644 --- a/jsdoc.json +++ b/jsdoc.json @@ -5,7 +5,7 @@ }, "source": { "includePattern": ".+\\.js(doc)?$", - "excludePattern": "(^|\\/|\\\\)_|node_modules|doc|coverage" + "excludePattern": "(^|\\/|\\\\)_|node_modules|doc|coverage|bin" }, "plugins": [], "templates": { From 19361507d57e9310d42b1e4203caae59dc972755 Mon Sep 17 00:00:00 2001 From: James Taracevicz Date: Mon, 30 Aug 2021 19:56:29 -0700 Subject: [PATCH 5/5] fix: stringify & format result from the eval on the expression so we can unpack inner objects --- bin/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/index.js b/bin/index.js index 5406fbfc..f62e9c02 100755 --- a/bin/index.js +++ b/bin/index.js @@ -48,5 +48,6 @@ function parseArgs() { } const { expression, object } = parseArgs(); +const result = jsonata(expression).evaluate(object); // eslint-disable-next-line no-console -console.log(jsonata(expression).evaluate(object)); +console.log(JSON.stringify(result, null, 2));