diff --git a/src/configuration/index.ts b/src/configuration/index.ts index 6099ab0..2155427 100644 --- a/src/configuration/index.ts +++ b/src/configuration/index.ts @@ -2,6 +2,7 @@ import { dialog, safeStorage } from 'electron'; import { existsSync, mkdirSync, readFileSync, writeFile } from 'node:fs'; import { resolve } from 'node:path'; import envPaths from 'env-paths'; +import { log } from '../log'; import { CONFIGURATION_CHANNEL } from '../preload/configurationChannel'; import { ConnectionObject } from '../sql/types'; import { DEFAULT_THEME } from './themes'; @@ -16,7 +17,7 @@ import { const envPath = envPaths('TianaTables', { suffix: '' }); const dataFilePath = resolve(envPath.config, 'config.json'); -console.log('[CONFIG] Configuration file path:', dataFilePath); +log('CONFIG', 'Configuration file path:', dataFilePath); function getBaseConfig(): Configuration { return { diff --git a/src/log.ts b/src/log.ts new file mode 100644 index 0000000..e93004f --- /dev/null +++ b/src/log.ts @@ -0,0 +1,7 @@ +export function log(domain: string, message: string, ...args: unknown[]) { + if (process.env.NODE_ENV !== 'development') { + return; + } + + console.log(`${domain} ${message}`, ...args); +} diff --git a/src/main.ts b/src/main.ts index 537994d..606f026 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,6 +5,7 @@ import installExtension, { } from 'electron-devtools-installer'; import { updateElectronApp } from 'update-electron-app'; import { bindIpcMain as bindIpcMainConfiguration } from './configuration'; +import { log } from './log'; import connectionStackInstance from './sql'; // Handle creating/removing shortcuts on Windows when installing/uninstalling. @@ -17,9 +18,14 @@ updateElectronApp(); const isMac = process.platform !== 'darwin'; function installReactDevToolsExtension() { + // don't install the extension in production + if (process.env.NODE_ENV === 'development') { + return; + } + installExtension(REACT_DEVELOPER_TOOLS) .then((name) => { - console.log(`[DEBUG] Added Extension: ${name}`); + log('DEBUG', `Added Extension: ${name}`); // once extension is loaded, reload the view after a short period (probably to be sure that the extension is loaded ?) BrowserWindow.getAllWindows().forEach((win) => { @@ -51,7 +57,9 @@ const createWindow = () => { } // Open the DevTools. - mainWindow.webContents.openDevTools(); + if (process.env.NODE_ENV === 'development') { + mainWindow.webContents.openDevTools(); + } }; // This method will be called when Electron has finished diff --git a/src/sql/index.ts b/src/sql/index.ts index ed7648e..ea7e497 100644 --- a/src/sql/index.ts +++ b/src/sql/index.ts @@ -1,5 +1,6 @@ import { Connection, createConnection } from 'mysql2/promise'; import { getConfiguration } from '../configuration'; +import { log } from '../log'; import { SQL_CHANNEL } from '../preload/sqlChannel'; import { ConnectionObject, QueryResult } from './types'; @@ -25,7 +26,7 @@ class ConnectionStack { async executeQuery(connectionName: string, query: string): QueryResult { const connection = await this.#getConnection(connectionName); - console.log(`[SQL] Execute query on "${connectionName}": "${query}"`); + log('SQL', `Execute query on "${connectionName}": "${query}"`); return await connection.query(query); } @@ -63,13 +64,13 @@ class ConnectionStack { throw new Error(`Connection already opened on "${name}"`); } - console.log(`[SQL] Open connection to "${name}"`); + log('SQL', `Open connection to "${name}"`); // TODO use a connection pool instead ? https://github.com/mysqljs/mysql?tab=readme-ov-file#establishing-connections const connection = await createConnection(rest); connection.on('end', () => { - console.log(`[SQL] Connection to "${name}" ended`); + log('SQL', `Connection to "${name}" ended`); this.connections.delete(name); });