Skip to content

Commit

Permalink
Add linting and style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SleepyLeslie committed Jun 7, 2024
1 parent 404270d commit 0b104c6
Show file tree
Hide file tree
Showing 8 changed files with 404 additions and 125 deletions.
12 changes: 5 additions & 7 deletions ext/app/electron/UpdateManager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as electron from 'electron';
import * as electronUpdater from 'electron-updater';
import * as log from 'app/server/lib/log';
import * as version from 'app/common/version';
import * as electron from "electron";
import * as electronUpdater from "electron-updater";
import * as log from "app/server/lib/log";
import * as version from "app/common/version";

class UpdateManager {
export class UpdateManager {
private _suppressPopups: boolean;
private _appMenu: any;
private _timeout: any;
Expand Down Expand Up @@ -175,5 +175,3 @@ class UpdateManager {
}) as any);
}
}

module.exports = UpdateManager;
119 changes: 60 additions & 59 deletions ext/app/electron/config.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import * as path from 'path'
import * as fse from 'fs-extra'
import * as electron from 'electron';
import * as ini from 'ini'
import * as os from 'os'
import * as log from 'app/server/lib/log';
import * as packageJson from 'desktop.package.json';
import {commonUrls} from 'app/common/gristUrls';
import * as electron from "electron";
import * as fse from "fs-extra";
import * as ini from "ini";
import * as log from "app/server/lib/log";
import * as os from "os";
import * as packageJson from "desktop.package.json";
import * as path from "path";
import { commonUrls } from "app/common/gristUrls";

const CONFIG_DIR = path.join(electron.app.getPath("appData"), packageJson.name)
const CONFIG_DIR = path.join(electron.app.getPath("appData"), packageJson.name);

const APPDATA_DIR = (process.platform == "win32") ? electron.app.getPath("userData") :
path.join(electron.app.getPath("home"), ".local", "share", packageJson.name)
path.join(electron.app.getPath("home"), ".local", "share", packageJson.name);

// Electron's app.getPath("userData") uses productName instead of name
// but productName should be "full capitalized name", not ideal for naming our config directory.
const DEFAULT_CONFIG_FILE = path.join(CONFIG_DIR, "config.ini")
const NO_VALIDATION = () => true
const DEFAULT_CONFIG_FILE = path.join(CONFIG_DIR, "config.ini");
const NO_VALIDATION = () => true;

/**
* Suggest a value for an environment variable. If the variable is already set, do nothing. Otherwise set the value.
Expand All @@ -28,14 +28,15 @@ function suggestEnv(name: string, value: string): void {
}
}

type IConfig = Record<string, any>
// The ini library recognizes boolean values, but not numbers. All other values are treated as strings.
type INI = { [key: string]: (INI | string | boolean) }

class Config implements IConfig {
class Config {

[key: string]: any
#config: INI;

constructor(c: IConfig) {
Object.assign(this, c)
constructor(c: INI) {
this.#config = c;
}

/**
Expand All @@ -53,135 +54,135 @@ class Config implements IConfig {
validator: (value: string) => boolean,
defaultValue: string,
): void {
let confValue: any = this
let confValue: INI[keyof INI] = this.#config;
for (const segment of confKey.split(".")) {
confValue = confValue[segment]
confValue = (confValue as INI)[segment];
if (confValue === undefined) {
break
break;
}
}
let envValue = process.env[envKey]
const envValue = process.env[envKey];
if (envValue === undefined) {
if (confValue === undefined) {
log.info(`Neither ${confKey} nor $${envKey} is specified, using default value ${defaultValue}`)
process.env[envKey] = defaultValue
return
log.info(`Neither ${confKey} nor $${envKey} is specified, using default value ${defaultValue}`);
process.env[envKey] = defaultValue;
return;
} else {
// envvar is undefined but config has it
if (!validator(confValue)) {
log.warn(`${confKey} has invalid value ${confValue}, using default value ${defaultValue}`)
process.env[envKey] = defaultValue
if (!["string", "boolean"].includes(typeof confValue) || !validator(confValue.toString())) {
log.warn(`${confKey} has invalid value ${confValue}, using default value ${defaultValue}`);
process.env[envKey] = defaultValue;
} else {
process.env[envKey] = confValue
process.env[envKey] = confValue.toString();
}
}
} else {
// envvar is defined, ignore config
if (confValue !== undefined) {
log.warn(`${confKey} is overridden by $${envKey}`)
log.warn(`${confKey} is overridden by $${envKey}`);
}
if (!validator(envValue)) {
log.warn(`$${envKey} has invalid value ${envValue}, using default value ${defaultValue}`)
process.env[envKey] = defaultValue
log.warn(`$${envKey} has invalid value ${envValue}, using default value ${defaultValue}`);
process.env[envKey] = defaultValue;
}
}
}
}


export function loadConfigFile(filename: string = DEFAULT_CONFIG_FILE) {
let config: Config
let config: Config;
try {
let configBuffer = fse.readFileSync(filename)
config = new Config(ini.parse(configBuffer.toString("utf8")))
const configBuffer = fse.readFileSync(filename);
config = new Config(ini.parse(configBuffer.toString("utf8")));
} catch (err) {
log.warn(`Failed to read configuration file: ${err}`)
config = new Config({})
log.warn(`Failed to read configuration file: ${err}`);
config = new Config({});
}
// Section: login
config.apply(
"login.username",
"GRIST_DEFAULT_USERNAME",
NO_VALIDATION,
getUsername()
)
);
config.apply(
"login.email",
"GRIST_DEFAULT_EMAIL",
NO_VALIDATION,
getEmail()
)
);
// Section: server
config.apply(
"server.listen",
"GRIST_HOST",
NO_VALIDATION,
"localhost"
)
);
config.apply(
"server.port",
"GRIST_PORT",
(portstr) => {
if (! /^\d+$/.test(portstr)) {
return false
return false;
}
let port = parseInt(portstr)
return port > 0 && port < 65536
const port = parseInt(portstr);
return port > 0 && port < 65536;
},
"_RANDOM"
)
);
config.apply(
"server.auth",
"GRIST_DESKTOP_AUTH",
(auth) => ["strict", "none", "mixed"].includes(auth),
"strict"
)
);
// Section: sandbox
config.apply(
"sandbox.flavor",
"GRIST_SANDBOX_FLAVOR",
(flavor) => ["pyodide", "unsandboxed", "gvisor", "macSandboxExec"].includes(flavor),
"pyodide"
)
);
// Section: storage
config.apply(
"storage.instance",
"GRIST_INST_DIR",
NO_VALIDATION,
APPDATA_DIR
)
);
config.apply(
"storage.documents",
"GRIST_DATA_DIR",
NO_VALIDATION,
electron.app.getPath("documents")
)
);
config.apply(
"storage.plugins",
"GRIST_USER_ROOT",
NO_VALIDATION,
path.join(electron.app.getPath("home"), ".grist")
)
);
config.apply(
"storage.homedb",
"TYPEORM_DATABASE",
NO_VALIDATION,
path.join(APPDATA_DIR, "home.sqlite3")
)
);

const homeDBLocation = path.parse((process.env.TYPEORM_DATABASE as string)).dir
const homeDBLocation = path.parse((process.env.TYPEORM_DATABASE as string)).dir;
if (!fse.existsSync(homeDBLocation)) {
log.warn(`Directory to contain the home DB does not exist, creating ${homeDBLocation}`)
fse.mkdirSync(homeDBLocation)
log.warn(`Directory to contain the home DB does not exist, creating ${homeDBLocation}`);
fse.mkdirSync(homeDBLocation);
}

// We don't allow manually setting these envvars anymore. Fixing them makes maintaining grist-desktop easier.
process.env.GRIST_SINGLE_PORT = "true"
process.env.GRIST_SERVE_SAME_ORIGIN = "true"
process.env.GRIST_DEFAULT_PRODUCT = "Free"
process.env.GRIST_SINGLE_PORT = "true";
process.env.GRIST_SERVE_SAME_ORIGIN = "true";
process.env.GRIST_DEFAULT_PRODUCT = "Free";
process.env.GRIST_ORG_IN_PATH = "true";
process.env.GRIST_HIDE_UI_ELEMENTS = "helpCenter,billing,templates,multiSite,multiAccounts";
process.env.GRIST_CONTACT_SUPPORT_URL = packageJson.repository + "/issues"
process.env.GRIST_CONTACT_SUPPORT_URL = packageJson.repository + "/issues";
if (process.env.GRIST_DESKTOP_AUTH !== "mixed") {
process.env.GRIST_FORCE_LOGIN = "true";
}
Expand All @@ -193,12 +194,12 @@ export function loadConfigFile(filename: string = DEFAULT_CONFIG_FILE) {

function getUsername(): string {
try {
return os.userInfo().username
return os.userInfo().username;
} catch {
return "You"
return "You";
}
}

function getEmail(): string {
return getUsername().toLowerCase() + "@" + os.hostname()
return getUsername().toLowerCase() + "@" + os.hostname();
}
15 changes: 7 additions & 8 deletions ext/app/electron/logins.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {ApiError} from 'app/common/ApiError';
import { UserProfile } from 'app/common/LoginSessionAPI';
import {expressWrap} from 'app/server/lib/expressWrap';
import {GristLoginMiddleware, GristLoginSystem, GristServer,
setUserInSession} from 'app/server/lib/GristServer';
import {getOrgUrl} from 'app/server/lib/requestUtils';
import {Request} from 'express';
import { GristLoginMiddleware, GristLoginSystem, GristServer, setUserInSession } from "app/server/lib/GristServer";
import { ApiError } from "app/common/ApiError";
import { Request } from "express";
import { UserProfile } from "app/common/LoginSessionAPI";
import cookie from "cookie";
import { expressWrap } from "app/server/lib/expressWrap";
import { getOrgUrl } from "app/server/lib/requestUtils";

const cookie = require('cookie');

export type GristDesktopAuthMode = 'strict' | 'none' | 'mixed';

Expand Down
Loading

0 comments on commit 0b104c6

Please sign in to comment.