@@ -15,6 +15,7 @@ const argv = yargs
1515 'Sorts the file config.yml and output result to STDOUT wrapped to 100 columns' ] ,
1616 [ '$0 --input config.yml --indent 4 --output sorted.yml' ,
1717 'Indents with 4 spaces and outputs result to file sorted.yml' ] ,
18+ [ '$0 --input config.yml --prioritize name' , 'Sorts alphabetically, keeps "name" key at the top' ] ,
1819 [ 'cat config.yml | $0' ,
1920 'Sorts alphabetically from STDIN' ]
2021 ] )
@@ -75,6 +76,11 @@ const argv = yargs
7576 describe : 'Wrap line width (-1 for unlimited width)' ,
7677 number : true
7778 } )
79+ . option ( 'prioritize' , {
80+ alias : 'p' ,
81+ describe : 'Comma seperated list of keys to prioritize' ,
82+ string : true
83+ } )
7884 . help ( 'h' )
7985 . alias ( 'h' , 'help' )
8086 . version ( )
@@ -101,8 +107,16 @@ argv.input.forEach((file) => {
101107
102108 const documents = yaml . loadAll ( content )
103109
110+ const prioritize = argv . prioritize ? argv . prioritize . split ( ',' ) . reverse ( ) : [ ]
111+
104112 const sortedDocuments = documents . map ( doc => yaml . dump ( doc , {
105- sortKeys : true ,
113+ sortKeys : function ( a , b ) {
114+ const ia = prioritize . indexOf ( a )
115+ const ib = prioritize . indexOf ( b )
116+ return ia !== - 1 || ib !== - 1
117+ ? ( ib < ia ? - 1 : ib > ia )
118+ : ( a < b ? - 1 : a > b )
119+ } ,
106120 indent : argv . indent ,
107121 lineWidth : argv . lineWidth ,
108122 quotingType : argv . quotingStyle === 'double' ? '"' : "'" ,
0 commit comments