snippeteer/snippets/db/dot_parser.js

43 lines
949 B
JavaScript

'use strict';
const fs = require ('fs-extra');
/**
* @param str
*/
function get_table_info (str) {
const lines = str.split (/\n/ug);
lines.splice (0, 2);
lines.splice (lines.length - 2, 2);
const name_line = lines.splice (0);
const { name } = (/<b>(?<name>\S)<\/b>/u).exec (name_line).groups;
const columns = [];
return { name, columns };
}
/**
* get all tables from a structure file
*
* @param {string} file path to the structure file
* @returns {Array<object>} tables
*/
async function get_tables (file) {
const lines = (await fs.readFile (file, 'utf-8')).split (/\n/gu);
const curr = [];
const tables = [];
for (const l of lines) {
if (curr.length > 0 || (/\S+ \[label=</u).test (l))
curr.push (l);
if ((/>\]/u).test (l)) {
tables.push (curr.join ('\n'));
curr.splice (0, curr.length);
}
}
return tables.map ((val) => get_table_info (val));
}
module.exports = { get_tables };