Skip to content

Commit 5d81b40

Browse files
committed
fix: extra crlf in stdout, enable - as output for stdout
1 parent 0291d45 commit 5d81b40

File tree

4 files changed

+45
-22
lines changed

4 files changed

+45
-22
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ This tool is basically a tiny wrapper around [js-yaml](https://github.com/nodeca
1919
Usage: yaml-sort [options]
2020
2121
Options:
22-
-i, --input The YAML file(s) which needs to be sorted [array] [default: "-"]
23-
-o, --output The YAML file to output sorted content to [string]
22+
-i, --input The YAML file(s) which needs to be sorted [array] [default: STDIN]
23+
-o, --output The YAML file to output sorted content to [string] [default: overwrite input file if specified or STDOUT]
2424
-s, --stdout Output the proposed sort to STDOUT only [boolean]
2525
-k, --check Check if the given file(s) is already sorted [boolean]
26-
--indent, --id Indentation width to use (in spaces) [number] [default: 2]
26+
--indent, --id Indentation width (in spaces) [number] [default: 2]
2727
-e, --encoding Input encoding [choices: "ascii", "utf8", "utf16le"] [default: "utf8"]
2828
-q, --quotingStyle Strings will be quoted using this quoting style [choices: "single", "double"] [default: "single"]
2929
-w, --lineWidth Wrap line width (-1 for unlimited width) [number] [default: 80]

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "yaml-sort",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Sort YAML files alphabetically",
55
"main": "yaml-sort.js",
66
"scripts": {

test/test.js

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ test('CLI w/o arg (STDIN)', (t) => {
2828
'b:\n' +
2929
' b: 35\n' +
3030
' c:\n' +
31-
' d: false\n' +
32-
'\n')
31+
' d: false\n')
3332
proc.stderr.match('')
3433
proc.end()
3534
})
@@ -41,8 +40,7 @@ test('CLI w/ arg', (t) => {
4140
'b:\n' +
4241
' b: 35\n' +
4342
' c:\n' +
44-
' d: false\n' +
45-
'\n')
43+
' d: false\n')
4644
proc.stderr.match('')
4745
proc.end()
4846
})
@@ -57,8 +55,7 @@ test('CLI quoting style single', (t) => {
5755
' a: \'hello: "john"\'\n' +
5856
' d: false\n' +
5957
' e: \'"foo"\'\n' +
60-
' f: \'\'\'foo\'\'\'\n' +
61-
'\n')
58+
' f: \'\'\'foo\'\'\'\n')
6259
proc.stderr.match('')
6360
proc.end()
6461
})
@@ -73,8 +70,7 @@ test('CLI quoting style double', (t) => {
7370
' a: "hello: \\"john\\""\n' +
7471
' d: false\n' +
7572
' e: "\\"foo\\""\n' +
76-
' f: "\'foo\'"\n' +
77-
'\n')
73+
' f: "\'foo\'"\n')
7874
proc.stderr.match('')
7975
proc.end()
8076
})
@@ -94,6 +90,17 @@ test('CLI --output', (t) => {
9490
proc.end()
9591
})
9692

93+
test('CLI --output -', (t) => {
94+
const proc = spawn(t, '../yaml-sort.js --input test.yml --output -', opts)
95+
proc.exitCode(0)
96+
proc.stdout.match('a: Lorem ipsum dolor sit amet, consectetur adipiscing elit...\n' +
97+
'b:\n' +
98+
' b: 35\n' +
99+
' c:\n' +
100+
' d: false\n')
101+
proc.stderr.match('')
102+
proc.end()
103+
})
97104
test('CLI --output (STDIN)', (t) => {
98105
const proc = spawn(t,
99106
'cat test.yml | ../yaml-sort.js --input - --output output.yml' +
@@ -109,15 +116,26 @@ test('CLI --output (STDIN)', (t) => {
109116
proc.end()
110117
})
111118

119+
test('CLI (STDIN) (STDOUT)', (t) => {
120+
const proc = spawn(t, 'cat test.yml | ../yaml-sort.js', opts)
121+
proc.exitCode(0)
122+
proc.stdout.match('a: Lorem ipsum dolor sit amet, consectetur adipiscing elit...\n' +
123+
'b:\n' +
124+
' b: 35\n' +
125+
' c:\n' +
126+
' d: false\n')
127+
proc.stderr.match('')
128+
proc.end()
129+
})
130+
112131
test('CLI --indent', (t) => {
113132
const proc = spawn(t, '../yaml-sort.js --input test.yml --stdout --indent 4', opts)
114133
proc.exitCode(0)
115134
proc.stdout.match('a: Lorem ipsum dolor sit amet, consectetur adipiscing elit...\n' +
116135
'b:\n' +
117136
' b: 35\n' +
118137
' c:\n' +
119-
' d: false\n' +
120-
'\n')
138+
' d: false\n')
121139
proc.stderr.match('')
122140
proc.end()
123141
})
@@ -131,8 +149,7 @@ test('CLI --lineWidth', (t) => {
131149
'b:\n' +
132150
' b: 35\n' +
133151
' c:\n' +
134-
' d: false\n' +
135-
'\n')
152+
' d: false\n')
136153
proc.stderr.match('')
137154
proc.end()
138155
})
@@ -144,8 +161,7 @@ test('CLI --lineWidth unlimited', (t) => {
144161
'b:\n' +
145162
' b: 35\n' +
146163
' c:\n' +
147-
' d: false\n' +
148-
'\n')
164+
' d: false\n')
149165
proc.stderr.match('')
150166
proc.end()
151167
})

yaml-sort.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ const argv = yargs
2222
alias: 'i',
2323
describe: 'The YAML file(s) which needs to be sorted',
2424
default: '-',
25+
defaultDescription: 'STDIN',
26+
normalize: true,
2527
string: true,
2628
array: true
2729
})
2830
.option('output', {
2931
alias: 'o',
3032
describe: 'The YAML file to output sorted content to',
33+
defaultDescription: 'overwrite input file if specified or STDOUT',
34+
normalize: true,
3135
string: true
3236
})
3337
.option('stdout', {
@@ -45,7 +49,7 @@ const argv = yargs
4549
.option('indent', {
4650
alias: 'id',
4751
default: 2,
48-
describe: 'Indentation width to use (in spaces)',
52+
describe: 'Indentation width (in spaces)',
4953
number: true
5054
})
5155
.option('encoding', {
@@ -83,6 +87,11 @@ argv.input.forEach((file) => {
8387
process.exit(22)
8488
}
8589

90+
const output =
91+
argv.stdout || (argv.output === '.') || (isStdin && !argv.output)
92+
? process.stdout.fd
93+
: (argv.output ? argv.output : file)
94+
8695
const content = fs.readFileSync(isStdin ? process.stdin.fd : file, argv.encoding)
8796

8897
const sorted = yaml.dump(yaml.load(content), {
@@ -97,11 +106,9 @@ argv.input.forEach((file) => {
97106
success = false
98107
console.warn(`'${file}' is not sorted and/or formatted (indent, line width).`)
99108
}
100-
} else if (argv.stdout || (isStdin && !argv.output)) {
101-
console.log(sorted)
102109
} else {
103110
fs.writeFile(
104-
argv.output ? argv.output : file,
111+
output,
105112
sorted,
106113
(error) => {
107114
if (error) {

0 commit comments

Comments
 (0)