Skip to content

Commit febdb50

Browse files
committed
feat(module): improve credentials resolution; allow running from outside of cwd without errors
1 parent 2448fd5 commit febdb50

1 file changed

Lines changed: 54 additions & 40 deletions

File tree

src/module.ts

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { existsSync } from 'node:fs'
2-
import { addComponentsDir, addImportsDir, addPlugin, addServerHandler, addServerImports, addServerPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
2+
import { addComponentsDir, addImportsDir, addPlugin, addServerHandler, addServerImports, addServerImportsDir, addServerPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
33
import { createConsola } from 'consola'
44
import { join } from 'pathe'
55
import chalk from 'chalk'
6-
import prompts from 'prompts'
6+
import { prompt } from 'prompts'
77
import * as execa from 'execa'
88

99
const logger = createConsola({
@@ -78,6 +78,31 @@ export default defineNuxtModule<ModuleOptions>({
7878
const hasConfigFile = () => existsSync(edgeDbConfigPath)
7979
const canPrompt = nuxt.options.dev
8080

81+
async function piped$(
82+
command: string,
83+
args: string[],
84+
quiet: boolean = false,
85+
cwd: string = resolveProject(),
86+
) {
87+
try {
88+
if (quiet) {
89+
return execa.execa(command, args, { cwd })
90+
}
91+
else {
92+
const commandProcess = execa.execa(command, args, { cleanup: true, cwd })
93+
94+
commandProcess?.stdout?.pipe(process.stdout)
95+
96+
commandProcess.stdin?.pipe(process.stdin)
97+
98+
return commandProcess
99+
}
100+
}
101+
catch (e) {
102+
logger.error(e)
103+
}
104+
}
105+
81106
async function generateInterfaces(
82107
quiet: boolean = options.generateQuiet,
83108
force: boolean = false,
@@ -144,7 +169,7 @@ export default defineNuxtModule<ModuleOptions>({
144169
*/
145170
let edgedbCliVersion
146171
try {
147-
edgedbCliVersion = await execa.execa(`edgedb`, [`--version`]).then(result => result.stdout.replace('EdgeDB CLI ', ''))
172+
edgedbCliVersion = await execa.execa(`edgedb`, [`--version`], { cwd: resolveProject() }).then(result => result.stdout.replace('EdgeDB CLI ', ''))
148173
}
149174
catch (e) {
150175
//
@@ -154,7 +179,7 @@ export default defineNuxtModule<ModuleOptions>({
154179
error(`Could not find ${edgeColor('EdgeDB')} CLI.`, true)
155180
if (activePrompts.installCliPrompt)
156181
return
157-
activePrompts.installCliPrompt = prompts(
182+
activePrompts.installCliPrompt = prompt(
158183
{
159184
type: 'confirm',
160185
name: 'value',
@@ -173,8 +198,8 @@ export default defineNuxtModule<ModuleOptions>({
173198
const response = await activePrompts.installCliPrompt
174199
if (response?.value === true) {
175200
try {
176-
await execa.execaCommand(`curl --proto '=https' --tlsv1.2 -sSf https://sh.edgedb.com | sh`)
177-
edgedbCliVersion = await execa.execa(`edgedb`, ['--version']).then(result => result?.stdout?.replace('EdgeDB CLI ', ''))
201+
await execa.execaCommand(`curl --proto '=https' --tlsv1.2 -sSf https://sh.edgedb.com | sh`, { cwd: resolveProject() })
202+
edgedbCliVersion = await execa.execa(`edgedb`, ['--version'], { cwd: resolveProject() }).then(result => result?.stdout?.replace('EdgeDB CLI ', ''))
178203
success(`EdgeDB CLI version ${edgedbCliVersion} installed.`, true)
179204
}
180205
catch (e) {
@@ -194,7 +219,7 @@ export default defineNuxtModule<ModuleOptions>({
194219
logger.log(` ${chalk.red('➜')} Could not find ${edgeColor('EdgeDB')} configuration file.`, true)
195220
if (activePrompts.initPrompt)
196221
return
197-
activePrompts.initPrompt = prompts(
222+
activePrompts.initPrompt = prompt(
198223
{
199224
type: 'confirm',
200225
name: 'value',
@@ -232,22 +257,23 @@ export default defineNuxtModule<ModuleOptions>({
232257
let uiUrl: any | undefined
233258
if (!process.env.NUXT_EDGEDB_UI_URL && options.injectDbCredentials) {
234259
try {
235-
uiUrl = await execa.execa(`edgedb`, ['ui', '--print-url'])
260+
uiUrl = await execa.execa(`edgedb`, ['ui', '--print-url'], { cwd: resolveProject() })
236261
}
237262
catch (e) {
238263
//
239264
}
240265
}
241266

242267
if (process.env?.NUXT_EDGEDB_UI_URL || uiUrl?.stdout) {
268+
// @ts-expect-error - ?
243269
nuxt.hook('devtools:customTabs', (tabs) => {
244270
tabs.push({
245271
// unique identifier
246272
name: 'nuxt-edgedb-module',
247273
// title to display in the tab
248274
title: 'EdgeDB',
249275
// any icon from Iconify, or a URL to an image
250-
icon: 'logos:edgedb',
276+
icon: 'edgedb-icon',
251277
category: 'app',
252278
// iframe view
253279
view: {
@@ -279,7 +305,7 @@ export default defineNuxtModule<ModuleOptions>({
279305
if (activePrompts.queriesPrompt)
280306
return
281307

282-
activePrompts.queriesPrompt = prompts(
308+
activePrompts.queriesPrompt = prompt(
283309
{
284310
type: 'confirm',
285311
name: 'value',
@@ -318,7 +344,7 @@ export default defineNuxtModule<ModuleOptions>({
318344
if (options.watchPrompt) {
319345
if (activePrompts.migrationPrompt)
320346
return
321-
activePrompts.migrationPrompt = prompts(
347+
activePrompts.migrationPrompt = prompt(
322348
{
323349
type: 'confirm',
324350
name: 'value',
@@ -364,7 +390,7 @@ export default defineNuxtModule<ModuleOptions>({
364390
if (options.watchPrompt) {
365391
if (activePrompts.schemaPrompt)
366392
return
367-
activePrompts.schemaPrompt = prompts(
393+
activePrompts.schemaPrompt = prompt(
368394
{
369395
type: 'confirm',
370396
name: 'value',
@@ -505,17 +531,24 @@ export default defineNuxtModule<ModuleOptions>({
505531
}
506532

507533
if (options.auth) {
508-
if (!process.env.NUXT_EDGEDB_AUTH_BASE_URL && options.injectDbCredentials) {
534+
if (options.injectDbCredentials) {
509535
// http://localhost:10702/db/edgedb/ext/auth/
510536
let dbCredentials: any | undefined
511537
try {
512-
dbCredentials = await execa.execaCommand(`edgedb instance credentials --json`)
538+
dbCredentials = await execa.execaCommand(`edgedb instance credentials --json`, { cwd: resolveProject() })
513539
}
514540
catch (e) {
515541
//
516542
}
517543
if (dbCredentials) {
518-
const { host, port, database } = JSON.parse(dbCredentials.stdout)
544+
const { host, port, database, user, password, tls_ca, tls_security } = JSON.parse(dbCredentials.stdout)
545+
process.env.NUXT_EDGEDB_HOST = host
546+
process.env.NUXT_EDGEDB_PORT = port
547+
process.env.NUXT_EDGEDB_DATABASE = database
548+
process.env.NUXT_EDGEDB_USER = user
549+
process.env.NUXT_EDGEDB_PASS = password
550+
process.env.NUXT_EDGEDB_TLS_CA = tls_ca
551+
process.env.NUXT_EDGEDB_TLS_SECURITY = tls_security
519552
process.env.NUXT_EDGEDB_AUTH_BASE_URL = `http://${host}:${port}/db/${database}/ext/auth/`
520553
}
521554
}
@@ -532,6 +565,12 @@ export default defineNuxtModule<ModuleOptions>({
532565
addImportsDir(resolveLocal('./runtime/composables'))
533566

534567
// Server
568+
addServerImports([
569+
{
570+
from: resolveLocal('./runtime/server/composables/useEdgeDbIdentity'),
571+
name: 'useEdgeDbIdentity',
572+
},
573+
])
535574
addServerHandler({
536575
handler: resolveLocal('./runtime/api/auth/login'),
537576
route: '/api/auth/login',
@@ -605,28 +644,3 @@ function useLogger() {
605644
},
606645
)
607646
}
608-
609-
async function piped$(
610-
command: string,
611-
args: string[],
612-
quiet: boolean = false,
613-
// execPath = process.cwd(),
614-
) {
615-
try {
616-
if (quiet) {
617-
return execa.execa(command, args)
618-
}
619-
else {
620-
const commandProcess = execa.execa(command, args, { cleanup: true })
621-
622-
commandProcess?.stdout?.pipe(process.stdout)
623-
624-
commandProcess.stdin?.pipe(process.stdin)
625-
626-
return commandProcess
627-
}
628-
}
629-
catch (e) {
630-
logger.error(e)
631-
}
632-
}

0 commit comments

Comments
 (0)