Skip to content

Commit

Permalink
Merge cfbfd8e into 0e6996d
Browse files Browse the repository at this point in the history
  • Loading branch information
exdis committed Jul 25, 2019
2 parents 0e6996d + cfbfd8e commit 7b9883f
Show file tree
Hide file tree
Showing 10 changed files with 488 additions and 29 deletions.
Empty file modified bin/jora
100644 → 100755
Empty file.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs');
const path = require('path');
const cli = require('clap');
const jora = require('jora/dist/jora');
const colorize = require('./utils/colorize');

function readFromStream(stream, processBuffer) {
const buffer = [];
Expand All @@ -15,6 +16,7 @@ function readFromStream(stream, processBuffer) {
function processOptions(options, args) {
const query = options.query || args[0];
const pretty = options.pretty || false;
const color = options.color;
let inputFile = options.input;
let outputFile = options.output;

Expand All @@ -35,6 +37,7 @@ function processOptions(options, args) {
return {
query,
pretty,
color,
inputFile,
outputFile
};
Expand Down Expand Up @@ -88,7 +91,8 @@ function processStream(options) {
if (options.outputFile) {
fs.writeFileSync(options.outputFile, serializedResult, 'utf-8');
} else {
console.log(serializedResult);
const result = options.color ? colorize(serializedResult) : serializedResult;
console.log(result);
}
});
}
Expand All @@ -101,6 +105,7 @@ var command = cli.create('jora', '[query]')
.option('-p, --pretty [indent]', 'Pretty print with optionally specified indentation (4 spaces by default)', value =>
value === undefined ? 4 : Number(value) || false
, false)
.option('--no-color', 'Suppress color output')
.action(function(args) {
var options = processOptions(this.values, args);

Expand Down
83 changes: 61 additions & 22 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"coveralls": "nyc report --reporter=text-lcov | coveralls"
},
"dependencies": {
"ansi-regex": "^4.1.0",
"chalk": "^2.4.2",
"clap": "^1.0.9",
"jora": "1.0.0-alpha.10"
},
Expand Down
15 changes: 15 additions & 0 deletions test/fixture.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"string": "st\"ri\u1234ng",
"number": 1234,
"emptyArray": [],
"emptyObject": {},
"null": null,
"false": false,
"true": true,
"complexJSON": {
"f\"oo\u01234"
:-0.1234, "bar": 0e-3,
"ba/": -0.1234e12,
"quux": -1.123
}
}
23 changes: 23 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const {
LEFT_BRACE,
RIGHT_BRACE,
LEFT_BRACKET,
RIGHT_BRACKET,
STRING,
NUMBER,
NULL,
FALSE,
TRUE
} = require('../utils/constants').TOKENS;
const { TOKEN_COLORS } = require('../utils/constants');
const ansiRegex = require('ansi-regex');

module.exports = {
STRING: TOKEN_COLORS[STRING](' ').match(ansiRegex()),
NUMBER: TOKEN_COLORS[NUMBER](' ').match(ansiRegex()),
EMPTY_ARRAY: [...TOKEN_COLORS[LEFT_BRACKET](' ').match(ansiRegex()), ...TOKEN_COLORS[RIGHT_BRACKET](' ').match(ansiRegex())],
EMPTY_OBJECT: [...TOKEN_COLORS[LEFT_BRACE](' ').match(ansiRegex()), ...TOKEN_COLORS[RIGHT_BRACE](' ').match(ansiRegex())],
NULL: TOKEN_COLORS[NULL](' ').match(ansiRegex()),
FALSE: TOKEN_COLORS[FALSE](' ').match(ansiRegex()),
TRUE: TOKEN_COLORS[TRUE](' ').match(ansiRegex())
};
72 changes: 66 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,31 @@ const child = require('child_process');
const cmd = 'node';
const pkgJson = path.join(__dirname, '../package.json');
const pkgJsonData = require(pkgJson);
const fs = require('fs');
const colorize = require('../utils/colorize');
const fixture = require('../test/fixture.json');
const ansiRegex = require('ansi-regex');
const {
STRING,
NUMBER,
EMPTY_ARRAY,
EMPTY_OBJECT,
NULL,
FALSE,
TRUE
} = require('./helpers');

function match(rx) {
return actual => rx.test(actual);
}

function matchANSI(fixture) {
return data => assert.deepEqual(data.match(ansiRegex()), fixture);
}

function run() {
var args = [path.join(__dirname, '../bin/jora')].concat(Array.prototype.slice.call(arguments));
var proc = child.spawn(cmd, args, { stdio: 'pipe' });
var proc = child.spawn(cmd, args, { stdio: 'pipe', env: { FORCE_COLOR: true } });
var error = '';
var wrapper = new Promise(function(resolve, reject) {
proc.once('exit', code =>
Expand Down Expand Up @@ -63,7 +80,7 @@ it('should output help with `-h` or `--help`', () =>
);

it('should output data itself when no query', () =>
run()
run('--no-color')
.input('42')
.output('42')
);
Expand All @@ -74,25 +91,25 @@ it('should output version', () =>
);

it('should read content from stdin if no file specified', () =>
run('version')
run('version', '--no-color')
.input(JSON.stringify(pkgJsonData))
.output(JSON.stringify(pkgJsonData.version))
);

it('should read from file', () =>
run('-i', pkgJson, '-q', 'version')
run('-i', pkgJson, '-q', 'version', '--no-color')
.output(JSON.stringify(pkgJsonData.version))
);

describe('pretty print', function() {
it('indentation should be 4 spaces by default', () =>
run('dependencies.keys()', '-p')
run('dependencies.keys()', '-p', '--no-color')
.input(JSON.stringify(pkgJsonData))
.output(JSON.stringify(Object.keys(pkgJsonData.dependencies), null, 4))
);

it('indentation should be as specified', () =>
run('dependencies.keys()', '-p', '3')
run('dependencies.keys()', '-p', '3', '--no-color')
.input(JSON.stringify(pkgJsonData))
.output(JSON.stringify(Object.keys(pkgJsonData.dependencies), null, 3))
);
Expand Down Expand Up @@ -120,3 +137,46 @@ describe('errors', function() {
)
);
});

describe('colorizer', function() {
it('Should colorize string', () =>
run('string')
.input(JSON.stringify(fixture))
.output(matchANSI(STRING))
);
it('Should colorize number', () =>
run('number')
.input(JSON.stringify(fixture))
.output(matchANSI(NUMBER))
);
it('Should colorize empty array', () =>
run('emptyArray')
.input(JSON.stringify(fixture))
.output(matchANSI(EMPTY_ARRAY))
);
it('Should colorize empty object', () =>
run('emptyArray')
.input(JSON.stringify(fixture))
.output(matchANSI(EMPTY_OBJECT))
);
it('Should colorize empty object', () =>
run('null')
.input(JSON.stringify(fixture))
.output(matchANSI(NULL))
);
it('Should colorize empty object', () =>
run('false')
.input(JSON.stringify(fixture))
.output(matchANSI(FALSE))
);
it('Should colorize empty object', () =>
run('true')
.input(JSON.stringify(fixture))
.output(matchANSI(TRUE))
);
it('Should colorize raw complex JSON', () =>
colorize(
fs.readFileSync(path.resolve(__dirname, './fixture.json')).toString()
).match(ansiRegex())
);
});
Loading

0 comments on commit 7b9883f

Please sign in to comment.