From 30d14eeab40a01a2d09cec839c266a913b01db3f Mon Sep 17 00:00:00 2001 From: Forest Hoffman Date: Sat, 9 Mar 2019 11:11:30 -0600 Subject: [PATCH] Adhere to XDG base directory spec for dataDir and logDir (#156) --- packages/server/src/cli.ts | 10 +++++++--- packages/server/src/constants.ts | 6 ++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/server/src/cli.ts b/packages/server/src/cli.ts index a4352d82..5fb25172 100644 --- a/packages/server/src/cli.ts +++ b/packages/server/src/cli.ts @@ -12,7 +12,7 @@ import { requireModule, requireFork, forkModule } from "./vscode/bootstrapFork"; import { SharedProcess, SharedProcessState } from "./vscode/sharedProcess"; import { setup as setupNativeModules } from "./modules"; import { fillFs } from "./fill"; -import { isCli, serveStatic, buildDir } from "./constants"; +import { isCli, serveStatic, buildDir, dataHome, cacheHome } from "./constants"; import opn = require("opn"); export class Entry extends Command { @@ -49,7 +49,7 @@ export class Entry extends Command { } const { args, flags } = this.parse(Entry); - const dataDir = path.resolve(flags["data-dir"] || path.join(os.homedir(), ".code-server")); + const dataDir = path.resolve(flags["data-dir"] || path.join(dataHome, "code-server")); const workingDir = path.resolve(args["workdir"]); setupNativeModules(dataDir); @@ -81,7 +81,11 @@ export class Entry extends Command { fs.mkdirSync(dataDir); } - const logDir = path.join(dataDir, "logs", new Date().toISOString().replace(/[-:.TZ]/g, "")); + if (!fs.existsSync(cacheHome)) { + fs.mkdirSync(cacheHome); + } + + const logDir = path.join(cacheHome, "code-server/logs", new Date().toISOString().replace(/[-:.TZ]/g, "")); process.env.VSCODE_LOGS = logDir; const certPath = flags.cert ? path.resolve(flags.cert) : undefined; diff --git a/packages/server/src/constants.ts b/packages/server/src/constants.ts index d603a603..da761801 100644 --- a/packages/server/src/constants.ts +++ b/packages/server/src/constants.ts @@ -1,5 +1,11 @@ import * as path from "path"; +import * as os from "os"; export const isCli = typeof process.env.CLI !== "undefined" && process.env.CLI !== "false"; export const serveStatic = typeof process.env.SERVE_STATIC !== "undefined" && process.env.SERVE_STATIC !== "false"; export const buildDir = process.env.BUILD_DIR ? path.resolve(process.env.BUILD_DIR) : ""; +const xdgResolve = (primary: string | undefined, fallback: string): string => { + return primary ? path.resolve(primary) : path.resolve(process.env.HOME || os.homedir(), fallback); +}; +export const dataHome = xdgResolve(process.env.XDG_DATA_HOME, ".local/share"); +export const cacheHome = xdgResolve(process.env.XDG_CACHE_HOME, ".cache");