@dbml/cli 5.6.0-alpha.2 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +19 -0
- package/eslint.config.ts +6 -13
- package/jest.config.ts +14 -0
- package/lib/cli/config.js +29 -0
- package/lib/cli/connector.js +38 -0
- package/lib/cli/export.js +37 -0
- package/lib/cli/import.js +36 -0
- package/lib/cli/index.js +76 -0
- package/lib/cli/outputPlugins/outputConsolePlugin.js +12 -0
- package/lib/cli/outputPlugins/outputFilePlugin.js +28 -0
- package/lib/cli/utils.js +81 -0
- package/lib/cli/validatePlugins/validatePlugins.js +14 -0
- package/lib/errors/domainError.js +15 -0
- package/lib/errors/index.js +20 -0
- package/lib/errors/syntaxError.js +23 -0
- package/lib/helpers/logger.js +74 -0
- package/lib/index.js +20 -1303
- package/package.json +27 -10
- package/src/cli/import.js +1 -4
- package/src/cli/utils.js +1 -1
- package/src/errors/syntaxError.js +2 -2
- package/tsconfig.json +1 -2
- package/vite.config.ts +0 -50
package/.babelrc
ADDED
package/eslint.config.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { defineConfig } from 'eslint/config';
|
|
|
4
4
|
import stylistic from '@stylistic/eslint-plugin';
|
|
5
5
|
import tseslint from 'typescript-eslint';
|
|
6
6
|
import tsparser from '@typescript-eslint/parser';
|
|
7
|
+
import jest from 'eslint-plugin-jest';
|
|
7
8
|
|
|
8
9
|
export default defineConfig(
|
|
9
10
|
eslint.configs.recommended,
|
|
@@ -20,8 +21,9 @@ export default defineConfig(
|
|
|
20
21
|
'node_modules/*',
|
|
21
22
|
'lib/*',
|
|
22
23
|
'bin/*',
|
|
24
|
+
'jestHelpers.js',
|
|
23
25
|
'eslint.config.ts',
|
|
24
|
-
'
|
|
26
|
+
'jest.config.ts',
|
|
25
27
|
],
|
|
26
28
|
},
|
|
27
29
|
{
|
|
@@ -36,7 +38,6 @@ export default defineConfig(
|
|
|
36
38
|
sourceType: 'module',
|
|
37
39
|
ecmaVersion: 2018,
|
|
38
40
|
project: './tsconfig.json',
|
|
39
|
-
tsconfigRootDir: import.meta.dirname,
|
|
40
41
|
},
|
|
41
42
|
},
|
|
42
43
|
plugins: {
|
|
@@ -73,19 +74,11 @@ export default defineConfig(
|
|
|
73
74
|
},
|
|
74
75
|
},
|
|
75
76
|
{
|
|
76
|
-
files: ['**/*.test.js', '**/*.spec.js'
|
|
77
|
+
files: ['**/*.test.js', '**/*.spec.js'],
|
|
78
|
+
...jest.configs['flat/recommended'],
|
|
77
79
|
languageOptions: {
|
|
78
80
|
globals: {
|
|
79
|
-
|
|
80
|
-
describe: 'readonly',
|
|
81
|
-
it: 'readonly',
|
|
82
|
-
test: 'readonly',
|
|
83
|
-
expect: 'readonly',
|
|
84
|
-
beforeAll: 'readonly',
|
|
85
|
-
afterAll: 'readonly',
|
|
86
|
-
beforeEach: 'readonly',
|
|
87
|
-
afterEach: 'readonly',
|
|
88
|
-
vi: 'readonly',
|
|
81
|
+
...jest.environments.globals.globals,
|
|
89
82
|
},
|
|
90
83
|
},
|
|
91
84
|
rules: {
|
package/jest.config.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type Config } from 'jest';
|
|
2
|
+
|
|
3
|
+
const config: Config = {
|
|
4
|
+
testMatch: ["**/?(*.)+(spec|test).?([mc])[jt]s?(x)"],
|
|
5
|
+
preset: 'ts-jest',
|
|
6
|
+
transform: {
|
|
7
|
+
"^.+\\.js$": "babel-jest",
|
|
8
|
+
},
|
|
9
|
+
collectCoverage: true,
|
|
10
|
+
coverageReporters: ["json", "json-summary", "html", "text"],
|
|
11
|
+
coverageDirectory: "coverage",
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export default config;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _default = exports.default = {
|
|
8
|
+
mysql: {
|
|
9
|
+
name: 'MySQL'
|
|
10
|
+
},
|
|
11
|
+
mysqlLegacy: {
|
|
12
|
+
name: 'MySQL'
|
|
13
|
+
},
|
|
14
|
+
postgres: {
|
|
15
|
+
name: 'PostgreSQL'
|
|
16
|
+
},
|
|
17
|
+
postgresLegacy: {
|
|
18
|
+
name: 'PostgreSQL'
|
|
19
|
+
},
|
|
20
|
+
mssql: {
|
|
21
|
+
name: 'SQL Server'
|
|
22
|
+
},
|
|
23
|
+
oracle: {
|
|
24
|
+
name: 'Oracle'
|
|
25
|
+
},
|
|
26
|
+
snowflake: {
|
|
27
|
+
name: 'Snowflake'
|
|
28
|
+
}
|
|
29
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = connectionHandler;
|
|
7
|
+
var _core = require("@dbml/core");
|
|
8
|
+
var _connector = require("@dbml/connector");
|
|
9
|
+
var _figures = _interopRequireDefault(require("figures"));
|
|
10
|
+
var _chalk = _interopRequireDefault(require("chalk"));
|
|
11
|
+
var _path = _interopRequireDefault(require("path"));
|
|
12
|
+
var _utils = require("./utils");
|
|
13
|
+
var _outputConsolePlugin = _interopRequireDefault(require("./outputPlugins/outputConsolePlugin"));
|
|
14
|
+
var _outputFilePlugin = _interopRequireDefault(require("./outputPlugins/outputFilePlugin"));
|
|
15
|
+
var _logger = _interopRequireDefault(require("../helpers/logger"));
|
|
16
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
17
|
+
async function connectionHandler(program) {
|
|
18
|
+
try {
|
|
19
|
+
const {
|
|
20
|
+
connection,
|
|
21
|
+
databaseType
|
|
22
|
+
} = (0, _utils.getConnectionOpt)(program.args);
|
|
23
|
+
const opts = program.opts();
|
|
24
|
+
const schemaJson = await _connector.connector.fetchSchemaJson(connection, databaseType);
|
|
25
|
+
if (!opts.outFile && !opts.outDir) {
|
|
26
|
+
const res = _core.importer.generateDbml(schemaJson);
|
|
27
|
+
_outputConsolePlugin.default.write(res);
|
|
28
|
+
} else if (opts.outFile) {
|
|
29
|
+
const res = _core.importer.generateDbml(schemaJson);
|
|
30
|
+
new _outputFilePlugin.default((0, _utils.resolvePaths)(opts.outFile)).write(res);
|
|
31
|
+
|
|
32
|
+
// bearer:disable javascript_lang_logger
|
|
33
|
+
console.log(` ${_chalk.default.green(_figures.default.main.tick)} Generated DBML file from database's connection: ${_path.default.basename(opts.outFile)}`);
|
|
34
|
+
}
|
|
35
|
+
} catch (error) {
|
|
36
|
+
_logger.default.error(error);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = exportHandler;
|
|
7
|
+
var _figures = _interopRequireDefault(require("figures"));
|
|
8
|
+
var _chalk = _interopRequireDefault(require("chalk"));
|
|
9
|
+
var _path = _interopRequireDefault(require("path"));
|
|
10
|
+
var _core = require("@dbml/core");
|
|
11
|
+
var _utils = require("./utils");
|
|
12
|
+
var _validatePlugins = require("./validatePlugins/validatePlugins");
|
|
13
|
+
var _outputConsolePlugin = _interopRequireDefault(require("./outputPlugins/outputConsolePlugin"));
|
|
14
|
+
var _outputFilePlugin = _interopRequireDefault(require("./outputPlugins/outputFilePlugin"));
|
|
15
|
+
var _config = _interopRequireDefault(require("./config"));
|
|
16
|
+
var _logger = _interopRequireDefault(require("../helpers/logger"));
|
|
17
|
+
var _errors = require("../errors");
|
|
18
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
19
|
+
async function exportHandler(program) {
|
|
20
|
+
try {
|
|
21
|
+
const inputPaths = (0, _utils.resolvePaths)(program.args);
|
|
22
|
+
(0, _utils.validateInputFilePaths)(inputPaths, _validatePlugins.validateFilePlugin);
|
|
23
|
+
const opts = program.opts();
|
|
24
|
+
const format = (0, _utils.getFormatOpt)(opts);
|
|
25
|
+
if (!opts.outFile && !opts.outDir) {
|
|
26
|
+
(0, _utils.generate)(inputPaths, dbml => _core.exporter.export(dbml, format), _outputConsolePlugin.default);
|
|
27
|
+
} else if (opts.outFile) {
|
|
28
|
+
const header = ['-- SQL dump generated using DBML (dbml.dbdiagram.io)\n', `-- Database: ${_config.default[format].name}\n`, `-- Generated at: ${new Date().toISOString()}\n\n`].join('');
|
|
29
|
+
(0, _utils.generate)(inputPaths, dbml => _core.exporter.export(dbml, format), new _outputFilePlugin.default((0, _utils.resolvePaths)(opts.outFile), header));
|
|
30
|
+
console.log(` ${_chalk.default.green(_figures.default.main.tick)} Generated SQL dump file (${_config.default[format].name}): ${_path.default.basename(opts.outFile)}`);
|
|
31
|
+
}
|
|
32
|
+
} catch (error) {
|
|
33
|
+
_logger.default.error(`\n ${error.diags.map(diag => new _errors.SyntaxError(diag.filepath, diag)).map(({
|
|
34
|
+
message
|
|
35
|
+
}) => message).join('\n ')}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = importHandler;
|
|
7
|
+
var _core = require("@dbml/core");
|
|
8
|
+
var _figures = _interopRequireDefault(require("figures"));
|
|
9
|
+
var _chalk = _interopRequireDefault(require("chalk"));
|
|
10
|
+
var _path = _interopRequireDefault(require("path"));
|
|
11
|
+
var _config = _interopRequireDefault(require("./config"));
|
|
12
|
+
var _utils = require("./utils");
|
|
13
|
+
var _validatePlugins = require("./validatePlugins/validatePlugins");
|
|
14
|
+
var _outputConsolePlugin = _interopRequireDefault(require("./outputPlugins/outputConsolePlugin"));
|
|
15
|
+
var _outputFilePlugin = _interopRequireDefault(require("./outputPlugins/outputFilePlugin"));
|
|
16
|
+
var _logger = _interopRequireDefault(require("../helpers/logger"));
|
|
17
|
+
var _errors = require("../errors");
|
|
18
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
19
|
+
async function importHandler(program) {
|
|
20
|
+
try {
|
|
21
|
+
const inputPaths = (0, _utils.resolvePaths)(program.args);
|
|
22
|
+
(0, _utils.validateInputFilePaths)(inputPaths, _validatePlugins.validateFilePlugin);
|
|
23
|
+
const opts = program.opts();
|
|
24
|
+
const format = (0, _utils.getFormatOpt)(opts);
|
|
25
|
+
if (!opts.outFile && !opts.outDir) {
|
|
26
|
+
(0, _utils.generate)(inputPaths, sql => _core.importer.import(sql, format), _outputConsolePlugin.default);
|
|
27
|
+
} else if (opts.outFile) {
|
|
28
|
+
(0, _utils.generate)(inputPaths, sql => _core.importer.import(sql, format), new _outputFilePlugin.default((0, _utils.resolvePaths)(opts.outFile)));
|
|
29
|
+
console.log(` ${_chalk.default.green(_figures.default.main.tick)} Generated DBML file from SQL file (${_config.default[format].name}): ${_path.default.basename(opts.outFile)}`);
|
|
30
|
+
}
|
|
31
|
+
} catch (error) {
|
|
32
|
+
_logger.default.error(`\n ${error.diags.map(diag => new _errors.SyntaxError(diag.filepath, diag)).map(({
|
|
33
|
+
message
|
|
34
|
+
}) => message).join('\n ')}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
package/lib/cli/index.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.db2dbml = db2dbml;
|
|
7
|
+
exports.dbml2sql = dbml2sql;
|
|
8
|
+
exports.sql2dbml = sql2dbml;
|
|
9
|
+
var _commander = _interopRequireDefault(require("commander"));
|
|
10
|
+
var _import = _interopRequireDefault(require("./import"));
|
|
11
|
+
var _export = _interopRequireDefault(require("./export"));
|
|
12
|
+
var _connector = _interopRequireDefault(require("./connector"));
|
|
13
|
+
var _package = _interopRequireDefault(require("../../package.json"));
|
|
14
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
15
|
+
function showHelp(args) {
|
|
16
|
+
if (args.length < 3) _commander.default.help();
|
|
17
|
+
}
|
|
18
|
+
function dbml2sql(args) {
|
|
19
|
+
_commander.default.version(_package.default.version);
|
|
20
|
+
_commander.default.usage('[options] <files...>').option('--mysql').option('--postgres').option('--mssql').option('--oracle').option('-o, --out-file <pathspec>', 'compile all input files into a single files');
|
|
21
|
+
// .option('-d, --out-dir <pathspec>', 'compile an input directory of dbml files into an output directory');
|
|
22
|
+
|
|
23
|
+
showHelp(args);
|
|
24
|
+
_commander.default.parse(args);
|
|
25
|
+
(0, _export.default)(_commander.default);
|
|
26
|
+
}
|
|
27
|
+
function sql2dbml(args) {
|
|
28
|
+
_commander.default.version(_package.default.version);
|
|
29
|
+
_commander.default.usage('[options] <files...>').option('--mysql').option('--mysql-legacy').option('--postgres').option('--postgres-legacy').option('--mssql').option('--mssql-legacy').option('--snowflake').option('--oracle').option('-o, --out-file <pathspec>', 'compile all input files into a single files');
|
|
30
|
+
// .option('-d, --out-dir <pathspec>', 'compile an input directory of sql files into an output directory');
|
|
31
|
+
|
|
32
|
+
showHelp(args);
|
|
33
|
+
_commander.default.parse(args);
|
|
34
|
+
(0, _import.default)(_commander.default);
|
|
35
|
+
}
|
|
36
|
+
function db2dbml(args) {
|
|
37
|
+
_commander.default.version(_package.default.version);
|
|
38
|
+
const description = `Generate DBML directly from a database
|
|
39
|
+
<database-type> your database format (postgres, mysql, mssql, snowflake, bigquery)
|
|
40
|
+
<connection-string> your database connection string:
|
|
41
|
+
- postgres: 'postgresql://user:password@localhost:5432/dbname?schemas=schema1,schema2,schema3'
|
|
42
|
+
- mysql: 'mysql://user:password@localhost:3306/dbname'
|
|
43
|
+
- mssql: 'Server=localhost,1433;Database=master;User Id=sa;Password=your_password;Encrypt=true;TrustServerCertificate=true;Schemas=schema1,schema2,schema3;'
|
|
44
|
+
- oracle: 'username/password@[//]host[:port][/service_name]'
|
|
45
|
+
- snowflake:
|
|
46
|
+
- password-based authentication: 'SERVER=<account_identifier>.<region>;UID=<your_username>;PWD=<your_password>;DATABASE=<your_database>;WAREHOUSE=<your_warehouse>;ROLE=<your_role>;SCHEMAS=schema1,schema2,schema3;'
|
|
47
|
+
- key pair authentication: 'SERVER=<account_identifier>.<region>;UID=<your_username>;AUTHENTICATOR=SNOWFLAKE_JWT;PRIVATE_KEY_PATH=<path_to_your_private_key.p8>;PASSPHRASE=<your_private_key_passphrase>;DATABASE=<your_database>;WAREHOUSE=<your_warehouse>;ROLE=<your_role>;SCHEMAS=schema1,schema2,schema3;'
|
|
48
|
+
|
|
49
|
+
Note: If you did not use passphrase to encrypt your private key, you can leave the "PASSPHRASE" empty.
|
|
50
|
+
|
|
51
|
+
- bigquery: /path_to_json_credential.json
|
|
52
|
+
|
|
53
|
+
For BigQuery, the credential file supports flexible authentication:
|
|
54
|
+
|
|
55
|
+
1. Application Default Credentials (ADC):
|
|
56
|
+
- Empty file: {} - uses environment authentication
|
|
57
|
+
- Override specific fields: {"project_id": "my-project", "datasets": [...]}
|
|
58
|
+
|
|
59
|
+
For more information about ADC, see https://cloud.google.com/docs/authentication/application-default-credentials
|
|
60
|
+
|
|
61
|
+
2. Explicit Service Account (bypasses ADC):
|
|
62
|
+
{
|
|
63
|
+
"project_id": "your-project-id",
|
|
64
|
+
"client_email": "your-client-email",
|
|
65
|
+
"private_key": "your-private-key",
|
|
66
|
+
"datasets": ["dataset_1", "dataset_2", ...]
|
|
67
|
+
}
|
|
68
|
+
Note: Both client_email and private_key must be provided together.
|
|
69
|
+
|
|
70
|
+
If "datasets" is not specified or is empty, all accessible datasets will be fetched.
|
|
71
|
+
`;
|
|
72
|
+
_commander.default.usage('<database-type> <connection-string> [options]').description(description).option('-o, --out-file <pathspec>', 'compile all input files into a single files');
|
|
73
|
+
showHelp(args);
|
|
74
|
+
_commander.default.parse(args);
|
|
75
|
+
(0, _connector.default)(_commander.default);
|
|
76
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
class OutputConsolePlugin {
|
|
8
|
+
static write(content) {
|
|
9
|
+
console.log(content);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
var _default = exports.default = OutputConsolePlugin;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _fs = _interopRequireDefault(require("fs"));
|
|
8
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
+
class OutputFilePlugin {
|
|
10
|
+
constructor(filePath, header) {
|
|
11
|
+
this.filePath = filePath;
|
|
12
|
+
this.header = header;
|
|
13
|
+
this.isWrite = false;
|
|
14
|
+
}
|
|
15
|
+
start() {
|
|
16
|
+
_fs.default.writeFileSync(this.filePath, '');
|
|
17
|
+
this.stream = _fs.default.createWriteStream(this.filePath, {
|
|
18
|
+
flags: 'a'
|
|
19
|
+
});
|
|
20
|
+
if (this.header) this.stream.write(this.header);
|
|
21
|
+
this.isWrite = true;
|
|
22
|
+
}
|
|
23
|
+
write(content) {
|
|
24
|
+
if (!this.isWrite) this.start();
|
|
25
|
+
this.stream.write(content);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
var _default = exports.default = OutputFilePlugin;
|
package/lib/cli/utils.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.generate = generate;
|
|
7
|
+
exports.getConnectionOpt = getConnectionOpt;
|
|
8
|
+
exports.getFormatOpt = getFormatOpt;
|
|
9
|
+
exports.resolvePaths = resolvePaths;
|
|
10
|
+
exports.validateInputFilePaths = validateInputFilePaths;
|
|
11
|
+
var _path2 = _interopRequireDefault(require("path"));
|
|
12
|
+
var _fs = _interopRequireDefault(require("fs"));
|
|
13
|
+
var _core = require("@dbml/core");
|
|
14
|
+
var _lodash = require("lodash");
|
|
15
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
16
|
+
function resolvePaths(paths) {
|
|
17
|
+
if (!Array.isArray(paths)) {
|
|
18
|
+
return _path2.default.resolve(process.cwd(), paths);
|
|
19
|
+
}
|
|
20
|
+
return paths.map(_path => _path2.default.resolve(process.cwd(), _path));
|
|
21
|
+
}
|
|
22
|
+
function validateInputFilePaths(paths, validatePlugin) {
|
|
23
|
+
return paths.every(_path => validatePlugin(_path));
|
|
24
|
+
}
|
|
25
|
+
function getFormatOpt(opts) {
|
|
26
|
+
const formatOpts = Object.keys(opts).filter(opt => {
|
|
27
|
+
return ['postgres', 'mysql', 'mssql', 'postgresLegacy', 'mysqlLegacy', 'mssqlLegacy', 'oracle', 'snowflake'].includes(opt);
|
|
28
|
+
});
|
|
29
|
+
let format = 'postgres';
|
|
30
|
+
let cnt = 0;
|
|
31
|
+
formatOpts.forEach(opt => {
|
|
32
|
+
if (opts[opt]) {
|
|
33
|
+
cnt += 1;
|
|
34
|
+
if (cnt > 1) throw new Error('Too many format options');
|
|
35
|
+
format = opt;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return format;
|
|
39
|
+
}
|
|
40
|
+
function getConnectionOpt(args) {
|
|
41
|
+
const supportedDatabases = ['postgres', 'mysql', 'mssql', 'snowflake', 'bigquery', 'oracle'];
|
|
42
|
+
const defaultConnectionOpt = {
|
|
43
|
+
connection: args[0],
|
|
44
|
+
databaseType: 'unknown'
|
|
45
|
+
};
|
|
46
|
+
return (0, _lodash.reduce)(args, (connectionOpt, arg) => {
|
|
47
|
+
if (supportedDatabases.includes(arg)) connectionOpt.databaseType = arg;
|
|
48
|
+
// Check if the arg is a connection string using regex
|
|
49
|
+
const connectionStringRegex = /^.*[:;]/;
|
|
50
|
+
if (connectionStringRegex.test(arg)) {
|
|
51
|
+
// Example: jdbc:mysql://localhost:3306/mydatabase
|
|
52
|
+
// Example: odbc:Driver={SQL Server};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
|
|
53
|
+
connectionOpt.connection = arg;
|
|
54
|
+
}
|
|
55
|
+
const windowFilepathRegex = /^[a-zA-Z]:[\\/](?:[^<>:"/\\|?*\n\r]+[\\/])*[^<>:"/\\|?*\n\r]*$/;
|
|
56
|
+
const unixFilepathRegex = /^(\/|\.\/|~\/|\.\.\/)([^<>:"|?*\n\r]*\/?)*[^<>:"|?*\n\r]*$/;
|
|
57
|
+
if (windowFilepathRegex.test(arg) || unixFilepathRegex.test(arg)) {
|
|
58
|
+
connectionOpt.connection = arg;
|
|
59
|
+
}
|
|
60
|
+
return connectionOpt;
|
|
61
|
+
}, defaultConnectionOpt);
|
|
62
|
+
}
|
|
63
|
+
function generate(inputPaths, transform, outputPlugin) {
|
|
64
|
+
inputPaths.forEach(_path => {
|
|
65
|
+
const source = _fs.default.readFileSync(_path, 'utf-8');
|
|
66
|
+
try {
|
|
67
|
+
const content = transform(source);
|
|
68
|
+
outputPlugin.write(content);
|
|
69
|
+
} catch (e) {
|
|
70
|
+
if (e instanceof _core.CompilerError) {
|
|
71
|
+
throw e.map(diag => ({
|
|
72
|
+
...diag,
|
|
73
|
+
message: diag.message,
|
|
74
|
+
filepath: _path2.default.basename(_path),
|
|
75
|
+
stack: diag.stack
|
|
76
|
+
}));
|
|
77
|
+
}
|
|
78
|
+
throw e;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.validateFilePlugin = validateFilePlugin;
|
|
7
|
+
var _fs = _interopRequireDefault(require("fs"));
|
|
8
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
+
function validateFilePlugin(_path) {
|
|
10
|
+
const stat = _fs.default.statSync(_path);
|
|
11
|
+
if (stat.isDirectory(_path)) {
|
|
12
|
+
throw new Error('Expect input to be files');
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
class DomainError extends Error {
|
|
8
|
+
constructor(message, rootError = {}) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = this.constructor.name;
|
|
11
|
+
this.rootError = rootError;
|
|
12
|
+
Error.captureStackTrace(this, this.constructor);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
var _default = exports.default = DomainError;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "DomainError", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _domainError.default;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "SyntaxError", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _syntaxError.default;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
var _domainError = _interopRequireDefault(require("./domainError"));
|
|
19
|
+
var _syntaxError = _interopRequireDefault(require("./syntaxError"));
|
|
20
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _lodash = _interopRequireDefault(require("lodash"));
|
|
8
|
+
var _domainError = _interopRequireDefault(require("./domainError"));
|
|
9
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
|
+
class SyntaxError extends _domainError.default {
|
|
11
|
+
constructor(fileName, rootError = {}) {
|
|
12
|
+
let message = `You have a syntax error at "${fileName}"`;
|
|
13
|
+
if (rootError.location) {
|
|
14
|
+
message += ` line ${rootError.location.start.line} column ${rootError.location.start.column}`;
|
|
15
|
+
}
|
|
16
|
+
message += '.';
|
|
17
|
+
if (!_lodash.default.isEmpty(rootError)) {
|
|
18
|
+
message += ` ${rootError.message}`;
|
|
19
|
+
}
|
|
20
|
+
super(message, rootError);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
var _default = exports.default = SyntaxError;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _winston = require("winston");
|
|
8
|
+
var _chalk = _interopRequireDefault(require("chalk"));
|
|
9
|
+
var _path = _interopRequireDefault(require("path"));
|
|
10
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
|
+
const {
|
|
12
|
+
combine,
|
|
13
|
+
timestamp,
|
|
14
|
+
printf
|
|
15
|
+
} = _winston.format;
|
|
16
|
+
const consoleFormat = printf(info => {
|
|
17
|
+
const {
|
|
18
|
+
level,
|
|
19
|
+
message
|
|
20
|
+
} = info;
|
|
21
|
+
return ` ${_chalk.default.red(level.toUpperCase())}: ${message}\n
|
|
22
|
+
A complete log can be found in:
|
|
23
|
+
${_path.default.resolve(process.cwd(), 'dbml-error.log')}`;
|
|
24
|
+
});
|
|
25
|
+
const fileFormat = printf(info => {
|
|
26
|
+
const {
|
|
27
|
+
timestamp,
|
|
28
|
+
stack,
|
|
29
|
+
rootError
|
|
30
|
+
} = info;
|
|
31
|
+
let logContent = `${timestamp}\n${stack}\n`;
|
|
32
|
+
if (rootError) {
|
|
33
|
+
logContent += '\nROOT_ERROR:';
|
|
34
|
+
logContent += `\n${rootError.stack}`;
|
|
35
|
+
if (rootError.location) {
|
|
36
|
+
logContent += `\n${JSON.stringify(rootError.location)}`;
|
|
37
|
+
}
|
|
38
|
+
logContent += '\n';
|
|
39
|
+
}
|
|
40
|
+
return logContent;
|
|
41
|
+
});
|
|
42
|
+
const consoleLogger = (0, _winston.createLogger)({
|
|
43
|
+
format: combine(consoleFormat),
|
|
44
|
+
transports: [new _winston.transports.Console({
|
|
45
|
+
level: 'error'
|
|
46
|
+
})]
|
|
47
|
+
});
|
|
48
|
+
const fileLogger = (0, _winston.createLogger)({
|
|
49
|
+
format: combine(timestamp(), fileFormat),
|
|
50
|
+
transports: [new _winston.transports.File({
|
|
51
|
+
filename: 'dbml-error.log',
|
|
52
|
+
level: 'error'
|
|
53
|
+
})]
|
|
54
|
+
});
|
|
55
|
+
const logger = {
|
|
56
|
+
debug(msg) {
|
|
57
|
+
consoleLogger.debug(msg);
|
|
58
|
+
},
|
|
59
|
+
info(msg) {
|
|
60
|
+
consoleLogger.info(msg);
|
|
61
|
+
},
|
|
62
|
+
warn(msg) {
|
|
63
|
+
consoleLogger.warn(msg);
|
|
64
|
+
},
|
|
65
|
+
error(msg) {
|
|
66
|
+
consoleLogger.error(msg);
|
|
67
|
+
fileLogger.error(msg);
|
|
68
|
+
},
|
|
69
|
+
log(level, msg) {
|
|
70
|
+
const lvl = exports[level];
|
|
71
|
+
lvl(msg);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
var _default = exports.default = logger;
|