diff --git a/README.md b/README.md index e612599..960c31c 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ 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] @@ -35,6 +36,6 @@ 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 ``` \ No newline at end of file diff --git a/test/test-order.yml b/test/test-order.yml new file mode 100644 index 0000000..092096e --- /dev/null +++ b/test/test-order.yml @@ -0,0 +1,5 @@ +b: + c: 'ccc' + d: 'ddd' + e: 'eee' +a: 'Lorem ipsum...' \ No newline at end of file diff --git a/test/test.js b/test/test.js index 8417d6f..680b6f1 100755 --- a/test/test.js +++ b/test/test.js @@ -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() +}) diff --git a/yaml-sort.js b/yaml-sort.js index e87f3d0..a164f19 100755 --- a/yaml-sort.js +++ b/yaml-sort.js @@ -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'] ]) @@ -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() @@ -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' ? '"' : "'",