diff --git a/index-unifieddb.js b/index-unifieddb.js new file mode 100755 index 0000000000..cdc1404d62 --- /dev/null +++ b/index-unifieddb.js @@ -0,0 +1,40 @@ +#!/usr/bin/env node +'use strict'; + +const process = require('process'); +const path = require('path'); +const fs = require('fs'); + +const { readdir, stat } = fs.promises; + +const rec = async (branch, root) => { + const node = {}; + const localPath = path.resolve(root, ...branch); + const els = await readdir(localPath); + for (const el of els) { + const isFile = (await stat(path.resolve(localPath, el))).isFile(); + const fileExt = path.extname(el); + const baseName = path.basename(el, fileExt); + if (isFile) { + if (['.yaml', '.json'].includes(fileExt)) { + node[baseName] = {$ref: path.join(...branch, el)}; + } + } else { + node[el] = await rec([...branch, el], root); + } + } + return node; +}; + +const main = async () => { + const [, , root] = process.argv; + if (root === undefined) { + console.error('usage: ./index-unifieddb.js '); + return; + } + const rootPath = path.resolve('.', root); + const tree = await rec([], rootPath); + console.log(JSON.stringify(tree, null, 2)); +}; + +main();