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

fix: extra crlf in stdout, enable - as output for stdout #22

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ This tool is basically a tiny wrapper around [js-yaml](https://github.com/nodeca
Usage: yaml-sort [options]

Options:
-i, --input The YAML file(s) which needs to be sorted [array] [default: "-"]
-o, --output The YAML file to output sorted content to [string]
-i, --input The YAML file(s) which needs to be sorted [array] [default: STDIN]
-o, --output The YAML file to output sorted content to [string] [default: overwrite input file if specified or STDOUT]
-s, --stdout Output the proposed sort to STDOUT only [boolean]
-k, --check Check if the given file(s) is already sorted [boolean]
--indent, --id Indentation width to use (in spaces) [number] [default: 2]
--indent, --id Indentation width (in spaces) [number] [default: 2]
-e, --encoding Input encoding [choices: "ascii", "utf8", "utf16le"] [default: "utf8"]
-q, --quotingStyle Strings will be quoted using this quoting style [choices: "single", "double"] [default: "single"]
-w, --lineWidth Wrap line width (-1 for unlimited width) [number] [default: 80]
Expand Down
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": "2.0.0",
"version": "2.0.1",
"description": "Sort YAML files alphabetically",
"main": "yaml-sort.js",
"scripts": {
Expand Down
44 changes: 30 additions & 14 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ test('CLI w/o arg (STDIN)', (t) => {
'b:\n' +
' b: 35\n' +
' c:\n' +
' d: false\n' +
'\n')
' d: false\n')
proc.stderr.match('')
proc.end()
})
Expand All @@ -41,8 +40,7 @@ test('CLI w/ arg', (t) => {
'b:\n' +
' b: 35\n' +
' c:\n' +
' d: false\n' +
'\n')
' d: false\n')
proc.stderr.match('')
proc.end()
})
Expand All @@ -57,8 +55,7 @@ test('CLI quoting style single', (t) => {
' a: \'hello: "john"\'\n' +
' d: false\n' +
' e: \'"foo"\'\n' +
' f: \'\'\'foo\'\'\'\n' +
'\n')
' f: \'\'\'foo\'\'\'\n')
proc.stderr.match('')
proc.end()
})
Expand All @@ -73,8 +70,7 @@ test('CLI quoting style double', (t) => {
' a: "hello: \\"john\\""\n' +
' d: false\n' +
' e: "\\"foo\\""\n' +
' f: "\'foo\'"\n' +
'\n')
' f: "\'foo\'"\n')
proc.stderr.match('')
proc.end()
})
Expand All @@ -94,6 +90,17 @@ test('CLI --output', (t) => {
proc.end()
})

test('CLI --output -', (t) => {
const proc = spawn(t, '../yaml-sort.js --input test.yml --output -', 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')
proc.stderr.match('')
proc.end()
})
test('CLI --output (STDIN)', (t) => {
const proc = spawn(t,
'cat test.yml | ../yaml-sort.js --input - --output output.yml' +
Expand All @@ -109,15 +116,26 @@ test('CLI --output (STDIN)', (t) => {
proc.end()
})

test('CLI (STDIN) (STDOUT)', (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')
proc.stderr.match('')
proc.end()
})

test('CLI --indent', (t) => {
const proc = spawn(t, '../yaml-sort.js --input test.yml --stdout --indent 4', 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')
' d: false\n')
proc.stderr.match('')
proc.end()
})
Expand All @@ -131,8 +149,7 @@ test('CLI --lineWidth', (t) => {
'b:\n' +
' b: 35\n' +
' c:\n' +
' d: false\n' +
'\n')
' d: false\n')
proc.stderr.match('')
proc.end()
})
Expand All @@ -144,8 +161,7 @@ test('CLI --lineWidth unlimited', (t) => {
'b:\n' +
' b: 35\n' +
' c:\n' +
' d: false\n' +
'\n')
' d: false\n')
proc.stderr.match('')
proc.end()
})
Expand Down
15 changes: 11 additions & 4 deletions yaml-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ const argv = yargs
alias: 'i',
describe: 'The YAML file(s) which needs to be sorted',
default: '-',
defaultDescription: 'STDIN',
normalize: true,
string: true,
array: true
})
.option('output', {
alias: 'o',
describe: 'The YAML file to output sorted content to',
defaultDescription: 'overwrite input file if specified or STDOUT',
normalize: true,
string: true
})
.option('stdout', {
Expand All @@ -45,7 +49,7 @@ const argv = yargs
.option('indent', {
alias: 'id',
default: 2,
describe: 'Indentation width to use (in spaces)',
describe: 'Indentation width (in spaces)',
number: true
})
.option('encoding', {
Expand Down Expand Up @@ -83,6 +87,11 @@ argv.input.forEach((file) => {
process.exit(22)
}

const output =
argv.stdout || (argv.output === '.') || (isStdin && !argv.output)
? process.stdout.fd
: (argv.output ? argv.output : file)

const content = fs.readFileSync(isStdin ? process.stdin.fd : file, argv.encoding)

const sorted = yaml.dump(yaml.load(content), {
Expand All @@ -97,11 +106,9 @@ argv.input.forEach((file) => {
success = false
console.warn(`'${file}' is not sorted and/or formatted (indent, line width).`)
}
} else if (argv.stdout || (isStdin && !argv.output)) {
console.log(sorted)
} else {
fs.writeFile(
argv.output ? argv.output : file,
output,
sorted,
(error) => {
if (error) {
Expand Down