|
| 1 | +#!/usr/bin/env node |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const process = require('process'); |
| 5 | +const path = require('path'); |
| 6 | +const fs = require('fs'); |
| 7 | + |
| 8 | +const { readdir, stat } = fs.promises; |
| 9 | + |
| 10 | +const rec = async (branch, root) => { |
| 11 | + const node = {}; |
| 12 | + const localPath = path.resolve(root, ...branch); |
| 13 | + const els = await readdir(localPath); |
| 14 | + for (const el of els) { |
| 15 | + const isFile = (await stat(path.resolve(localPath, el))).isFile(); |
| 16 | + const fileExt = path.extname(el); |
| 17 | + const baseName = path.basename(el, fileExt); |
| 18 | + if (isFile) { |
| 19 | + if (['.yaml', '.json'].includes(fileExt)) { |
| 20 | + node[baseName] = {$ref: path.join(...branch, el)}; |
| 21 | + } |
| 22 | + } else { |
| 23 | + node[el] = await rec([...branch, el], root); |
| 24 | + } |
| 25 | + } |
| 26 | + return node; |
| 27 | +}; |
| 28 | + |
| 29 | +const main = async () => { |
| 30 | + const [, , root] = process.argv; |
| 31 | + if (root === undefined) { |
| 32 | + console.error('usage: ./index-unifieddb.js <path-to-unifieddb-arch-folder>'); |
| 33 | + return; |
| 34 | + } |
| 35 | + const rootPath = path.resolve('.', root); |
| 36 | + const tree = await rec([], rootPath); |
| 37 | + console.log(JSON.stringify(tree, null, 2)); |
| 38 | +}; |
| 39 | + |
| 40 | +main(); |
0 commit comments