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",
"@playwright/test": "~1.21.1",
"@types/better-sqlite3": "~7.5.0",
"@types/leaflet": "~1.7.9",
"@types/node": "~17.0.23",
"@types/pg": "~8.6.5",
"@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
defaultPort: null,
defaultUser: null,
@ -68,24 +70,24 @@ module.exports = {
viewUpdateOption: false,
procedureDeterministic: false,
procedureDataAccess: false,
procedureSql: false,
procedureSql: null,
procedureContext: false,
procedureLanguage: false,
functionDeterministic: false,
functionDataAccess: false,
functionSql: false,
functionSql: null,
functionContext: false,
functionLanguage: false,
triggerSql: false,
triggerSql: null,
triggerStatementInCreation: false,
triggerMultipleEvents: false,
triggerTableInName: false,
triggerUpdateColumns: false,
triggerOnlyRename: false,
triggerEnableDisable: false,
triggerFunctionSql: false,
triggerFunctionlanguages: false,
triggerFunctionSql: null,
triggerFunctionlanguages: null,
parametersLength: false,
languages: false,
languages: null,
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
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
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
fileConnection: true,
// Structure

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,4 +1,4 @@
module.exports = [
export default [
'PRIMARY',
'INDEX',
'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) {
export function bufferToBase64 (buf: Buffer) {
const binstr = Array.prototype.map.call(buf, ch => {
return String.fromCharCode(ch);
}).join('');
return btoa(binstr);
return Buffer.from(binstr, 'base64');
}

View File

@ -1,5 +1,4 @@
'use strict';
export function formatBytes (bytes, decimals = 2) {
export function formatBytes (bytes: number, decimals = 2) {
if (bytes === 0) return '0 Bytes';
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 = {
0: '0000',
1: '0001',
@ -23,15 +21,11 @@ const lookup = {
D: '1101',
E: '1110',
F: '1111'
};
} as const;
/**
* Converts hexadecimal string to binary string
*
* @param {string} hex Hexadecimal string
* @returns {string} Binary string
*/
export default function hexToBinary (hex) {
type HexChar = keyof typeof lookup
export default function hexToBinary (hex: HexChar[]) {
let binary = '';
for (let i = 0; i < hex.length; i++)
binary += lookup[hex[i]];

View File

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

View File

@ -3,13 +3,7 @@
const pattern = /[\0\x08\x09\x1a\n\r"'\\\%]/gm;
const regex = new RegExp(pattern);
/**
* Escapes a string
*
* @param {String} string
* @returns {String}
*/
function sqlEscaper (string) {
function sqlEscaper (string: string) {
return string.replace(regex, char => {
const m = ['\\0', '\\x08', '\\x09', '\\x1a', '\\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,
"module": "CommonJS",
"noImplicitAny": true,
"jsx": "preserve",
"types": [
"node"
],
@ -19,7 +20,8 @@
"resolveJsonModule": true,
"removeComments": true,
"paths": {
"common/*": ["./src/common/*"]
"common/*": ["./src/common/*"],
"@/*": ["./src/renderer/*"],
}
}
}

View File

@ -27,7 +27,7 @@ const config = {
name: 'renderer',
mode: process.env.NODE_ENV,
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',
output: {
path: path.resolve(__dirname, 'dist'),