Skip to content

Commit

Permalink
Use template literals for paths
Browse files Browse the repository at this point in the history
  • Loading branch information
electronfriends committed Apr 13, 2024
1 parent 3510122 commit e49712f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 32 deletions.
9 changes: 4 additions & 5 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import path from 'node:path';

import { app } from 'electron';
import settings from 'electron-settings';

const appPath = app.isPackaged ? process.resourcesPath : app.getAppPath();
const defaultPath = 'C:\\Wemp';

export default {
paths: {
icons: path.join(app.getAppPath(), 'icons'),
logs: path.join(app.getPath('userData'), 'error.log'),
icons: `${appPath}/icons`,
logs: `${app.getPath('userData')}/error.log`,
services: settings.getSync('path')?.toString() || defaultPath,
stubs: path.join(app.getAppPath(), 'stubs')
stubs: `${appPath}/stubs`
},
services: [
{
Expand Down
9 changes: 4 additions & 5 deletions src/main-process/manager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs from 'node:fs';
import path from 'node:path';

import { dialog } from 'electron';
import settings from 'electron-settings';
Expand Down Expand Up @@ -27,7 +26,7 @@ export async function checkServices() {

for (const service of config.services) {
const serviceName = service.name.toLowerCase();
const servicePath = path.join(servicesPath, serviceName);
const servicePath = `${servicesPath}/${serviceName}`;
const serviceVersion = settings.getSync(`paths.${servicesPath}.${serviceName}`);

let currentService;
Expand All @@ -52,10 +51,10 @@ export async function checkServices() {
if (service.name === 'MariaDB') {
await currentService.install();
} else {
const stubConfigPath = path.join(config.paths.stubs, `${serviceName}/${service.config}`);
const stubConfigPath = `${config.paths.stubs}/${serviceName}/${service.config}`;
const content = fs.readFileSync(stubConfigPath, 'utf8');
const modifiedContent = content.replace('{servicesPath}', servicesPath);
fs.writeFileSync(path.join(servicePath, service.config), modifiedContent);
fs.writeFileSync(`${servicePath}/${service.config}`, modifiedContent);
}
}

Expand Down Expand Up @@ -100,7 +99,7 @@ function watchServiceConfig(serviceName) {
const serviceConfig = config.services.find(s => s.name === serviceName)?.config;

if (service && serviceConfig) {
const serviceConfigPath = path.join(servicesPath, serviceName, serviceConfig);
const serviceConfigPath = `${servicesPath}/${serviceName}/${serviceConfig}`;
if (fs.existsSync(serviceConfigPath)) {
service.debounce = service.debounce || null;

Expand Down
30 changes: 14 additions & 16 deletions src/main-process/menu.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import path from 'node:path';

import { Menu, MenuItem, Tray, app, shell } from 'electron';
import settings from 'electron-settings';

Expand All @@ -19,25 +17,25 @@ const iconsPath = config.paths.icons;
function createServiceMenuItem(service) {
const serviceName = service.name.toLowerCase();
const submenu = service.name !== 'phpMyAdmin' ? [
{ icon: path.join(iconsPath, 'circled-play.png'), label: 'Start', click: () => startService(service.name) },
{ icon: path.join(iconsPath, 'restart.png'), label: 'Restart', click: () => stopService(service.name, true) },
{ icon: path.join(iconsPath, 'shutdown.png'), label: 'Stop', click: () => stopService(service.name) },
{ icon: `${iconsPath}/circled-play.png`, label: 'Start', click: () => startService(service.name) },
{ icon: `${iconsPath}/restart.png`, label: 'Restart', click: () => stopService(service.name, true) },
{ icon: `${iconsPath}/shutdown.png`, label: 'Stop', click: () => stopService(service.name) },
{ type: 'separator' }
] : [
{ icon: path.join(iconsPath, 'web.png'), label: 'Open Web Interface', click: () => shell.openExternal(service.webInterfaceUrl) },
{ icon: `${iconsPath}/web.png`, label: 'Open Web Interface', click: () => shell.openExternal(service.webInterfaceUrl) },
];

return new MenuItem({
icon: path.join(iconsPath, serviceName + '.png'),
icon: `${iconsPath}/${serviceName}.png`,
id: service.name,
label: service.name,
visible: service.name === 'phpMyAdmin',
submenu: [
{ icon: path.join(iconsPath, serviceName + '.png'), label: `${service.name} ${service.version}`, enabled: false },
{ icon: `${iconsPath}/${serviceName}.png`, label: `${service.name} ${service.version}`, enabled: false },
{ type: 'separator' },
...submenu,
{ icon: path.join(iconsPath, 'settings.png'), label: 'Open Configuration', click: () => shell.openPath(path.join(config.paths.services, serviceName, service.config)) },
{ icon: path.join(iconsPath, 'folder.png'), label: 'Open Directory', click: () => shell.openPath(path.join(config.paths.services, serviceName)) }
{ icon: `${iconsPath}/settings.png`, label: 'Open Configuration', click: () => shell.openPath(`${config.paths.services}/${serviceName}/${service.config}`) },
{ icon: `${iconsPath}/folder.png`, label: 'Open Directory', click: () => shell.openPath(`${config.paths.services}/${serviceName}`) }
]
});
}
Expand All @@ -50,12 +48,12 @@ export function createMenu() {

const menuTemplate = [
{
icon: path.join(iconsPath, 'wemp.png'),
icon: `${iconsPath}/wemp.png`,
label: `Wemp ${app.getVersion()}`,
submenu: [
{ icon: path.join(iconsPath, 'restart.png'), label: 'Restart All Services', click: () => stopServices(true) },
{ icon: path.join(iconsPath, 'folder.png'), label: 'Set Services Path', click: async () => { await setServicesPath(); await stopServices(); app.relaunch(); app.exit(0); } },
{ icon: path.join(iconsPath, 'event-log.png'), label: 'View Error Logs', click: () => shell.openPath(config.paths.logs) },
{ icon: `${iconsPath}/restart.png`, label: 'Restart All Services', click: () => stopServices(true) },
{ icon: `${iconsPath}/folder.png`, label: 'Set Services Path', click: async () => { await setServicesPath(); await stopServices(); app.relaunch(); app.exit(0); } },
{ icon: `${iconsPath}/event-log.png`, label: 'View Error Logs', click: () => shell.openPath(config.paths.logs) },
{ type: 'separator' },
{ type: 'checkbox', label: 'Autostart Wemp', checked: app.getLoginItemSettings().openAtLogin, click: (menuItem) => app.setLoginItemSettings({ openAtLogin: menuItem.checked }) },
{ type: 'checkbox', label: 'Show Ready Notification', checked: settings.getSync('showReadyNotification'), click: (menuItem) => settings.setSync('showReadyNotification', menuItem.checked) }
Expand All @@ -64,11 +62,11 @@ export function createMenu() {
{ type: 'separator' },
...serviceMenuItems,
{ type: 'separator' },
{ icon: path.join(iconsPath, 'shutdown.png'), label: 'Quit Wemp', click: () => app.quit() }
{ icon: `${iconsPath}/shutdown.png`, label: 'Quit Wemp', click: () => app.quit() }
];

menu = Menu.buildFromTemplate(menuTemplate);
tray = new Tray(path.join(iconsPath, 'wemp.png'));
tray = new Tray(`${iconsPath}/wemp.png`);
tray.on('click', () => tray.popUpContextMenu());
tray.setToolTip('Click to manage your web server');
tray.setContextMenu(menu);
Expand Down
4 changes: 1 addition & 3 deletions src/services/base-service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import path from 'node:path';

import config from '../config';
import Process from '../utils/process';

Expand All @@ -14,7 +12,7 @@ class BaseService {
}

get cwd() {
return path.join(config.paths.services, this.name.toLowerCase(), this.binPath);
return `${config.paths.services}/${this.name.toLowerCase()}/${this.binPath}`;
}

async start() {
Expand Down
5 changes: 2 additions & 3 deletions src/utils/download.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs from 'node:fs';
import path from 'node:path';

import fetch from 'node-fetch';
import unzipper from 'unzipper';
Expand All @@ -15,7 +14,7 @@ import config from '../config';
export default async function download(service, isUpdate) {
try {
const serviceName = service.name.toLowerCase();
const servicePath = path.join(config.paths.services, serviceName);
const servicePath = `${config.paths.services}/${serviceName}`;

if (!fs.existsSync(servicePath)) {
fs.mkdirSync(servicePath, { recursive: true });
Expand Down Expand Up @@ -49,7 +48,7 @@ export default async function download(service, isUpdate) {
if (isConfigFile || isIgnored) {
entry.autodrain();
} else {
const fileDestPath = path.join(servicePath, fileName);
const fileDestPath = `${servicePath}/${fileName}`;

if (entry.type === 'Directory') {
fs.mkdirSync(fileDestPath, { recursive: true });
Expand Down

0 comments on commit e49712f

Please sign in to comment.