Skip to content

Commit

Permalink
Add --sandbox option
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Sep 19, 2019
1 parent 721bbbe commit dc57ef3
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 50 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## next (September 19, 2019)

- Added `--sandbox` option to open data and query in sandbox (opens in a browser a web interface with injected data and query saved in a temporary file)

## 1.1.1 (August 21, 2019)

- Fixed missed files in package
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Options:
-o, --output <filename> Output file (outputs to stdout if not set)
-p, --pretty [indent] Pretty print with optionally specified indentation (4 spaces by default)
-q, --query <query> Jora query
-s, --sandbox Output data and query in sandbox
-v, --version Output version
```
Expand Down
19 changes: 18 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const fs = require('fs');
const path = require('path');
const cli = require('clap');
const tempfile = require('tempfile');
const open = require('open');
const createSandboxFileContent = require('jora-sandbox');
const jora = require('jora/dist/jora');
const colorize = require('./utils/colorize');

Expand All @@ -17,6 +20,7 @@ function processOptions(options, args) {
const query = options.query || args[0];
const pretty = options.pretty || false;
const color = options.color && colorize.supported;
const sandbox = options.sandbox || false;
let inputFile = options.input;
let outputFile = options.output;

Expand All @@ -38,6 +42,7 @@ function processOptions(options, args) {
query,
pretty,
color,
sandbox,
inputFile,
outputFile
};
Expand Down Expand Up @@ -82,13 +87,24 @@ function serializeResult(data, options) {
}

function processStream(options) {
const query = prepareQuery(options);
const inputStream = options.inputFile === '<stdin>'
? process.stdin
: fs.createReadStream(options.inputFile);

readFromStream(inputStream, function(source) {
const data = prepareData(source);

if (options.sandbox) {
const filepath = tempfile('.html');
fs.writeFileSync(filepath, createSandboxFileContent(
{ name: options.inputFile, data },
options.query
));
open(filepath);
return;
}

const query = prepareQuery(options);
const result = performQuery(query, data, undefined);
const serializedResult = serializeResult(convertUndefinedToNull(result), options);

Expand All @@ -109,6 +125,7 @@ const command = cli.create('jora', '[query]')
value === undefined ? 4 : Number(value) || false
, false)
.option('--no-color', 'Suppress color output')
.option('-s, --sandbox', 'Output data and query in sandbox')
.action(function(args) {
const options = processOptions(this.values, args);

Expand Down
125 changes: 78 additions & 47 deletions package-lock.json

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

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@
"chalk": "^2.4.2",
"clap": "^1.0.9",
"jora": "1.0.0-alpha.10",
"supports-color": "^7.0.0"
"jora-sandbox": "^1.0.1",
"open": "^6.4.0",
"supports-color": "^7.0.0",
"tempfile": "^3.0.0"
},
"devDependencies": {
"coveralls": "^3.0.4",
"eslint": "^6.0.1",
"eslint": "^6.4.0",
"mocha": "^5.2.0",
"nyc": "^14.1.1"
},
Expand Down

0 comments on commit dc57ef3

Please sign in to comment.