Skip to content

Instantly share code, notes, and snippets.

@RodriguezLucha
Created February 14, 2020 18:57
Show Gist options
  • Save RodriguezLucha/1dda9637d32fc085533774cf63dd3c29 to your computer and use it in GitHub Desktop.
Save RodriguezLucha/1dda9637d32fc085533774cf63dd3c29 to your computer and use it in GitHub Desktop.
Sort Swagger YML
yaml = require("js-yaml");
fs = require("fs");
try {
var doc = yaml.safeLoad(fs.readFileSync("./input.yml", "utf8"));
let res = yaml.safeDump(doc, {
styles: {
"!!null": "canonical" // dump null as ~
},
lineWidth: 120,
sortKeys: function(a, b) {
const sortOrder = [
"swagger",
"info",
"host",
"basePath",
"schemes",
"paths",
"name",
"type",
"summary",
"description"
];
let index_a = sortOrder.indexOf(a);
let index_b = sortOrder.indexOf(b);
if (index_a > -1 && index_b > -1) {
return index_a > index_b ? 1 : index_a < index_b ? -1 : 0;
}
if (index_a != -1 && index_b == -1) {
return -1;
}
if (index_a == -1 && index_b != -1) {
return 1;
}
return a > b ? 1 : a < b ? -1 : 0;
}
});
console.log(res);
} catch (e) {
console.log(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment