refactor: common to ts

This commit is contained in:
Fabio Di Stasio 2022-05-10 12:57:25 +02:00
parent d1bfa282c3
commit cc5910b88f
27 changed files with 151 additions and 69 deletions

View File

@ -144,6 +144,7 @@
"@babel/preset-typescript": "~7.16.7", "@babel/preset-typescript": "~7.16.7",
"@playwright/test": "~1.21.1", "@playwright/test": "~1.21.1",
"@types/better-sqlite3": "~7.5.0", "@types/better-sqlite3": "~7.5.0",
"@types/leaflet": "~1.7.9",
"@types/node": "~17.0.23", "@types/node": "~17.0.23",
"@types/pg": "~8.6.5", "@types/pg": "~8.6.5",
"@typescript-eslint/eslint-plugin": "~5.18.0", "@typescript-eslint/eslint-plugin": "~5.18.0",

View File

@ -1,4 +1,6 @@
module.exports = { import { Customizations } from '../interfaces/customizations';
export const defaults: Customizations = {
// Defaults // Defaults
defaultPort: null, defaultPort: null,
defaultUser: null, defaultUser: null,
@ -68,24 +70,24 @@ module.exports = {
viewUpdateOption: false, viewUpdateOption: false,
procedureDeterministic: false, procedureDeterministic: false,
procedureDataAccess: false, procedureDataAccess: false,
procedureSql: false, procedureSql: null,
procedureContext: false, procedureContext: false,
procedureLanguage: false, procedureLanguage: false,
functionDeterministic: false, functionDeterministic: false,
functionDataAccess: false, functionDataAccess: false,
functionSql: false, functionSql: null,
functionContext: false, functionContext: false,
functionLanguage: false, functionLanguage: false,
triggerSql: false, triggerSql: null,
triggerStatementInCreation: false, triggerStatementInCreation: false,
triggerMultipleEvents: false, triggerMultipleEvents: false,
triggerTableInName: false, triggerTableInName: false,
triggerUpdateColumns: false, triggerUpdateColumns: false,
triggerOnlyRename: false, triggerOnlyRename: false,
triggerEnableDisable: false, triggerEnableDisable: false,
triggerFunctionSql: false, triggerFunctionSql: null,
triggerFunctionlanguages: false, triggerFunctionlanguages: null,
parametersLength: false, parametersLength: false,
languages: false, languages: null,
readOnlyMode: false readOnlyMode: false
}; };

View File

@ -1,6 +0,0 @@
module.exports = {
maria: require('./mysql'),
mysql: require('./mysql'),
pg: require('./postgresql'),
sqlite: require('./sqlite')
};

View File

@ -0,0 +1,10 @@
import * as mysql from 'common/customizations/mysql';
import * as postgresql from 'common/customizations/postgresql';
import * as sqlite from 'common/customizations/sqlite';
export default {
maria: mysql.customizations,
mysql: mysql.customizations,
pg: postgresql.customizations,
sqlite: sqlite.customizations
};

View File

@ -1,6 +1,7 @@
const defaults = require('./defaults'); import { Customizations } from '../interfaces/customizations';
import { defaults } from './defaults';
module.exports = { export const customizations: Customizations = {
...defaults, ...defaults,
// Defaults // Defaults
defaultPort: 3306, defaultPort: 3306,

View File

@ -1,6 +1,7 @@
const defaults = require('./defaults'); import { Customizations } from '../interfaces/customizations';
import { defaults } from './defaults';
module.exports = { export const customizations: Customizations = {
...defaults, ...defaults,
// Defaults // Defaults
defaultPort: 5432, defaultPort: 5432,

View File

@ -1,4 +1,8 @@
module.exports = { import { Customizations } from '../interfaces/customizations';
import { defaults } from './defaults';
export const customizations: Customizations = {
...defaults,
// Core // Core
fileConnection: true, fileConnection: true,
// Structure // Structure

View File

@ -1,4 +1,4 @@
module.exports = [ export default [
{ {
group: 'integer', group: 'integer',
types: [ types: [

View File

@ -1,4 +1,4 @@
module.exports = [ export default [
{ {
group: 'integer', group: 'integer',
types: [ types: [

View File

@ -1,4 +1,4 @@
module.exports = [ export default [
{ {
group: 'integer', group: 'integer',
types: [ types: [

View File

@ -1,4 +1,4 @@
module.exports = [ export default [
'PRIMARY', 'PRIMARY',
'INDEX', 'INDEX',
'UNIQUE', 'UNIQUE',

View File

@ -1,4 +1,4 @@
module.exports = [ export default [
'PRIMARY', 'PRIMARY',
'INDEX', 'INDEX',
'UNIQUE' 'UNIQUE'

View File

@ -1,4 +1,4 @@
module.exports = [ export default [
'PRIMARY', 'PRIMARY',
'INDEX', 'INDEX',
'UNIQUE' 'UNIQUE'

View File

@ -0,0 +1,91 @@
export interface Customizations {
// Defaults
defaultPort?: number;
defaultUser?: string;
defaultDatabase?: string;
// Core
database?: boolean;
collations?: boolean;
engines?: boolean;
connectionSchema?: boolean;
sslConnection?: boolean;
sshConnection?: boolean;
fileConnection?: boolean;
cancelQueries?: boolean;
// Tools
processesList?: boolean;
usersManagement?: boolean;
variables?: boolean;
// Structure
schemas?: boolean;
tables?: boolean;
views?: boolean;
triggers?: boolean;
triggerFunctions?: boolean;
routines?: boolean;
functions?: boolean;
schedulers?: boolean;
// Settings
elementsWrapper: string;
stringsWrapper: string;
tableAdd?: boolean;
viewAdd?: boolean;
triggerAdd?: boolean;
triggerFunctionAdd?: boolean;
routineAdd?: boolean;
functionAdd?: boolean;
schedulerAdd?: boolean;
databaseEdit?: boolean;
schemaEdit?: boolean;
schemaDrop?: boolean;
schemaExport?: boolean;
exportByChunks?: boolean;
schemaImport?: boolean;
tableSettings?: boolean;
tableOptions?: boolean;
tableArray?: boolean;
tableRealCount?: boolean;
viewSettings?: boolean;
triggerSettings?: boolean;
triggerFunctionSettings?: boolean;
routineSettings?: boolean;
functionSettings?: boolean;
schedulerSettings?: boolean;
indexes?: boolean;
foreigns?: boolean;
sortableFields?: boolean;
unsigned?: boolean;
nullable?: boolean;
nullablePrimary?: boolean;
zerofill?: boolean;
autoIncrement?: boolean;
comment?: boolean;
collation?: boolean;
definer?: boolean;
onUpdate?: boolean;
viewAlgorithm?: boolean;
viewSqlSecurity?: boolean;
viewUpdateOption?: boolean;
procedureDeterministic?: boolean;
procedureDataAccess?: boolean;
procedureSql?: string;
procedureContext?: boolean;
procedureLanguage?: boolean;
functionDeterministic?: boolean;
functionDataAccess?: boolean;
functionSql?: string;
functionContext?: boolean;
functionLanguage?: boolean;
triggerSql?: string;
triggerStatementInCreation?: boolean;
triggerMultipleEvents?: boolean;
triggerTableInName?: boolean;
triggerUpdateColumns?: boolean;
triggerOnlyRename?: boolean;
triggerEnableDisable?: boolean;
triggerFunctionSql?: string;
triggerFunctionlanguages?: string[];
parametersLength?: boolean;
languages?: string[];
readOnlyMode?: boolean;
}

View File

@ -1,7 +1,6 @@
'use strict'; export function bufferToBase64 (buf: Buffer) {
export function bufferToBase64 (buf) {
const binstr = Array.prototype.map.call(buf, ch => { const binstr = Array.prototype.map.call(buf, ch => {
return String.fromCharCode(ch); return String.fromCharCode(ch);
}).join(''); }).join('');
return btoa(binstr); return Buffer.from(binstr, 'base64');
} }

View File

@ -1,5 +1,4 @@
'use strict'; export function formatBytes (bytes: number, decimals = 2) {
export function formatBytes (bytes, decimals = 2) {
if (bytes === 0) return '0 Bytes'; if (bytes === 0) return '0 Bytes';
const k = 1024; const k = 1024;

View File

@ -1,10 +0,0 @@
/**
*
* @param {any[]} array
* @returns {number}
*/
export function getArrayDepth (array) {
return Array.isArray(array)
? 1 + Math.max(0, ...array.map(getArrayDepth))
: 0;
}

View File

@ -0,0 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
export function getArrayDepth (array: any[]): number {
return Array.isArray(array)
? 1 + Math.max(0, ...array.map(getArrayDepth))
: 0;
}

View File

@ -1,5 +1,3 @@
'use strict';
const lookup = { const lookup = {
0: '0000', 0: '0000',
1: '0001', 1: '0001',
@ -23,15 +21,11 @@ const lookup = {
D: '1101', D: '1101',
E: '1110', E: '1110',
F: '1111' F: '1111'
}; } as const;
/** type HexChar = keyof typeof lookup
* Converts hexadecimal string to binary string
* export default function hexToBinary (hex: HexChar[]) {
* @param {string} hex Hexadecimal string
* @returns {string} Binary string
*/
export default function hexToBinary (hex) {
let binary = ''; let binary = '';
for (let i = 0; i < hex.length; i++) for (let i = 0; i < hex.length; i++)
binary += lookup[hex[i]]; binary += lookup[hex[i]];

View File

@ -1,5 +1,4 @@
'use strict'; export function mimeFromHex (hex: string) {
export function mimeFromHex (hex) {
switch (hex.substring(0, 4)) { // 2 bytes switch (hex.substring(0, 4)) { // 2 bytes
case '424D': case '424D':
return { ext: 'bmp', mime: 'image/bmp' }; return { ext: 'bmp', mime: 'image/bmp' };
@ -23,7 +22,7 @@ export function mimeFromHex (hex) {
case '425A68': case '425A68':
return { ext: 'bz2', mime: 'application/x-bzip2' }; return { ext: 'bz2', mime: 'application/x-bzip2' };
default: default:
switch (hex) { // 4 bytes switch (hex) { // 4 bites
case '89504E47': case '89504E47':
return { ext: 'png', mime: 'image/png' }; return { ext: 'png', mime: 'image/png' };
case '47494638': case '47494638':

View File

@ -3,13 +3,7 @@
const pattern = /[\0\x08\x09\x1a\n\r"'\\\%]/gm; const pattern = /[\0\x08\x09\x1a\n\r"'\\\%]/gm;
const regex = new RegExp(pattern); const regex = new RegExp(pattern);
/** function sqlEscaper (string: string) {
* Escapes a string
*
* @param {String} string
* @returns {String}
*/
function sqlEscaper (string) {
return string.replace(regex, char => { return string.replace(regex, char => {
const m = ['\\0', '\\x08', '\\x09', '\\x1a', '\\n', '\\r', '\'', '\"', '\\', '\\\\', '%']; const m = ['\\0', '\\x08', '\\x09', '\\x1a', '\\n', '\\r', '\'', '\"', '\\', '\\\\', '%'];
const r = ['\\\\0', '\\\\b', '\\\\t', '\\\\z', '\\\\n', '\\\\r', '\\\'', '\\\"', '\\\\', '\\\\\\\\', '\%']; const r = ['\\\\0', '\\\\b', '\\\\t', '\\\\z', '\\\\n', '\\\\r', '\\\'', '\\\"', '\\\\', '\\\\\\\\', '\%'];

View File

@ -1,8 +0,0 @@
/**
* @export
* @param {String} [prefix]
* @returns {String} Unique ID
*/
export function uidGen (prefix) {
return (prefix ? `${prefix}:` : '') + Math.random().toString(36).substr(2, 9).toUpperCase();
}

View File

@ -0,0 +1,3 @@
export function uidGen (prefix?: string) {
return (prefix ? `${prefix}:` : '') + Math.random().toString(36).substr(2, 9).toUpperCase();
}

View File

@ -11,6 +11,7 @@
"allowJs": true, "allowJs": true,
"module": "CommonJS", "module": "CommonJS",
"noImplicitAny": true, "noImplicitAny": true,
"jsx": "preserve",
"types": [ "types": [
"node" "node"
], ],
@ -19,7 +20,8 @@
"resolveJsonModule": true, "resolveJsonModule": true,
"removeComments": true, "removeComments": true,
"paths": { "paths": {
"common/*": ["./src/common/*"] "common/*": ["./src/common/*"],
"@/*": ["./src/renderer/*"],
} }
} }
} }

View File

@ -27,7 +27,7 @@ const config = {
name: 'renderer', name: 'renderer',
mode: process.env.NODE_ENV, mode: process.env.NODE_ENV,
devtool: isDevMode ? 'eval-source-map' : false, devtool: isDevMode ? 'eval-source-map' : false,
entry: path.join(__dirname, './src/renderer/index.js'), entry: path.join(__dirname, './src/renderer/index.ts'),
target: 'electron-renderer', target: 'electron-renderer',
output: { output: {
path: path.resolve(__dirname, 'dist'), path: path.resolve(__dirname, 'dist'),