Skip to content

Commit

Permalink
xo: Fix unicorn/prevent-abbreviations.
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Kaseorg <anders@zulip.com>
  • Loading branch information
andersk committed Mar 22, 2024
1 parent 86e28f5 commit 47366b7
Show file tree
Hide file tree
Showing 37 changed files with 425 additions and 383 deletions.
24 changes: 12 additions & 12 deletions app/common/config-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ const logger = new Logger({
file: "config-util.log",
});

let db: JsonDB;
let database: JsonDB;

reloadDb();
reloadDatabase();

export function getConfigItem<Key extends keyof Config>(
key: Key,
defaultValue: Config[Key],
): z.output<(typeof configSchemata)[Key]> {
try {
db.reload();
database.reload();
} catch (error: unknown) {
logger.error("Error while reloading settings.json: ");
logger.error(error);
}

try {
return configSchemata[key].parse(db.getObject<unknown>(`/${key}`));
return configSchemata[key].parse(database.getObject<unknown>(`/${key}`));
} catch (error: unknown) {
if (!(error instanceof DataError)) throw error;
setConfigItem(key, defaultValue);
Expand All @@ -46,13 +46,13 @@ export function getConfigItem<Key extends keyof Config>(
// This function returns whether a key exists in the configuration file (settings.json)
export function isConfigItemExists(key: string): boolean {
try {
db.reload();
database.reload();
} catch (error: unknown) {
logger.error("Error while reloading settings.json: ");
logger.error(error);
}

return db.exists(`/${key}`);
return database.exists(`/${key}`);
}

export function setConfigItem<Key extends keyof Config>(
Expand All @@ -66,16 +66,16 @@ export function setConfigItem<Key extends keyof Config>(
}

configSchemata[key].parse(value);
db.push(`/${key}`, value, true);
db.save();
database.push(`/${key}`, value, true);
database.save();
}

export function removeConfigItem(key: string): void {
db.delete(`/${key}`);
db.save();
database.delete(`/${key}`);
database.save();
}

function reloadDb(): void {
function reloadDatabase(): void {
const settingsJsonPath = path.join(
app.getPath("userData"),
"/config/settings.json",
Expand All @@ -96,5 +96,5 @@ function reloadDb(): void {
}
}

db = new JsonDB(settingsJsonPath, true, true);
database = new JsonDB(settingsJsonPath, true, true);
}
28 changes: 14 additions & 14 deletions app/common/default-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@ import {app} from "zulip:remote";

let setupCompleted = false;

const zulipDir = app.getPath("userData");
const logDir = `${zulipDir}/Logs/`;
const configDir = `${zulipDir}/config/`;
const zulipDirectory = app.getPath("userData");
const logDirectory = `${zulipDirectory}/Logs/`;
const configDirectory = `${zulipDirectory}/config/`;
export const initSetUp = (): void => {
// If it is the first time the app is running
// create zulip dir in userData folder to
// avoid errors
if (!setupCompleted) {
if (!fs.existsSync(zulipDir)) {
fs.mkdirSync(zulipDir);
if (!fs.existsSync(zulipDirectory)) {
fs.mkdirSync(zulipDirectory);
}

if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
if (!fs.existsSync(logDirectory)) {
fs.mkdirSync(logDirectory);
}

// Migrate config files from app data folder to config folder inside app
// data folder. This will be done once when a user updates to the new version.
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir);
const domainJson = `${zulipDir}/domain.json`;
const settingsJson = `${zulipDir}/settings.json`;
const updatesJson = `${zulipDir}/updates.json`;
const windowStateJson = `${zulipDir}/window-state.json`;
if (!fs.existsSync(configDirectory)) {
fs.mkdirSync(configDirectory);
const domainJson = `${zulipDirectory}/domain.json`;
const settingsJson = `${zulipDirectory}/settings.json`;
const updatesJson = `${zulipDirectory}/updates.json`;
const windowStateJson = `${zulipDirectory}/window-state.json`;
const configData = [
{
path: domainJson,
Expand All @@ -44,7 +44,7 @@ export const initSetUp = (): void => {
];
for (const data of configData) {
if (fs.existsSync(data.path)) {
fs.copyFileSync(data.path, configDir + data.fileName);
fs.copyFileSync(data.path, configDirectory + data.fileName);
fs.unlinkSync(data.path);
}
}
Expand Down
8 changes: 4 additions & 4 deletions app/common/enterprise-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const logger = new Logger({
let enterpriseSettings: Partial<EnterpriseConfig>;
let configFile: boolean;

reloadDb();
reloadDatabase();

function reloadDb(): void {
function reloadDatabase(): void {
let enterpriseFile = "/etc/zulip-desktop-config/global_config.json";
if (process.platform === "win32") {
enterpriseFile =
Expand Down Expand Up @@ -56,7 +56,7 @@ export function getConfigItem<Key extends keyof EnterpriseConfig>(
key: Key,
defaultValue: EnterpriseConfig[Key],
): EnterpriseConfig[Key] {
reloadDb();
reloadDatabase();
if (!configFile) {
return defaultValue;
}
Expand All @@ -66,7 +66,7 @@ export function getConfigItem<Key extends keyof EnterpriseConfig>(
}

export function configItemExists(key: keyof EnterpriseConfig): boolean {
reloadDb();
reloadDatabase();
if (!configFile) {
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions app/common/link-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export async function openBrowser(url: URL): Promise<void> {
} else {
// For security, indirect links to non-whitelisted protocols
// through a real web browser via a local HTML file.
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "zulip-redirect-"));
const file = path.join(dir, "redirect.html");
const directory = fs.mkdtempSync(path.join(os.tmpdir(), "zulip-redirect-"));
const file = path.join(directory, "redirect.html");
fs.writeFileSync(
file,
html`
Expand All @@ -37,7 +37,7 @@ export async function openBrowser(url: URL): Promise<void> {
await shell.openPath(file);
setTimeout(() => {
fs.unlinkSync(file);
fs.rmdirSync(dir);
fs.rmdirSync(directory);
}, 15_000);
}
}
34 changes: 17 additions & 17 deletions app/common/logger-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type LoggerOptions = {

initSetUp();

const logDir = `${app.getPath("userData")}/Logs`;
const logDirectory = `${app.getPath("userData")}/Logs`;

type Level = "log" | "debug" | "info" | "warn" | "error";

Expand All @@ -23,7 +23,7 @@ export default class Logger {
constructor(options: LoggerOptions = {}) {
let {file = "console.log"} = options;

file = `${logDir}/${file}`;
file = `${logDirectory}/${file}`;

// Trim log according to type of process
if (process.type === "renderer") {
Expand All @@ -38,31 +38,31 @@ export default class Logger {
this.nodeConsole = nodeConsole;
}

_log(type: Level, ...args: unknown[]): void {
args.unshift(this.getTimestamp() + " |\t");
args.unshift(type.toUpperCase() + " |");
this.nodeConsole[type](...args);
console[type](...args);
_log(type: Level, ...arguments_: unknown[]): void {
arguments_.unshift(this.getTimestamp() + " |\t");
arguments_.unshift(type.toUpperCase() + " |");
this.nodeConsole[type](...arguments_);
console[type](...arguments_);
}

log(...args: unknown[]): void {
this._log("log", ...args);
log(...arguments_: unknown[]): void {
this._log("log", ...arguments_);
}

debug(...args: unknown[]): void {
this._log("debug", ...args);
debug(...arguments_: unknown[]): void {
this._log("debug", ...arguments_);
}

info(...args: unknown[]): void {
this._log("info", ...args);
info(...arguments_: unknown[]): void {
this._log("info", ...arguments_);
}

warn(...args: unknown[]): void {
this._log("warn", ...args);
warn(...arguments_: unknown[]): void {
this._log("warn", ...arguments_);
}

error(...args: unknown[]): void {
this._log("error", ...args);
error(...arguments_: unknown[]): void {
this._log("error", ...arguments_);
}

getTimestamp(): string {
Expand Down
8 changes: 4 additions & 4 deletions app/common/typed-ipc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {DndSettings} from "./dnd-util.js";
import type {MenuProps, ServerConf} from "./types.js";
import type {MenuProperties, ServerConfig} from "./types.js";

export type MainMessage = {
"clear-app-settings": () => void;
Expand All @@ -21,12 +21,12 @@ export type MainMessage = {
toggleAutoLauncher: (AutoLaunchValue: boolean) => void;
"unread-count": (unreadCount: number) => void;
"update-badge": (messageCount: number) => void;
"update-menu": (props: MenuProps) => void;
"update-menu": (properties: MenuProperties) => void;
"update-taskbar-icon": (data: string, text: string) => void;
};

export type MainCall = {
"get-server-settings": (domain: string) => ServerConf;
"get-server-settings": (domain: string) => ServerConfig;
"is-online": (url: string) => boolean;
"poll-clipboard": (key: Uint8Array, sig: Uint8Array) => string | undefined;
"save-server-icon": (iconURL: string) => string | null;
Expand Down Expand Up @@ -74,7 +74,7 @@ export type RendererMessage = {
"toggle-silent": (state: boolean) => void;
"toggle-tray": (state: boolean) => void;
toggletray: () => void;
tray: (arg: number) => void;
tray: (argument: number) => void;
"update-realm-icon": (serverURL: string, iconURL: string) => void;
"update-realm-name": (serverURL: string, realmName: string) => void;
"webview-reload": () => void;
Expand Down
6 changes: 3 additions & 3 deletions app/common/types.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
export type MenuProps = {
export type MenuProperties = {
tabs: TabData[];
activeTabIndex?: number;
enableMenu?: boolean;
};

export type NavItem =
export type NavigationItem =
| "General"
| "Network"
| "AddServer"
| "Organizations"
| "Shortcuts";

export type ServerConf = {
export type ServerConfig = {
url: string;
alias: string;
icon: string;
Expand Down
10 changes: 5 additions & 5 deletions app/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import windowStateKeeper from "electron-window-state";
import * as ConfigUtil from "../common/config-util.js";
import {bundlePath, bundleUrl, publicPath} from "../common/paths.js";
import type {RendererMessage} from "../common/typed-ipc.js";
import type {MenuProps} from "../common/types.js";
import type {MenuProperties} from "../common/types.js";

import {appUpdater, shouldQuitForUpdate} from "./autoupdater.js";
import * as BadgeSettings from "./badge-settings.js";
Expand Down Expand Up @@ -424,10 +424,10 @@ ${error}`,
},
);

ipcMain.on("update-menu", (_event, props: MenuProps) => {
AppMenu.setMenu(props);
if (props.activeTabIndex !== undefined) {
const activeTab = props.tabs[props.activeTabIndex];
ipcMain.on("update-menu", (_event, properties: MenuProperties) => {
AppMenu.setMenu(properties);
if (properties.activeTabIndex !== undefined) {
const activeTab = properties.tabs[properties.activeTabIndex];
mainWindow.setTitle(`Zulip - ${activeTab.name}`);
}
});
Expand Down
20 changes: 10 additions & 10 deletions app/main/linux-update-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ const logger = new Logger({
file: "linux-update-util.log",
});

let db: JsonDB;
let database: JsonDB;

reloadDb();
reloadDatabase();

export function getUpdateItem(
key: string,
defaultValue: true | null = null,
): true | null {
reloadDb();
reloadDatabase();
let value: unknown;
try {
value = db.getObject<unknown>(`/${key}`);
value = database.getObject<unknown>(`/${key}`);
} catch (error: unknown) {
if (!(error instanceof DataError)) throw error;
}
Expand All @@ -36,16 +36,16 @@ export function getUpdateItem(
}

export function setUpdateItem(key: string, value: true | null): void {
db.push(`/${key}`, value, true);
reloadDb();
database.push(`/${key}`, value, true);
reloadDatabase();
}

export function removeUpdateItem(key: string): void {
db.delete(`/${key}`);
reloadDb();
database.delete(`/${key}`);
reloadDatabase();
}

function reloadDb(): void {
function reloadDatabase(): void {
const linuxUpdateJsonPath = path.join(
app.getPath("userData"),
"/config/updates.json",
Expand All @@ -65,5 +65,5 @@ function reloadDb(): void {
}
}

db = new JsonDB(linuxUpdateJsonPath, true, true);
database = new JsonDB(linuxUpdateJsonPath, true, true);
}

0 comments on commit 47366b7

Please sign in to comment.