[READ-ONLY] a fast, modern browser for the npm registry
at main 41 lines 1.4 kB view raw
1import { defineNuxtModule, useNuxt, addServerTemplate } from 'nuxt/kit' 2import process from 'node:process' 3import { join } from 'node:path' 4import { appendFileSync, existsSync, readFileSync } from 'node:fs' 5import { randomUUID } from 'node:crypto' 6import { getEnv } from '../config/env.ts' 7 8export default defineNuxtModule({ 9 meta: { 10 name: 'oauth', 11 }, 12 async setup() { 13 const nuxt = useNuxt() 14 15 const { previewUrl, productionUrl } = await getEnv(nuxt.options.dev) 16 const clientUri = productionUrl || previewUrl || 'http://127.0.0.1:3000' 17 18 // bake it into a virtual file 19 addServerTemplate({ 20 filename: '#oauth/config', 21 getContents: () => `export const clientUri = ${JSON.stringify(clientUri)};`, 22 }) 23 24 if (nuxt.options._prepare || process.env.NUXT_SESSION_PASSWORD) { 25 return 26 } 27 28 const envPath = join(nuxt.options.rootDir, '.env') 29 const hasPassword = 30 existsSync(envPath) && /^NUXT_SESSION_PASSWORD=/m.test(readFileSync(envPath, 'utf-8')) 31 32 if (!hasPassword) { 33 // eslint-disable-next-line no-console 34 console.info('Generating NUXT_SESSION_PASSWORD for development environment.') 35 const password = randomUUID().replace(/-/g, '') 36 37 nuxt.options.runtimeConfig.sessionPassword = password 38 appendFileSync(envPath, `# generated by dev module\nNUXT_SESSION_PASSWORD=${password}\n`) 39 } 40 }, 41})