Created
March 27, 2021 05:56
-
-
Save crutchcorn/a7d7d9d5d9690b0497ce86280516aaa1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path'); | |
const fs = require('fs'); | |
const xml2js = require('xml2js'); | |
async function walk(dir) { | |
const finalObj = {}; | |
for await (const d of await fs.promises.opendir(dir)) { | |
const entry = path.join(dir, d.name); | |
if (d.isDirectory()) { | |
finalObj[d.name] = await walk(entry); | |
continue; | |
} | |
const contents = await fs.promises.readFile(entry); | |
finalObj[d.name] = contents; | |
continue; | |
} | |
return finalObj; | |
} | |
async function main() { | |
const fileContentObj = await walk(path.resolve('./Layouts')); | |
const parser = new xml2js.Parser(/* options */); | |
const fileParsedArr = await Promise.all(Object.entries(fileContentObj).map(async ([fileName, fileContents]) => { | |
const parsedObj = await parser.parseStringPromise(fileContents); | |
const keys = ['name', 'type', 'default', 'description']; | |
// Array | |
const entryArr = parsedObj['layout']['entry']; | |
let header = `|${keys.map(key => key.toUpperCase()).join('|')}|`; | |
let seperator = `|${keys.map(_ => '---').join('|')}|`; | |
const body = entryArr.map(entry => { | |
const entryArr = keys.map(key => { | |
const [val] = entry[key] || ['']; | |
return val; | |
}) | |
return `|${entryArr.join('|')}|`; | |
}).join('\n'); | |
return [fileName, `${header}\n${seperator}\n${body}`]; | |
})) | |
await Promise.all(fileParsedArr.map(async ([fileName, fileContents]) => { | |
await fs.promises.writeFile(path.resolve(`./output/${fileName}.md`), fileContents); | |
})) | |
} | |
main().catch(console.error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
At some point, I had to debug JSON and had to write this JSON:
"((?!name|type|default|size|entry|description).*)": \[