22
33'use strict'
44
5- const argv = require ( 'yargs' )
5+ const fs = require ( 'fs' )
6+ const yaml = require ( 'js-yaml' )
7+ const yargs = require ( 'yargs' )
8+
9+ const argv = yargs
610 . usage ( 'Usage: $0 [options]' )
7- . example (
8- '$0 --input config.yml' ,
9- 'Sorts alphabetically and overwrites the file config.yml'
10- )
11- . example (
12- '$0 --input config.yml --lineWidth 100 --stdout' ,
13- 'Sorts the file config.yml and output result to STDOUT wrapped to 100 columns'
14- )
15- . example (
16- '$0 --input config.yml --indent 4 --output sorted.yml' ,
17- 'Indents with 4 spaces and outputs result to file sorted.yml'
18- )
11+ . example ( [
12+ [ '$0 --input config.yml' ,
13+ 'Sorts alphabetically and overwrites the file config.yml' ] ,
14+ [ '$0 --input config.yml --lineWidth 100 --stdout' ,
15+ 'Sorts the file config.yml and output result to STDOUT wrapped to 100 columns' ] ,
16+ [ '$0 --input config.yml --indent 4 --output sorted.yml' ,
17+ 'Indents with 4 spaces and outputs result to file sorted.yml' ] ,
18+ [ 'cat config.yml | $0' ,
19+ 'Sorts alphabetically from STDIN' ]
20+ ] )
1921 . option ( 'input' , {
2022 alias : 'i' ,
2123 describe : 'The YAML file(s) which needs to be sorted' ,
22- demandOption : 'Please provide an input file ' ,
24+ default : '- ' ,
2325 string : true ,
2426 array : true
2527 } )
@@ -30,14 +32,14 @@ const argv = require('yargs')
3032 } )
3133 . option ( 'stdout' , {
3234 alias : 's' ,
33- default : false ,
3435 describe : 'Output the proposed sort to STDOUT only' ,
36+ conflicts : 'output' ,
3537 boolean : true
3638 } )
3739 . option ( 'check' , {
3840 alias : 'k' ,
39- default : false ,
4041 describe : 'Check if the given file(s) is already sorted' ,
42+ conflicts : [ 'output' , 'stdout' ] ,
4143 boolean : true
4244 } )
4345 . option ( 'indent' , {
@@ -46,27 +48,36 @@ const argv = require('yargs')
4648 describe : 'Indentation width to use (in spaces)' ,
4749 number : true
4850 } )
51+ . option ( 'encoding' , {
52+ alias : 'e' ,
53+ default : 'utf8' ,
54+ describe : 'Input encoding' ,
55+ choices : [ 'ascii' , 'utf8' , 'utf16le' ]
56+ } )
4957 . option ( 'lineWidth' , {
5058 alias : 'w' ,
5159 default : 80 ,
5260 describe : 'Wrap line width' ,
5361 number : true
5462 } )
55- . demandOption ( [ 'input' ] )
5663 . help ( 'h' )
5764 . alias ( 'h' , 'help' )
5865 . version ( )
5966 . wrap ( null )
6067 . argv
6168
62- const yaml = require ( 'js-yaml' )
63- const fs = require ( 'fs' )
64-
6569let success = true
6670
6771argv . input . forEach ( ( file ) => {
6872 try {
69- const content = fs . readFileSync ( file , 'utf8' )
73+ const isStdin = file === '-'
74+
75+ if ( isStdin && process . stdin . isTTY ) {
76+ yargs . showHelp ( )
77+ process . exit ( 22 )
78+ }
79+
80+ const content = fs . readFileSync ( isStdin ? process . stdin . fd : file , argv . encoding )
7081
7182 const sorted = yaml . dump ( yaml . load ( content ) , {
7283 sortKeys : true ,
@@ -79,15 +90,16 @@ argv.input.forEach((file) => {
7990 success = false
8091 console . warn ( `'${ file } ' is not sorted and/or formatted (indent, line width).` )
8192 }
82- } else if ( argv . stdout ) {
93+ } else if ( argv . stdout || ( isStdin && ! argv . output ) ) {
8394 console . log ( sorted )
8495 } else {
8596 fs . writeFile (
8697 argv . output ? argv . output : file ,
8798 sorted ,
8899 ( error ) => {
89100 if ( error ) {
90- return console . error ( error )
101+ success = false
102+ console . error ( error )
91103 }
92104 } )
93105 }
0 commit comments