refactor(desktop): use electron-log in shell-env and simplify env merging (#26284)
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import { MainLogger } from "electron-log"
|
||||||
import log from "electron-log/main.js"
|
import log from "electron-log/main.js"
|
||||||
import { readFileSync, readdirSync, statSync, unlinkSync } from "node:fs"
|
import { readFileSync, readdirSync, statSync, unlinkSync } from "node:fs"
|
||||||
import { dirname, join } from "node:path"
|
import { dirname, join } from "node:path"
|
||||||
@@ -5,11 +6,14 @@ import { dirname, join } from "node:path"
|
|||||||
const MAX_LOG_AGE_DAYS = 7
|
const MAX_LOG_AGE_DAYS = 7
|
||||||
const TAIL_LINES = 1000
|
const TAIL_LINES = 1000
|
||||||
|
|
||||||
|
let logger: MainLogger
|
||||||
|
export const getLogger = () => logger
|
||||||
|
|
||||||
export function initLogging() {
|
export function initLogging() {
|
||||||
log.transports.file.maxSize = 5 * 1024 * 1024
|
log.transports.file.maxSize = 5 * 1024 * 1024
|
||||||
initConsoleTransport()
|
initConsoleTransport()
|
||||||
cleanup()
|
cleanup()
|
||||||
return log
|
return (logger = log)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function tail(): string {
|
export function tail(): string {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { fileURLToPath } from "node:url"
|
|||||||
import { app, utilityProcess } from "electron"
|
import { app, utilityProcess } from "electron"
|
||||||
import type { Details } from "electron"
|
import type { Details } from "electron"
|
||||||
import { DEFAULT_SERVER_URL_KEY, WSL_ENABLED_KEY } from "./constants"
|
import { DEFAULT_SERVER_URL_KEY, WSL_ENABLED_KEY } from "./constants"
|
||||||
import { getUserShell, loadShellEnv, mergeShellEnv } from "./shell-env"
|
import { getUserShell, loadShellEnv } from "./shell-env"
|
||||||
import { getStore } from "./store"
|
import { getStore } from "./store"
|
||||||
import type { SqliteMigrationProgress } from "../preload/types"
|
import type { SqliteMigrationProgress } from "../preload/types"
|
||||||
|
|
||||||
@@ -57,16 +57,13 @@ export function setWslConfig(config: WslConfig) {
|
|||||||
|
|
||||||
export function preferAppEnv(userDataPath: string) {
|
export function preferAppEnv(userDataPath: string) {
|
||||||
const shell = process.platform === "win32" ? null : getUserShell()
|
const shell = process.platform === "win32" ? null : getUserShell()
|
||||||
Object.assign(
|
Object.assign(process.env, {
|
||||||
process.env,
|
...(shell ? loadShellEnv(shell) : null),
|
||||||
mergeShellEnv(shell ? loadShellEnv(shell) : null, {
|
OPENCODE_EXPERIMENTAL_ICON_DISCOVERY: "true",
|
||||||
...process.env,
|
OPENCODE_EXPERIMENTAL_FILEWATCHER: "true",
|
||||||
OPENCODE_EXPERIMENTAL_ICON_DISCOVERY: "true",
|
OPENCODE_CLIENT: "desktop",
|
||||||
OPENCODE_EXPERIMENTAL_FILEWATCHER: "true",
|
XDG_STATE_HOME: process.env.XDG_STATE_HOME ?? userDataPath,
|
||||||
OPENCODE_CLIENT: "desktop",
|
})
|
||||||
XDG_STATE_HOME: process.env.XDG_STATE_HOME ?? userDataPath,
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function spawnLocalServer(
|
export async function spawnLocalServer(
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { spawnSync } from "node:child_process"
|
import { spawnSync } from "node:child_process"
|
||||||
import { basename } from "node:path"
|
import { basename } from "node:path"
|
||||||
|
import { getLogger } from "./logging";
|
||||||
|
|
||||||
const TIMEOUT = 5_000
|
const TIMEOUT = 5_000
|
||||||
|
|
||||||
@@ -55,28 +56,29 @@ export function isNushell(shell: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function loadShellEnv(shell: string) {
|
export function loadShellEnv(shell: string) {
|
||||||
|
const logger = getLogger()
|
||||||
if (isNushell(shell)) {
|
if (isNushell(shell)) {
|
||||||
console.log(`[server] Skipping shell env probe for nushell: ${shell}`)
|
logger.log(`[server] Skipping shell env probe for nushell: ${shell}`)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
const interactive = probe(shell, "-il")
|
const interactive = probe(shell, "-il")
|
||||||
if (interactive.type === "Loaded") {
|
if (interactive.type === "Loaded") {
|
||||||
console.log(`[server] Loaded shell environment with -il (${Object.keys(interactive.value).length} vars)`)
|
logger.log(`[server] Loaded shell environment with -il (${Object.keys(interactive.value).length} vars)`)
|
||||||
return interactive.value
|
return interactive.value
|
||||||
}
|
}
|
||||||
if (interactive.type === "Timeout") {
|
if (interactive.type === "Timeout") {
|
||||||
console.warn(`[server] Interactive shell env probe timed out: ${shell}`)
|
logger.log(`[server] Interactive shell env probe timed out: ${shell}`)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
const login = probe(shell, "-l")
|
const login = probe(shell, "-l")
|
||||||
if (login.type === "Loaded") {
|
if (login.type === "Loaded") {
|
||||||
console.log(`[server] Loaded shell environment with -l (${Object.keys(login.value).length} vars)`)
|
logger.log(`[server] Loaded shell environment with -l (${Object.keys(login.value).length} vars)`)
|
||||||
return login.value
|
return login.value
|
||||||
}
|
}
|
||||||
|
|
||||||
console.warn(`[server] Falling back to app environment: ${shell}`)
|
logger.log(`[server] Falling back to app environment: ${shell}`)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user