Skip to content

Commit cfab36b

Browse files
committed
refactor: make password param to defaultConfigFile
1 parent 77c1150 commit cfab36b

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

src/node/cli.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -505,17 +505,18 @@ export async function setDefaults(cliArgs: Args, configArgs?: ConfigArgs): Promi
505505
/**
506506
* Helper function to return the default config file.
507507
*
508+
* @param {string} password - Password passed in (usually from generatePassword())
508509
* @returns The default config file:
509510
*
510511
* - bind-addr: 127.0.0.1:8080
511512
* - auth: password
512-
* - password: <generated-password>
513+
* - password: <password>
513514
* - cert: false
514515
*/
515-
export async function defaultConfigFile(): Promise<string> {
516+
export function defaultConfigFile(password: string): string {
516517
return `bind-addr: 127.0.0.1:8080
517518
auth: password
518-
password: ${await generatePassword()}
519+
password: ${password}
519520
cert: false
520521
`
521522
}
@@ -540,7 +541,8 @@ export async function readConfigFile(configPath?: string): Promise<ConfigArgs> {
540541
await fs.mkdir(path.dirname(configPath), { recursive: true })
541542

542543
try {
543-
await fs.writeFile(configPath, await defaultConfigFile(), {
544+
const generatedPassword = await generatePassword()
545+
await fs.writeFile(configPath, defaultConfigFile(generatedPassword), {
544546
flag: "wx", // wx means to fail if the path exists.
545547
})
546548
logger.info(`Wrote default config file to ${humanPath(configPath)}`)

test/unit/node/cli.test.ts

+8-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
splitOnFirstEquals,
1515
} from "../../../src/node/cli"
1616
import { tmpdir } from "../../../src/node/constants"
17-
import { paths } from "../../../src/node/util"
17+
import { generatePassword, paths } from "../../../src/node/util"
1818
import { useEnv } from "../../utils/helpers"
1919

2020
type Mutable<T> = {
@@ -646,15 +646,12 @@ describe("bindAddrFromArgs", () => {
646646

647647
describe("defaultConfigFile", () => {
648648
it("should return the dfeault config file as a string", async () => {
649-
const actualDefaultConfigFile = await defaultConfigFile()
650-
// Since the password is autogenerated within the function
651-
// we can't assert it with .toMatch
652-
// but we can check that the config at least includes
653-
// these strings.
654-
const expectedStrings = [`bind-addr: 127.0.0.1:8080`, `auth: password`, `password`, `cert: false`]
655-
656-
expectedStrings.forEach((str) => {
657-
expect(actualDefaultConfigFile).toContain(str)
658-
})
649+
const password = await generatePassword()
650+
const actual = defaultConfigFile(password)
651+
652+
expect(actual).toMatch(`bind-addr: 127.0.0.1:8080
653+
auth: password
654+
password: ${password}
655+
cert: false`)
659656
})
660657
})

0 commit comments

Comments
 (0)