Skip to content

Instantly share code, notes, and snippets.

@outloudvi
Created September 8, 2020 10:27
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 outloudvi/befb8f3df3b97b49a17010b7c91892b6 to your computer and use it in GitHub Desktop.
Save outloudvi/befb8f3df3b97b49a17010b7c91892b6 to your computer and use it in GitHub Desktop.
// Run this in "vtbs/".
const fs = require('fs')
const FIX_FILENAME = false
const FIX_CONTENTFILENAME = true
async function main() {
const info = fs.readdirSync('.')
for (let filename of info) {
if (FIX_FILENAME) {
const username = filename.replace(/.json$/, '')
if (username.trim() !== username) {
console.log('Bad username for', username)
fs.renameSync(username + '.json', username.trim() + '.json')
filename = username.trim() + '.json'
}
}
if (FIX_CONTENTFILENAME) {
let chg = false
const fileContent = fs.readFileSync(filename, 'utf-8')
const fileObject = JSON.parse(fileContent)
if (typeof fileObject.group === 'string') {
if (fileObject.group.trim() !== fileObject.group) chg = true
fileObject.group = fileObject.group.trim()
}
if (fileObject.name) {
for (const [k, v] of Object.entries(fileObject.name)) {
if (typeof v === 'string') {
if (v !== v.trim()) chg = true
fileObject.name[k] = v.trim()
} else if (Array.isArray(v)) {
fileObject.name[k] = v.map((x) => {
if (x !== x.trim()) chg = true
return x.trim()
})
}
}
} else {
console.log(fileObject)
}
if (chg) {
fs.writeFileSync(
filename,
JSON.stringify(fileObject, null, 2) +
(fileContent[fileContent.length - 1] === '\n' ? '\n' : '')
)
}
}
}
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment