-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathutils.js
More file actions
83 lines (70 loc) · 2.43 KB
/
utils.js
File metadata and controls
83 lines (70 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import path from 'path';
import fs from 'fs';
import { CompilerError } from '@dbml/core';
import { reduce } from 'lodash';
function resolvePaths (paths) {
if (!Array.isArray(paths)) {
return path.resolve(process.cwd(), paths);
}
return paths.map((_path) => path.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', 'sqlite'].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 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.readFileSync(_path, 'utf-8');
try {
const content = transform(source);
outputPlugin.write(content);
} catch (e) {
if (e instanceof CompilerError) throw e.map((diag) => ({ ...diag, message: diag.message, filepath: path.basename(_path), stack: diag.stack }));
throw e;
}
});
}
export {
resolvePaths,
validateInputFilePaths,
getFormatOpt,
getConnectionOpt,
generate,
};