Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: sort keys on output #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion bin/generate-schema
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ var content = ''
var mode = 'json'
var quiet = false
var file = null
var sortKeys = false

// Setup CLI
cli.version(pkg.version)
cli.usage('[options ...] [file]')

cli.option('-k, --keys', 'Sort object keys of the output', function () {
sortKeys = true
})

cli.option('-q, --quiet', 'Skip help message in program output', function () {
quiet = true
})
Expand Down Expand Up @@ -102,6 +107,18 @@ function callback (data) {

var stream = process.stdin

var sortObjectKeys = (_key, value) => {
if (typeof value === 'object' && !Array.isArray(value)) {
return Object.keys(value)
.sort()
.reduce((sorted, k) => {
sorted[k] = value[k];
return sorted;
}, {});
}
return value;
}

if (file) {
stream = fs.createReadStream(file)
stream.on('close', function () {
Expand All @@ -123,6 +140,7 @@ if (!file) {
})

stream.on('end', function () {
console.log(JSON.stringify(GenerateSchema[mode](evaluate(content)), null, 2))
var output = GenerateSchema[mode](evaluate(content))
console.log(JSON.stringify(output, sortKeys ? sortObjectKeys : null, 2))
})
}