mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-28 10:32:27 +08:00
refactor(server/utils): isDev move to utils and replace fn with boolean
this value cannot change during runtime, => there is no need to have these checks as dynamic function, instead just export the boolean value directly
This commit is contained in:
parent
13a1b42e12
commit
818cc30650
@ -2,11 +2,11 @@ import assetPath from "../services/asset_path.js";
|
|||||||
import path from "path";
|
import path from "path";
|
||||||
import { fileURLToPath } from "url";
|
import { fileURLToPath } from "url";
|
||||||
import express from "express";
|
import express from "express";
|
||||||
import env from "../services/env.js";
|
import { isDev } from "../services/utils.js";
|
||||||
import type serveStatic from "serve-static";
|
import type serveStatic from "serve-static";
|
||||||
|
|
||||||
const persistentCacheStatic = (root: string, options?: serveStatic.ServeStaticOptions<express.Response<any, Record<string, any>>>) => {
|
const persistentCacheStatic = (root: string, options?: serveStatic.ServeStaticOptions<express.Response<any, Record<string, any>>>) => {
|
||||||
if (!env.isDev()) {
|
if (!isDev) {
|
||||||
options = {
|
options = {
|
||||||
maxAge: "1y",
|
maxAge: "1y",
|
||||||
...options
|
...options
|
||||||
@ -17,7 +17,7 @@ const persistentCacheStatic = (root: string, options?: serveStatic.ServeStaticOp
|
|||||||
|
|
||||||
async function register(app: express.Application) {
|
async function register(app: express.Application) {
|
||||||
const srcRoot = path.join(path.dirname(fileURLToPath(import.meta.url)), "..");
|
const srcRoot = path.join(path.dirname(fileURLToPath(import.meta.url)), "..");
|
||||||
if (env.isDev()) {
|
if (isDev) {
|
||||||
const webpack = (await import("webpack")).default;
|
const webpack = (await import("webpack")).default;
|
||||||
const webpackMiddleware = (await import("webpack-dev-middleware")).default;
|
const webpackMiddleware = (await import("webpack-dev-middleware")).default;
|
||||||
const productionConfig = (await import("../../webpack.config.js")).default;
|
const productionConfig = (await import("../../webpack.config.js")).default;
|
||||||
|
@ -5,8 +5,7 @@ import attributeService from "../services/attributes.js";
|
|||||||
import config from "../services/config.js";
|
import config from "../services/config.js";
|
||||||
import optionService from "../services/options.js";
|
import optionService from "../services/options.js";
|
||||||
import log from "../services/log.js";
|
import log from "../services/log.js";
|
||||||
import env from "../services/env.js";
|
import { isDev, isElectron } from "../services/utils.js";
|
||||||
import { isElectron } from "../services/utils.js";
|
|
||||||
import protectedSessionService from "../services/protected_session.js";
|
import protectedSessionService from "../services/protected_session.js";
|
||||||
import packageJson from "../../package.json" with { type: "json" };
|
import packageJson from "../../package.json" with { type: "json" };
|
||||||
import assetPath from "../services/asset_path.js";
|
import assetPath from "../services/asset_path.js";
|
||||||
@ -52,7 +51,7 @@ function index(req: Request, res: Response) {
|
|||||||
maxEntityChangeSyncIdAtLoad: sql.getValue("SELECT COALESCE(MAX(id), 0) FROM entity_changes WHERE isSynced = 1"),
|
maxEntityChangeSyncIdAtLoad: sql.getValue("SELECT COALESCE(MAX(id), 0) FROM entity_changes WHERE isSynced = 1"),
|
||||||
instanceName: config.General ? config.General.instanceName : null,
|
instanceName: config.General ? config.General.instanceName : null,
|
||||||
appCssNoteIds: getAppCssNoteIds(),
|
appCssNoteIds: getAppCssNoteIds(),
|
||||||
isDev: env.isDev(),
|
isDev,
|
||||||
isMainWindow: view === "mobile" ? true : !req.query.extraWindow,
|
isMainWindow: view === "mobile" ? true : !req.query.extraWindow,
|
||||||
isProtectedSessionAvailable: protectedSessionService.isProtectedSessionAvailable(),
|
isProtectedSessionAvailable: protectedSessionService.isProtectedSessionAvailable(),
|
||||||
maxContentWidth: Math.max(640, parseInt(options.maxContentWidth)),
|
maxContentWidth: Math.max(640, parseInt(options.maxContentWidth)),
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import assetPath from "./asset_path.js";
|
import assetPath from "./asset_path.js";
|
||||||
import env from "./env.js";
|
import { isDev } from "./utils.js";
|
||||||
|
|
||||||
export default env.isDev() ? assetPath + "/app" : assetPath + "/app-dist";
|
export default isDev ? assetPath + "/app" : assetPath + "/app-dist";
|
@ -8,8 +8,7 @@ import fs from "fs";
|
|||||||
import themeNames from "./code_block_theme_names.json" with { type: "json" };
|
import themeNames from "./code_block_theme_names.json" with { type: "json" };
|
||||||
import { t } from "i18next";
|
import { t } from "i18next";
|
||||||
import { join } from "path";
|
import { join } from "path";
|
||||||
import { isElectron, getResourceDir } from "./utils.js";
|
import { isDev, isElectron, getResourceDir } from "./utils.js";
|
||||||
import env from "./env.js";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a color scheme for the code block syntax highlight.
|
* Represents a color scheme for the code block syntax highlight.
|
||||||
@ -46,7 +45,7 @@ export function listSyntaxHighlightingThemes() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getStylesDirectory() {
|
function getStylesDirectory() {
|
||||||
if (isElectron() && !env.isDev()) {
|
if (isElectron && !isDev) {
|
||||||
return "styles";
|
return "styles";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
function isDev() {
|
|
||||||
return !!(process.env.TRILIUM_ENV && process.env.TRILIUM_ENV === "dev");
|
|
||||||
}
|
|
||||||
|
|
||||||
export default {
|
|
||||||
isDev
|
|
||||||
};
|
|
@ -1,6 +1,5 @@
|
|||||||
import config from "./config.js";
|
import config from "./config.js";
|
||||||
import { isElectron } from "./utils.js";
|
import { isDev, isElectron } from "./utils.js";
|
||||||
import env from "./env.js";
|
|
||||||
import dataDir from "./data_dir.js";
|
import dataDir from "./data_dir.js";
|
||||||
|
|
||||||
function parseAndValidate(portStr: string, source: string) {
|
function parseAndValidate(portStr: string, source: string) {
|
||||||
@ -19,7 +18,7 @@ let port: number;
|
|||||||
if (process.env.TRILIUM_PORT) {
|
if (process.env.TRILIUM_PORT) {
|
||||||
port = parseAndValidate(process.env.TRILIUM_PORT, "environment variable TRILIUM_PORT");
|
port = parseAndValidate(process.env.TRILIUM_PORT, "environment variable TRILIUM_PORT");
|
||||||
} else if (isElectron) {
|
} else if (isElectron) {
|
||||||
port = env.isDev() ? 37740 : 37840;
|
port = isDev ? 37740 : 37840;
|
||||||
} else {
|
} else {
|
||||||
port = parseAndValidate(config["Network"]["port"] || "3000", `Network.port in ${dataDir.CONFIG_INI_PATH}`);
|
port = parseAndValidate(config["Network"]["port"] || "3000", `Network.port in ${dataDir.CONFIG_INI_PATH}`);
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@ import sanitize from "sanitize-filename";
|
|||||||
import mimeTypes from "mime-types";
|
import mimeTypes from "mime-types";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { fileURLToPath } from "url";
|
import { fileURLToPath } from "url";
|
||||||
import env from "./env.js";
|
|
||||||
import { dirname, join } from "path";
|
import { dirname, join } from "path";
|
||||||
|
|
||||||
const randtoken = generator({ source: "crypto" });
|
const randtoken = generator({ source: "crypto" });
|
||||||
@ -19,6 +18,8 @@ export const isWindows = process.platform === "win32";
|
|||||||
|
|
||||||
export const isElectron = !!process.versions["electron"];
|
export const isElectron = !!process.versions["electron"];
|
||||||
|
|
||||||
|
export const isDev = !!(process.env.TRILIUM_ENV && process.env.TRILIUM_ENV === "dev");
|
||||||
|
|
||||||
export function newEntityId() {
|
export function newEntityId() {
|
||||||
return randomString(12);
|
return randomString(12);
|
||||||
}
|
}
|
||||||
@ -316,7 +317,7 @@ export function envToBoolean(val: string | undefined) {
|
|||||||
* @returns the resource dir.
|
* @returns the resource dir.
|
||||||
*/
|
*/
|
||||||
export function getResourceDir() {
|
export function getResourceDir() {
|
||||||
if (isElectron && !env.isDev()) {
|
if (isElectron && !isDev) {
|
||||||
return process.resourcesPath;
|
return process.resourcesPath;
|
||||||
} else {
|
} else {
|
||||||
return join(dirname(fileURLToPath(import.meta.url)), "..", "..");
|
return join(dirname(fileURLToPath(import.meta.url)), "..", "..");
|
||||||
|
@ -2,7 +2,6 @@ import path from "path";
|
|||||||
import url from "url";
|
import url from "url";
|
||||||
import port from "./port.js";
|
import port from "./port.js";
|
||||||
import optionService from "./options.js";
|
import optionService from "./options.js";
|
||||||
import env from "./env.js";
|
|
||||||
import log from "./log.js";
|
import log from "./log.js";
|
||||||
import sqlInit from "./sql_init.js";
|
import sqlInit from "./sql_init.js";
|
||||||
import cls from "./cls.js";
|
import cls from "./cls.js";
|
||||||
@ -10,7 +9,7 @@ import keyboardActionsService from "./keyboard_actions.js";
|
|||||||
import remoteMain from "@electron/remote/main/index.js";
|
import remoteMain from "@electron/remote/main/index.js";
|
||||||
import type { App, BrowserWindow, BrowserWindowConstructorOptions, WebContents } from "electron";
|
import type { App, BrowserWindow, BrowserWindowConstructorOptions, WebContents } from "electron";
|
||||||
import { ipcMain } from "electron";
|
import { ipcMain } from "electron";
|
||||||
import { isMac, isWindows } from "./utils.js";
|
import { isDev, isMac, isWindows } from "./utils.js";
|
||||||
|
|
||||||
import { fileURLToPath } from "url";
|
import { fileURLToPath } from "url";
|
||||||
import { dirname } from "path";
|
import { dirname } from "path";
|
||||||
@ -169,7 +168,7 @@ function configureWebContents(webContents: WebContents, spellcheckEnabled: boole
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getIcon() {
|
function getIcon() {
|
||||||
return path.join(dirname(fileURLToPath(import.meta.url)), "../../images/app-icons/png/256x256" + (env.isDev() ? "-dev" : "") + ".png");
|
return path.join(dirname(fileURLToPath(import.meta.url)), "../../images/app-icons/png/256x256" + (isDev ? "-dev" : "") + ".png");
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createSetupWindow() {
|
async function createSetupWindow() {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { WebSocketServer as WebSocketServer, WebSocket } from "ws";
|
import { WebSocketServer as WebSocketServer, WebSocket } from "ws";
|
||||||
import { isElectron, randomString } from "./utils.js";
|
import { isDev, isElectron, randomString } from "./utils.js";
|
||||||
import log from "./log.js";
|
import log from "./log.js";
|
||||||
import sql from "./sql.js";
|
import sql from "./sql.js";
|
||||||
import cls from "./cls.js";
|
import cls from "./cls.js";
|
||||||
@ -9,11 +9,10 @@ import protectedSessionService from "./protected_session.js";
|
|||||||
import becca from "../becca/becca.js";
|
import becca from "../becca/becca.js";
|
||||||
import AbstractBeccaEntity from "../becca/entities/abstract_becca_entity.js";
|
import AbstractBeccaEntity from "../becca/entities/abstract_becca_entity.js";
|
||||||
|
|
||||||
import env from "./env.js";
|
|
||||||
import type { IncomingMessage, Server as HttpServer } from "http";
|
import type { IncomingMessage, Server as HttpServer } from "http";
|
||||||
import type { EntityChange } from "./entity_changes_interface.js";
|
import type { EntityChange } from "./entity_changes_interface.js";
|
||||||
|
|
||||||
if (env.isDev()) {
|
if (isDev) {
|
||||||
const chokidar = (await import("chokidar")).default;
|
const chokidar = (await import("chokidar")).default;
|
||||||
const debounce = (await import("debounce")).default;
|
const debounce = (await import("debounce")).default;
|
||||||
const debouncedReloadFrontend = debounce(() => reloadFrontend("source code change"), 200);
|
const debouncedReloadFrontend = debounce(() => reloadFrontend("source code change"), 200);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user