Skip to content

Commit

Permalink
avoid dev tooling in production
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeniau committed Mar 6, 2024
1 parent 2608667 commit 1dac803
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/configuration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions src/log.ts
Original file line number Diff line number Diff line change
@@ -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);
}
12 changes: 10 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) => {
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/sql/index.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
});

Expand Down

0 comments on commit 1dac803

Please sign in to comment.