UNPKG

@dbml/cli

Version:

See our website [@dbml/cli](https://dbml.dbdiagram.io/cli/) for more information

79 lines (78 loc) 2.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generate = generate; exports.getConnectionOpt = getConnectionOpt; exports.getFormatOpt = getFormatOpt; exports.resolvePaths = resolvePaths; exports.validateInputFilePaths = validateInputFilePaths; var _path2 = _interopRequireDefault(require("path")); var _fs = _interopRequireDefault(require("fs")); var _core = require("@dbml/core"); var _lodash = require("lodash"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function resolvePaths(paths) { if (!Array.isArray(paths)) { return _path2.default.resolve(process.cwd(), paths); } return paths.map(_path => _path2.default.resolve(process.cwd(), _path)); } function validateInputFilePaths(paths, validatePlugin) { return paths.every(_path => validatePlugin(_path)); } function getFormatOpt(opts) { const formatOpts = Object.keys(opts).filter(opt => { return ['postgres', 'mysql', 'mssql', 'postgresLegacy', 'mysqlLegacy', 'mssqlLegacy', 'oracle', 'snowflake'].includes(opt); }); let format = 'postgres'; let cnt = 0; formatOpts.forEach(opt => { if (opts[opt]) { cnt += 1; if (cnt > 1) throw new Error('Too many format options'); format = opt; } }); return format; } function getConnectionOpt(args) { const supportedDatabases = ['postgres', 'mysql', 'mssql', 'snowflake', 'bigquery']; const defaultConnectionOpt = { connection: args[0], databaseType: 'unknown' }; return (0, _lodash.reduce)(args, (connectionOpt, arg) => { if (supportedDatabases.includes(arg)) connectionOpt.databaseType = arg; // Check if the arg is a connection string using regex const connectionStringRegex = /^.*[:;]/; if (connectionStringRegex.test(arg)) { // Example: jdbc:mysql://localhost:3306/mydatabase // Example: odbc:Driver={SQL Server};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; connectionOpt.connection = arg; } const windowFilepathRegex = /^[a-zA-Z]:[\\/](?:[^<>:"/\\|?*\n\r]+[\\/])*[^<>:"/\\|?*\n\r]*$/; const unixFilepathRegex = /^(\/|\.\/|~\/|\.\.\/)([^<>:"|?*\n\r]*\/?)*[^<>:"|?*\n\r]*$/; if (windowFilepathRegex.test(arg) || unixFilepathRegex.test(arg)) { connectionOpt.connection = arg; } return connectionOpt; }, defaultConnectionOpt); } function generate(inputPaths, transform, outputPlugin) { inputPaths.forEach(_path => { const source = _fs.default.readFileSync(_path, 'utf-8'); try { const content = transform(source); outputPlugin.write(content); } catch (e) { if (e instanceof _core.CompilerError) throw e.map(diag => ({ ...diag, message: diag.message, filepath: _path2.default.basename(_path), stack: diag.stack })); throw e; } }); }