refactor: make password param to defaultConfigFile

This commit is contained in:
Joe Previte 2021-09-28 15:51:00 -07:00
parent 77c1150b8d
commit 6c95f72d2b
No known key found for this signature in database
GPG Key ID: 2C91590C6B742C24
2 changed files with 14 additions and 15 deletions

View File

@ -505,17 +505,18 @@ export async function setDefaults(cliArgs: Args, configArgs?: ConfigArgs): Promi
/** /**
* Helper function to return the default config file. * Helper function to return the default config file.
* *
* @param {string} password - Password passed in (usually from generatePassword())
* @returns The default config file: * @returns The default config file:
* *
* - bind-addr: 127.0.0.1:8080 * - bind-addr: 127.0.0.1:8080
* - auth: password * - auth: password
* - password: <generated-password> * - password: <password>
* - cert: false * - cert: false
*/ */
export async function defaultConfigFile(): Promise<string> { export function defaultConfigFile(password: string): string {
return `bind-addr: 127.0.0.1:8080 return `bind-addr: 127.0.0.1:8080
auth: password auth: password
password: ${await generatePassword()} password: ${password}
cert: false cert: false
` `
} }
@ -540,7 +541,8 @@ export async function readConfigFile(configPath?: string): Promise<ConfigArgs> {
await fs.mkdir(path.dirname(configPath), { recursive: true }) await fs.mkdir(path.dirname(configPath), { recursive: true })
try { try {
await fs.writeFile(configPath, await defaultConfigFile(), { const generatedPassword = await generatePassword()
await fs.writeFile(configPath, defaultConfigFile(generatedPassword), {
flag: "wx", // wx means to fail if the path exists. flag: "wx", // wx means to fail if the path exists.
}) })
logger.info(`Wrote default config file to ${humanPath(configPath)}`) logger.info(`Wrote default config file to ${humanPath(configPath)}`)

View File

@ -14,7 +14,7 @@ import {
splitOnFirstEquals, splitOnFirstEquals,
} from "../../../src/node/cli" } from "../../../src/node/cli"
import { tmpdir } from "../../../src/node/constants" import { tmpdir } from "../../../src/node/constants"
import { paths } from "../../../src/node/util" import { generatePassword, paths } from "../../../src/node/util"
import { useEnv } from "../../utils/helpers" import { useEnv } from "../../utils/helpers"
type Mutable<T> = { type Mutable<T> = {
@ -645,16 +645,13 @@ describe("bindAddrFromArgs", () => {
}) })
describe("defaultConfigFile", () => { describe("defaultConfigFile", () => {
it("should return the dfeault config file as a string", async () => { it("should return the default config file as a string", async () => {
const actualDefaultConfigFile = await defaultConfigFile() const password = await generatePassword()
// Since the password is autogenerated within the function const actual = defaultConfigFile(password)
// we can't assert it with .toMatch
// but we can check that the config at least includes
// these strings.
const expectedStrings = [`bind-addr: 127.0.0.1:8080`, `auth: password`, `password`, `cert: false`]
expectedStrings.forEach((str) => { expect(actual).toMatch(`bind-addr: 127.0.0.1:8080
expect(actualDefaultConfigFile).toContain(str) auth: password
}) password: ${password}
cert: false`)
}) })
}) })