Skip to content

Commit

Permalink
feat(devtools): support for xdg-home-config (#526)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
paro-paro and antfu committed Nov 27, 2023
1 parent 68b8cfc commit 4abd280
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/devtools/src/dev-auth.ts
@@ -1,16 +1,16 @@
import { homedir } from 'node:os'
import { join } from 'node:path'
import { existsSync } from 'node:fs'
import fs from 'node:fs/promises'
import { randomStr } from '@antfu/utils'
import { getHomeDir } from './utils/local-options'

let token: string | undefined

export async function getDevAuthToken() {
if (token)
return token

const home = homedir()
const home = getHomeDir()
const dir = join(home, '.nuxt/devtools')
const filepath = join(dir, 'dev-auth-token.txt')

Expand Down
10 changes: 9 additions & 1 deletion packages/devtools/src/utils/local-options.ts
Expand Up @@ -10,6 +10,10 @@ interface LocalOptionSearchOptions {
key?: string | boolean
}

export function getHomeDir() {
return process.env.XDG_CONFIG_HOME || homedir()
}

export async function readLocalOptions<T>(defaults: T, options: LocalOptionSearchOptions): Promise<T> {
const { filePath } = getOptionsFilepath(options)

Expand All @@ -34,11 +38,15 @@ export async function readLocalOptions<T>(defaults: T, options: LocalOptionSearc

function getOptionsFilepath(options: LocalOptionSearchOptions) {
let hashedKey

if (options.key)
hashedKey = hash(`${options.root}:${options.key}`)
else
hashedKey = hash(options.root)
const filePath = join(homedir(), '.nuxt/devtools', `${hashedKey}.json`)

const home = getHomeDir()
const filePath = join(home, '.nuxt/devtools', `${hashedKey}.json`)

return {
filePath,
hashedKey,
Expand Down

0 comments on commit 4abd280

Please sign in to comment.