diff --git a/README.md b/README.md index 972e233..94136b3 100644 --- a/README.md +++ b/README.md @@ -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] diff --git a/test/test.js b/test/test.js index 8ec92c3..ceeacde 100755 --- a/test/test.js +++ b/test/test.js @@ -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() }) @@ -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() }) @@ -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() }) @@ -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() }) @@ -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' + @@ -109,6 +116,18 @@ 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) @@ -116,8 +135,7 @@ test('CLI --indent', (t) => { 'b:\n' + ' b: 35\n' + ' c:\n' + - ' d: false\n' + - '\n') + ' d: false\n') proc.stderr.match('') proc.end() }) @@ -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() }) @@ -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() }) diff --git a/yaml-sort.js b/yaml-sort.js index 43d8bf7..3de8941 100755 --- a/yaml-sort.js +++ b/yaml-sort.js @@ -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', { @@ -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', { @@ -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), { @@ -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) {