Skip to content
Open
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ Options:
-q, --quotingStyle Strings will be quoted using this quoting style [choices: "single", "double"] [default: "single"]
-f, --forceQuotes Force quotes for all scalar values [boolean] [default: false]
-w, --lineWidth Wrap line width (-1 for unlimited width) [number] [default: 80]
-p, --prioritize Comma seperated list of keys to prioritize [string]
-h, --help Show help [boolean]
--version Show version number [boolean]

Examples:
yaml-sort --input config.yml Sorts alphabetically and overwrites the file config.yml
yaml-sort --input config.yml --lineWidth 100 --stdout Sorts the file config.yml and output result to STDOUT wrapped to 100 columns
yaml-sort --input config.yml --indent 4 --output sorted.yml Indents with 4 spaces and outputs result to file sorted.yml
yaml-sort --input config.yml --forceQuotes --quotingStyle double Forces double quotes for all scalar values
yaml-sort --input config.yml --prioritize name Sorts alphabetically, keeps "name" key at the top
cat config.yml | yaml-sort Sorts alphabetically from STDIN
```
5 changes: 5 additions & 0 deletions test/test-order.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
b:
c: 'ccc'
d: 'ddd'
e: 'eee'
a: 'Lorem ipsum...'
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,27 @@ test('CLI multiple YAML documents --check SUCCESS', (t) => {
proc.stderr.match('')
proc.end()
})

test('CLI w order (full)', (t) => {
const proc = spawn(t, 'cat test-order.yml | ../yaml-sort.js --prioritize e,d,c', opts)
proc.exitCode(0)
proc.stdout.match('a: Lorem ipsum...\n' +
'b:\n' +
' e: eee\n' +
' d: ddd\n' +
' c: ccc\n')
proc.stderr.match('')
proc.end()
})

test('CLI w order (partial)', (t) => {
const proc = spawn(t, 'cat test-order.yml | ../yaml-sort.js --prioritize f,e,b', opts)
proc.exitCode(0)
proc.stdout.match('b:\n' +
' e: eee\n' +
' c: ccc\n' +
' d: ddd\n' +
'a: Lorem ipsum...\n')
proc.stderr.match('')
proc.end()
})
16 changes: 15 additions & 1 deletion yaml-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const argv = yargs
'Sorts the file config.yml and output result to STDOUT wrapped to 100 columns'],
['$0 --input config.yml --indent 4 --output sorted.yml',
'Indents with 4 spaces and outputs result to file sorted.yml'],
['$0 --input config.yml --prioritize name', 'Sorts alphabetically, keeps "name" key at the top'],
['cat config.yml | $0',
'Sorts alphabetically from STDIN']
])
Expand Down Expand Up @@ -75,6 +76,11 @@ const argv = yargs
describe: 'Wrap line width (-1 for unlimited width)',
number: true
})
.option('prioritize', {
alias: 'p',
describe: 'Comma seperated list of keys to prioritize',
string: true
})
.help('h')
.alias('h', 'help')
.version()
Expand All @@ -101,8 +107,16 @@ argv.input.forEach((file) => {

const documents = yaml.loadAll(content)

const prioritize = argv.prioritize ? argv.prioritize.split(',').reverse() : []

const sortedDocuments = documents.map(doc => yaml.dump(doc, {
sortKeys: true,
sortKeys: function (a, b) {
const ia = prioritize.indexOf(a)
const ib = prioritize.indexOf(b)
return ia !== -1 || ib !== -1
? (ib < ia ? -1 : ib > ia)
: (a < b ? -1 : a > b)
},
indent: argv.indent,
lineWidth: argv.lineWidth,
quotingType: argv.quotingStyle === 'double' ? '"' : "'",
Expand Down