Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/server web default setting #3366

Closed
wants to merge 9 commits into from
64 changes: 64 additions & 0 deletions .scripts/configure.electron.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as fs from 'fs';
import * as path from 'path';
import { hideBin } from 'yargs/helpers';
import yargs from 'yargs';

const argv: any = yargs(hideBin(process.argv)).argv;

function modifiedNextServer() {
const filePath = path.resolve(__dirname, '../apps/server-web/release/app/dist/standalone/apps/web/server.js');

let fileContent = fs.readFileSync(filePath, 'utf8');
const searchString = 'process.env.__NEXT_PRIVATE_STANDALONE_CONFIG';
const codeToInsert = `
nextConfig.serverRuntimeConfig = {
"GAUZY_API_SERVER_URL": process.env.GAUZY_API_SERVER_URL,
"NEXT_PUBLIC_GAUZY_API_SERVER_URL": process.env.NEXT_PUBLIC_GAUZY_API_SERVER_URL
}
`;

let lines = fileContent.split('\n');
const index = lines.findIndex((line) => line.includes(searchString));

if (index !== -1) {
lines.splice(index - 1, 0, codeToInsert);

fileContent = lines.join('\n');
fs.writeFileSync(filePath, fileContent, 'utf8');
console.log('Line of code successfully inserted.');
} else {
console.log(`The string "${searchString}" was not found in the file.`);
}
}

function modifiedWebConstant() {
const filePath = path.resolve(__dirname, '../apps/web/app/constants.ts');

let fileContent = fs.readFileSync(filePath, 'utf8');
const searchString = `export const IS_DESKTOP_APP = process.env.IS_DESKTOP_APP === 'true';`;
const codeToReplace = `export const IS_DESKTOP_APP = true;`;

fileContent = fileContent.replace(searchString, codeToReplace);

fs.writeFileSync(filePath, fileContent, 'utf8');
}

function revertWebConstant() {
const filePath = path.resolve(__dirname, '../apps/web/app/constants.ts');

let fileContent = fs.readFileSync(filePath, 'utf8');
const codeToReplace = `export const IS_DESKTOP_APP = process.env.IS_DESKTOP_APP === 'true';`;
const searchString = `export const IS_DESKTOP_APP = true;`;

fileContent = fileContent.replace(searchString, codeToReplace);

fs.writeFileSync(filePath, fileContent, 'utf8');
}


if (argv.type === 'server') {
modifiedNextServer();
revertWebConstant();
} else if (argv.type === 'constant') {
modifiedWebConstant();
}
24 changes: 12 additions & 12 deletions apps/server-web/src/locales/i18n/bg/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
"SERVER_WINDOW": "Прозорец на сървъра"
},
"MENU_APP": {
"ABOUT": "Относно",
"QUIT": "Изход",
"WINDOW": "Прозорец",
"SUBMENU": {
"SETTING": "Настройки",
"SERVER_WINDOW": "Сървърен прозорец",
"LEARN_MORE": "Научете повече",
"DOC": "Документация",
"SETTING_DEV": "Настройки за разработчици",
"SERVER_DEV": "Сървър за разработчици"
"APP_ABOUT": "Относно",
"APP_QUIT": "Изход",
"APP_WINDOW": "Прозорец",
"APP_SUBMENU": {
"APP_SETTING": "Настройки",
"APP_SERVER_WINDOW": "Сървърен прозорец",
"APP_LEARN_MORE": "Научете повече",
"APP_DOC": "Документация",
"APP_SETTING_DEV": "Настройки за разработчици",
"APP_SERVER_DEV": "Сървър за разработчици"
},
"DEV": "Разработчик",
"HELP": "Помощ"
"APP_DEV": "Разработчик",
"APP_HELP": "Помощ"
},
"FORM": {
"FIELDS": {
Expand Down
24 changes: 12 additions & 12 deletions apps/server-web/src/locales/i18n/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
"SERVER_WINDOW": "Server Window"
},
"MENU_APP": {
"ABOUT": "About",
"QUIT": "Quit",
"WINDOW": "Window",
"SUBMENU": {
"SETTING": "Setting",
"SERVER_WINDOW": "Server Window",
"LEARN_MORE": "Learn More",
"DOC": "Documentation",
"SETTING_DEV": "Setting Dev.",
"SERVER_DEV": "Server Dev."
"APP_ABOUT": "About",
"APP_QUIT": "Quit",
"APP_WINDOW": "Window",
"APP_SUBMENU": {
"APP_SETTING": "Setting",
"APP_SERVER_WINDOW": "Server Window",
"APP_LEARN_MORE": "Learn More",
"APP_DOC": "Documentation",
"APP_SETTING_DEV": "Setting Dev.",
"APP_SERVER_DEV": "Server Dev."
},
"DEV": "Developer",
"HELP": "Help"
"APP_DEV": "Developer",
"APP_HELP": "Help"
},
"FORM": {
"FIELDS": {
Expand Down
7 changes: 5 additions & 2 deletions apps/server-web/src/main/helpers/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export const EventLists = {
SERVER_WINDOW: 'SERVER_WINDOW',
RESTART_SERVER: 'RESTART_SERVER',
CHANGE_THEME: 'CHANGE_THEME',
SETUP_WINDOW: 'SETUP_WINDOW'
SETUP_WINDOW: 'SETUP_WINDOW',
SETTING_WINDOW_DEV: 'SETTING_WINDOW_DEV',
SERVER_WINDOW_DEV: 'SERVER_WINDOW_DEV'
}

export const SettingPageTypeMessage = {
Expand All @@ -38,7 +40,8 @@ export const SettingPageTypeMessage = {
updateSettingResponse: 'update-setting-response',
updateCancel: 'update-cancel',
restartServer: 'restart-server',
themeChange: 'theme-change'
themeChange: 'theme-change',
autoUpdateSetting: 'auto-update-setting'
}

export const ServerPageTypeMessage = {
Expand Down
2 changes: 1 addition & 1 deletion apps/server-web/src/main/helpers/interfaces/i-constant.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export type Channels = 'setting-page' | 'ipc-renderer' | 'language-set' | 'updater-page' | 'server-page' | 'theme-change' | 'current-theme';
export type Channels = 'setting-page' | 'ipc-renderer' | 'language-set' | 'updater-page' | 'server-page' | 'theme-change' | 'current-theme' | 'current-language';
15 changes: 15 additions & 0 deletions apps/server-web/src/main/helpers/interfaces/i-menu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface AppMenu {
id?: string;
label?: string;
click?: () => void;
submenu?: AppSubMenu[]
}

export interface AppSubMenu {
id?: string;
label?: string;
click?: () => void;
selector?: string;
type?: string;
accelerator?: string;
}
4 changes: 2 additions & 2 deletions apps/server-web/src/main/helpers/interfaces/i-server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
interface GeneralConfig {
export interface GeneralConfig {
lang?: string
autoUpdate?: boolean
updateCheckPeriode?: string
Expand All @@ -7,7 +7,7 @@ interface GeneralConfig {
[key: string]: any
}

interface ServerConfig {
export interface ServerConfig {
PORT: number;
NEXT_PUBLIC_GAUZY_API_SERVER_URL: string;
GAUZY_API_SERVER_URL: string;
Expand Down
1 change: 1 addition & 0 deletions apps/server-web/src/main/helpers/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './i-server';
export * from './i-desktop-dialog';
export * from './i-constant';
export * from './i-menu';
12 changes: 12 additions & 0 deletions apps/server-web/src/main/helpers/replace-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,15 @@ export const replaceConfig = async (folderPath: string, envOptions: EnvOptions)
console.log('error on replacing file', error);
}
}

export const clearDesktopConfig = (folderPath: string) => {
const fileNames = ['desktop-server.body', 'desktop-server.meta'];
try {
// remove cached desktop server config
fileNames.forEach((file) => {
fs.unlinkSync(path.join(folderPath, file));
})
} catch (error) {
console.log('skip unlink file on not exists');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class DesktopServerFactory {
if (!this.apiInstance && !!env) {
this.apiInstance = new WebService(path, env, win, signal, eventEmitter);
}
this.apiInstance.env = env;
return this.apiInstance;
}
}
50 changes: 37 additions & 13 deletions apps/server-web/src/main/helpers/services/libs/desktop-store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Store from 'electron-store';
import { WebServer } from '../../interfaces';
import { WebServer, ServerConfig } from '../../interfaces';
const store = new Store();
const DEFAULT_CONFIG:any = {
const DEFAULT_CONFIG:WebServer = {
server: {
PORT: 3002,
GAUZY_API_SERVER_URL: 'http://localhost:3000',
Expand All @@ -11,7 +11,7 @@ const DEFAULT_CONFIG:any = {
general: {
lang: 'en',
autoUpdate: true,
updateCheckPeriode: '1140'
updateCheckPeriode: '1140' // Time in minutes
}
}
export const LocalStore = {
Expand All @@ -35,17 +35,41 @@ export const LocalStore = {
});
},

deepMerge<T>(target: T, source: Partial<T>): T {
const result: T = { ...target };
Object.keys(source).forEach(key => {
const value = source[key as keyof T];
if (value && typeof value === 'object') {
result[key as keyof T] = this.deepMerge(
target[key as keyof T],
value as any
);
} else if (value !== undefined) {
result[key as keyof T] = value as any;
}
});
return result;
},

setDefaultServerConfig: () => {
const defaultConfig: WebServer | any = store.get('config') || {};
Object.keys(DEFAULT_CONFIG).forEach((key) => {
Object.keys(DEFAULT_CONFIG[key]).forEach((keySub) => {
defaultConfig[key] = defaultConfig[key] || {};
defaultConfig[key][keySub] = defaultConfig[key][keySub] || DEFAULT_CONFIG[key][keySub];
})
})
store.set({
config: defaultConfig
validateConfig(config: WebServer): void {
const required = ['PORT', 'GAUZY_API_SERVER_URL'];
required.forEach(field => {
if (!config || !config.server || !config?.server[field as keyof ServerConfig]) {
throw new Error(`Missing required field: ${field}`);
}
});
},

setDefaultServerConfig() {
try {
const storedConfig = store.get('config') as Partial<WebServer> || {};
const mergedConfig = this.deepMerge<WebServer>(DEFAULT_CONFIG, storedConfig)
this.validateConfig(mergedConfig || {});

store.set({ config: mergedConfig });
} catch (error) {
console.error('Failed to set default configuration:', error);
store.set({ config: DEFAULT_CONFIG });
}
}
};
10 changes: 3 additions & 7 deletions apps/server-web/src/main/helpers/services/web-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { EventEmitter } from 'stream';
export class WebService extends ServerTask {
constructor(
readonly path: string,
readonly env: any,
public env: any,
readonly window: BrowserWindow,
readonly signal: AbortSignal,
readonly eventEmitter: EventEmitter
Expand Down Expand Up @@ -38,11 +38,7 @@ export class WebService extends ServerTask {
}
}

private setApiConfig(): void {
// Object.assign(this.args, {
// API_HOST: '0.0.0.0',
// API_PORT: this.config.setting.PORT,
// API_BASE_URL: this.config.apiUrl
// });
public setApiConfig(): void {
Object.assign(this.args, {...this.env});
}
}
Loading
Loading