Skip to content

Instantly share code, notes, and snippets.

@damieng
Created November 9, 2021 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damieng/ff80160ab301c64262e471ae15f494f2 to your computer and use it in GitHub Desktop.
Save damieng/ff80160ab301c64262e471ae15f494f2 to your computer and use it in GitHub Desktop.
Fix my yaml files that had multiline "message:" key at the end using quotes not | and indents like it should
const Glob = require('glob').Glob
const process = require('process')
const fs = require('fs')
const path = process.argv[2]
Glob(path, {}, (err, files) => {
let count = 0;
for (let file of files) {
const lines = fs.readFileSync(file, 'utf-8').trim().split('\n')
let i = 0;
while (i < lines.length && !lines[i].startsWith("message: ")) i++;
if (i < lines.length && lines[i + 1] != '') {
let preambleIdx = i + 1;
let message = []
let firstLine = lines[i].substr(9).trim()
if (firstLine.startsWith('"')) firstLine = firstLine.substr(1)
message.push(' ' + firstLine)
lines[i++] = "message: |";
while (i < lines.length) {
message.push(' ' + lines[i++].trim())
}
let lastLine = message.pop().trim();
if (lastLine.endsWith('"')) lastLine = lastLine.substr(0, lastLine.length - 1)
message.push(' ' + lastLine)
const final = lines.slice(0, preambleIdx).concat(message)
fs.writeFileSync(file, final.join('\n'), 'utf-8')
count++;
}
}
console.log(`${count} files fixed`);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment