Skip to content

Commit 5c32807

Browse files
author
Joe
committed
refactor: make humanPath pure and pass in homedir
1 parent 8d1e5d4 commit 5c32807

File tree

5 files changed

+18
-12
lines changed

5 files changed

+18
-12
lines changed

src/node/cli.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ export async function readConfigFile(configPath?: string): Promise<ConfigArgs> {
606606
await fs.writeFile(configPath, defaultConfigFile(generatedPassword), {
607607
flag: "wx", // wx means to fail if the path exists.
608608
})
609-
logger.info(`Wrote default config file to ${humanPath(configPath)}`)
609+
logger.info(`Wrote default config file to ${humanPath(os.homedir(), configPath)}`)
610610
} catch (error: any) {
611611
// EEXIST is fine; we don't want to overwrite existing configurations.
612612
if (error.code !== "EEXIST") {

src/node/main.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { field, logger } from "@coder/logger"
2+
import * as os from "os"
23
import http from "http"
34
import path from "path"
45
import { Disposable } from "../common/emitter"
@@ -95,8 +96,8 @@ export const runCodeServer = async (
9596
): Promise<{ dispose: Disposable["dispose"]; server: http.Server }> => {
9697
logger.info(`code-server ${version} ${commit}`)
9798

98-
logger.info(`Using user-data-dir ${humanPath(args["user-data-dir"])}`)
99-
logger.trace(`Using extensions-dir ${humanPath(args["extensions-dir"])}`)
99+
logger.info(`Using user-data-dir ${humanPath(os.homedir(), args["user-data-dir"])}`)
100+
logger.trace(`Using extensions-dir ${humanPath(os.homedir(), args["extensions-dir"])}`)
100101

101102
if (args.auth === AuthType.Password && !args.password && !args["hashed-password"]) {
102103
throw new Error(
@@ -109,7 +110,7 @@ export const runCodeServer = async (
109110
const serverAddress = ensureAddress(app.server, protocol)
110111
const disposeRoutes = await register(app, args)
111112

112-
logger.info(`Using config file ${humanPath(args.config)}`)
113+
logger.info(`Using config file ${humanPath(os.homedir(), args.config)}`)
113114
logger.info(
114115
`${protocol.toUpperCase()} server listening on ${serverAddress.toString()} ${
115116
args.link ? "(randomized by --link)" : ""
@@ -123,14 +124,14 @@ export const runCodeServer = async (
123124
} else if (args.usingEnvHashedPassword) {
124125
logger.info(" - Using password from $HASHED_PASSWORD")
125126
} else {
126-
logger.info(` - Using password from ${humanPath(args.config)}`)
127+
logger.info(` - Using password from ${humanPath(os.homedir(), args.config)}`)
127128
}
128129
} else {
129130
logger.info(` - Authentication is disabled ${args.link ? "(disabled by --link)" : ""}`)
130131
}
131132

132133
if (args.cert) {
133-
logger.info(` - Using certificate for HTTPS: ${humanPath(args.cert.value)}`)
134+
logger.info(` - Using certificate for HTTPS: ${humanPath(os.homedir(), args.cert.value)}`)
134135
} else {
135136
logger.info(` - Not serving HTTPS ${args.link ? "(disabled by --link)" : ""}`)
136137
}

src/node/routes/login.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Router, Request } from "express"
22
import { promises as fs } from "fs"
33
import { RateLimiter as Limiter } from "limiter"
4+
import * as os from "os"
45
import * as path from "path"
56
import { rootPath } from "../constants"
67
import { authenticated, getCookieDomain, redirect, replaceTemplates } from "../http"
@@ -30,7 +31,7 @@ export class RateLimiter {
3031

3132
const getRoot = async (req: Request, error?: Error): Promise<string> => {
3233
const content = await fs.readFile(path.join(rootPath, "src/browser/pages/login.html"), "utf8")
33-
let passwordMsg = `Check the config file at ${humanPath(req.args.config)} for the password.`
34+
let passwordMsg = `Check the config file at ${humanPath(os.homedir(), req.args.config)} for the password.`
3435
if (req.args.usingEnvPassword) {
3536
passwordMsg = "Password was set from $PASSWORD."
3637
} else if (req.args.usingEnvHashedPassword) {

src/node/util.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,14 @@ export function getEnvPaths(): Paths {
9191
* humanPath replaces the home directory in path with ~.
9292
* Makes it more readable.
9393
*
94+
* @param homedir - the home directory(i.e. `os.homedir()`)
9495
* @param path - a file path
9596
*/
96-
export function humanPath(path?: string): string {
97+
export function humanPath(homedir: string, path?: string): string {
9798
if (!path) {
9899
return ""
99100
}
100-
return path.replace(os.homedir(), "~")
101+
return path.replace(homedir, "~")
101102
}
102103

103104
export const generateCertificate = async (hostname: string): Promise<{ cert: string; certKey: string }> => {

test/unit/node/util.test.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -479,13 +479,16 @@ describe("isFile", () => {
479479

480480
describe("humanPath", () => {
481481
it("should return an empty string if no path provided", () => {
482-
const actual = util.humanPath()
482+
const mockHomedir = "/home/coder"
483+
const actual = util.humanPath(mockHomedir)
483484
const expected = ""
484485
expect(actual).toBe(expected)
485486
})
486487
it("should replace the homedir with ~", () => {
487-
const actual = util.humanPath(`/home/coder/code-server`)
488+
const mockHomedir = "/home/coder"
489+
const path = `${mockHomedir}/code-server`
490+
const actual = util.humanPath(mockHomedir, path)
488491
const expected = "~/code-server"
489492
expect(actual).toBe(expected)
490493
})
491-
})
494+
})

0 commit comments

Comments
 (0)