UNPKG

quick-erd

Version:

quick and easy text-based ERD + code generator for migration, query, typescript types and orm entity

83 lines (82 loc) 2.41 kB
#!/usr/bin/env node "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = require("fs"); const text_to_sqlite_proxy_1 = require("../db/text-to-sqlite-proxy"); const file_1 = require("../utils/file"); function main() { const command = getCommand(); let mode; let type; const arg = process.argv[2]; switch (arg) { case undefined: break; case '--factory': { mode = 'factory'; break; } case '--singleton': { mode = 'singleton'; break; } case '--commonjs': case '--cjs': { type = 'commonjs'; break; } case '--module': case '--esm': { type = 'module'; break; } default: { console.error('Error: unknown argument:', arg); process.exit(1); } } const packageFile = 'package.json'; if (!type && (0, fs_1.existsSync)(packageFile)) { type = (0, file_1.readPackageJSON)(packageFile).type; } (0, file_1.readErdFromStdin)(text => { const code = (0, text_to_sqlite_proxy_1.textToSqliteProxy)(text, { mode, type, command }); // eslint-disable-next-line no-console console.log(code); }); } function getCommand() { let script = getNpmScript(); if (script) { return `npm run ${script}`; } const args = process.argv.slice(2).join(' '); let command = `npx erd-to-proxy`; if (args) { command += ' ' + args; } const erd_file = (0, fs_1.existsSync)('docs/erd.txt') ? 'docs/erd.txt' : 'erd.txt'; const proxy_file = (0, fs_1.existsSync)('src') ? 'src/proxy.ts' : 'proxy.ts'; command += ` < ${erd_file} > ${proxy_file}`; return command; } function getNpmScript() { let file = 'package.json'; if (!(0, fs_1.existsSync)(file)) { return null; } let text = (0, fs_1.readFileSync)(file).toString(); let pkg = JSON.parse(text); let scripts = pkg.scripts || {}; let proxyKey = Object.entries(scripts).find(([key, value]) => { return value.includes('erd-to-proxy'); })?.[0]; if (!proxyKey) { return null; } let updateKey = Object.entries(scripts).find(([key, value]) => { return value.includes(proxyKey); })?.[0]; return updateKey || proxyKey; } main();