[no publish] adding db structure interpreter

This commit is contained in:
Timo Hocker 2020-02-20 11:09:01 +01:00
parent 2b7d6cf307
commit 8d665a3576
3 changed files with 136 additions and 0 deletions

View File

@ -27,5 +27,8 @@
"dependencies": { "dependencies": {
"fs-extra": "^8.1.0", "fs-extra": "^8.1.0",
"yargs": "^15.1.0" "yargs": "^15.1.0"
},
"engines": {
"node": ">=10.0.0"
} }
} }

42
snippets/db/dot_parser.js Normal file
View File

@ -0,0 +1,42 @@
'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 };

91
snippets/db/index.js Normal file
View File

@ -0,0 +1,91 @@
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of snippeteer which is released under BSD-3-Clause.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, January 2020
*/
/* eslint-disable no-sync */
/* eslint-disable no-console */
'use strict';
const fs = require ('fs-extra');
const path = require ('path');
/**
* copies the full template to a new folder named after arg[0]
*
* @param {string} folder folder to run in
* @param {Array} args function arguments
*/
function run (folder, args) {
const snip_folder_path = [ folder ];
if (args.length > 0)
snip_folder_path.push (args[0]);
const snip_folder = path.join (...snip_folder_path);
const template = path.join (__dirname, 'template');
if (!fs.existsSync (snip_folder))
fs.mkdir (snip_folder);
for (const f of fs.readdirSync (template)) {
fs.copy (
path.join (template, f),
path.join (snip_folder, f),
{
recursive: true,
filter: (src, dest) => !fs.existsSync (dest)
}
);
}
}
/**
* checks if the arguments meet the requirements
*
* @param {string} folder folder to run in
* @param {Array} args function arguments
* @returns {boolean} true if arguments match requirements
*/
function assert (folder, args) {
const tests = [
{
f: () => (args.length < 2),
reason: 'too many arguments'
},
{
f: () => (args.length === 0 || typeof args[0] === 'string'),
reason: 'name is not a string'
},
{
f: () => (args.length === 0 || (/^[a-z]+$/iu).test (args[0])),
reason: 'name can only contain [a-z]'
},
{
f: () => (typeof folder === 'string'),
reason: 'cwd is not a folder (internal error)'
},
{
f: () => (fs.existsSync (folder)),
reason: 'cwd does not exist (internal error)'
},
{
f: () => (args.length === 1 || fs.readdirSync (folder).length === 0),
reason: 'folder is not empty'
},
{
f: () => (args.length === 0
|| !fs.existsSync (path.join (folder, args[0]))),
reason: 'folder already exists'
}
];
for (const test of tests) {
if (!test.f ()) {
console.log (test.reason);
return false;
}
}
return true;
}
module.exports = { run, assert };