Skip to content

Commit

Permalink
Add input from STDIN
Browse files Browse the repository at this point in the history
Add option for input encoding
  • Loading branch information
ddebin committed Mar 9, 2022
1 parent b0241b9 commit cb9033c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yaml-sort",
"version": "1.1.1",
"version": "1.2.0",
"description": "CLI tool to sort YAML files alphabetically",
"main": "yaml-sort.js",
"scripts": {
Expand Down
Binary file added test/test-utf16le.yml
Binary file not shown.
33 changes: 29 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@ const test = require('tape')
const spawn = require('tape-spawn')
const opts = { cwd: __dirname }

test('CLI w/o arg', (t) => {
const proc = spawn(t, '../yaml-sort.js', opts)
proc.exitCode(1)
proc.stderr.match(/Missing required argument: input/)
test('CLI help', (t) => {
const proc = spawn(t, '../yaml-sort.js -h', opts)
proc.exitCode(0)
proc.end()
})

test('CLI w/o arg (STDIN)', (t) => {
const proc = spawn(t, 'cat test.yml | ../yaml-sort.js', opts)
proc.exitCode(0)
proc.stdout.match('a: Lorem ipsum dolor sit amet, consectetur adipiscing elit...\n' +
'b:\n' +
' b: 35\n' +
' c:\n' +
' d: false\n' +
'\n')
proc.end()
})

Expand Down Expand Up @@ -82,6 +93,20 @@ test('CLI --check SUCCESS', (t) => {
proc.end()
})

test('CLI --encoding FAIL', (t) => {
const proc = spawn(t,
'../yaml-sort.js --input test-utf16le.yml --stdout', opts)
proc.exitCode(1)
proc.end()
})

test('CLI --encoding SUCCESS', (t) => {
const proc = spawn(t,
'../yaml-sort.js --input test-utf16le.yml --stdout --encoding utf16le', opts)
proc.exitCode(0)
proc.end()
})

test('CLI --lineWidth SUCCESS', (t) => {
const proc = spawn(t,
'../yaml-sort.js --input test.yml --output output.yml' +
Expand Down
18 changes: 14 additions & 4 deletions yaml-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ const argv = require('yargs')
'$0 --input config.yml --indent 4 --output sorted.yml',
'Indents with 4 spaces and outputs result to file sorted.yml'
)
.example(
'cat config.yml | $0',
'Sorts alphabetically from STDIN'
)
.option('input', {
alias: 'i',
describe: 'The YAML file(s) which needs to be sorted',
demandOption: 'Please provide an input file',
default: '-',
string: true,
array: true
})
Expand All @@ -46,13 +50,18 @@ const argv = require('yargs')
describe: 'Indentation width to use (in spaces)',
number: true
})
.option('encoding', {
alias: 'e',
default: 'utf8',
describe: 'Input encoding',
choices: ['ascii', 'utf8', 'utf16le']
})
.option('lineWidth', {
alias: 'w',
default: 80,
describe: 'Wrap line width',
number: true
})
.demandOption(['input'])
.help('h')
.alias('h', 'help')
.version()
Expand All @@ -66,7 +75,8 @@ let success = true

argv.input.forEach((file) => {
try {
const content = fs.readFileSync(file, 'utf8')
const stdin_input = file === '-'
const content = fs.readFileSync(stdin_input ? process.stdin.fd : file, argv.encoding)

const sorted = yaml.dump(yaml.load(content), {
sortKeys: true,
Expand All @@ -79,7 +89,7 @@ argv.input.forEach((file) => {
success = false
console.warn(`'${file}' is not sorted and/or formatted (indent, line width).`)
}
} else if (argv.stdout) {
} else if (argv.stdout || stdin_input) {
console.log(sorted)
} else {
fs.writeFile(
Expand Down

0 comments on commit cb9033c

Please sign in to comment.