Skip to content

Commit b590e6d

Browse files
committed
fix: lint errors
1 parent 53074df commit b590e6d

File tree

2 files changed

+54
-53
lines changed

2 files changed

+54
-53
lines changed

src/cli/utils.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,26 @@ export const loadOptions = async (): Promise<ModuleOptions> => {
5050
// Try to load Nuxt configuration first
5151
console.log('[Nuxt Users] Loading Nuxt project...')
5252
const nuxt = await loadNuxt({ cwd: process.cwd(), ready: false })
53-
53+
5454
// Get both configurations BEFORE module processing
5555
// 1. Top-level nuxtUsers (for module-style config)
5656
// 2. runtimeConfig.nuxtUsers (for runtime-style config - read from the original config)
5757
const topLevelConfig = nuxt.options.nuxtUsers as ModuleOptions
58-
58+
5959
// Read the original runtime config before it gets processed by the module
6060
const originalRuntimeConfig = await (async () => {
6161
try {
6262
// Load the nuxt.config.ts file directly to get the original runtime config
63-
const configPath = await import('path').then(p => p.resolve(process.cwd(), 'nuxt.config.ts'))
63+
const configPath = await import('node:path').then(p => p.resolve(process.cwd(), 'nuxt.config.ts'))
6464
const config = await import(configPath).then(m => m.default)
6565
return config?.runtimeConfig?.nuxtUsers
66-
} catch {
66+
}
67+
catch {
6768
// Fallback to processed config if direct loading fails
6869
return nuxt.options.runtimeConfig?.nuxtUsers
6970
}
7071
})()
71-
72-
72+
7373
await nuxt.close()
7474

7575
// Check if we have any configuration
@@ -78,7 +78,7 @@ export const loadOptions = async (): Promise<ModuleOptions> => {
7878
// Merge configurations with priority: topLevel > runtime > defaults
7979
// Special handling for connector to avoid mixing sqlite and mysql settings
8080
let mergedConfig = defu(topLevelConfig, originalRuntimeConfig, defaultOptions)
81-
81+
8282
// If a specific connector is configured, ensure we don't mix default connector settings
8383
const configuredConnector = topLevelConfig?.connector || originalRuntimeConfig?.connector
8484
if (configuredConnector) {
@@ -87,7 +87,7 @@ export const loadOptions = async (): Promise<ModuleOptions> => {
8787
connector: configuredConnector
8888
}
8989
}
90-
90+
9191
return mergedConfig
9292
}
9393
else {

test/cli/config-loading.test.ts

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
/**
22
* CLI Configuration Loading Tests
3-
*
3+
*
44
* This test suite validates the configuration loading functionality for CLI commands
55
* like 'npx nuxt-users migrate'. It ensures that:
6-
*
6+
*
77
* 1. Environment variables are properly parsed for database configuration
88
* 2. Top-level nuxtUsers configuration is loaded correctly
99
* 3. Runtime config (runtimeConfig.nuxtUsers) is loaded correctly
1010
* 4. Configuration merging works properly with correct precedence
1111
* 5. Connector configurations don't get mixed (regression test for the bug where
1212
* MySQL config was being mixed with SQLite defaults)
13-
*
13+
*
1414
* The tests cover various scenarios including zero-config, partial config,
1515
* malformed config, and mixed configurations.
1616
*/
1717

18-
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
18+
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
1919
import { loadOptions, getOptionsFromEnv } from '../../src/cli/utils'
2020
import { defaultOptions } from '../../src/module'
21-
import { writeFileSync, unlinkSync, existsSync, mkdirSync } from 'fs'
22-
import { join } from 'path'
21+
import { writeFileSync, unlinkSync, existsSync, mkdirSync } from 'node:fs'
22+
import { join } from 'node:path'
23+
import { execSync } from 'node:child_process'
2324

2425
describe('CLI: Configuration Loading', () => {
2526
const testDir = join(process.cwd(), 'test-config-loading')
@@ -28,25 +29,25 @@ describe('CLI: Configuration Loading', () => {
2829
beforeEach(() => {
2930
// Create test directory
3031
mkdirSync(testDir, { recursive: true })
31-
32+
3233
// Change to test directory for CLI tests
3334
process.chdir(testDir)
3435
})
3536

3637
afterEach(() => {
3738
// Change back to original directory
3839
process.chdir(join(testDir, '..'))
39-
40+
4041
// Clean up test files
4142
if (existsSync(configPath)) {
4243
unlinkSync(configPath)
4344
}
4445
if (existsSync(testDir)) {
4546
try {
4647
// Remove directory and contents
47-
const { execSync } = require('child_process')
4848
execSync(`rm -rf "${testDir}"`)
49-
} catch {
49+
}
50+
catch {
5051
// Ignore cleanup errors
5152
}
5253
}
@@ -66,7 +67,7 @@ describe('CLI: Configuration Loading', () => {
6667

6768
it('should return default SQLite configuration when no env vars are set', () => {
6869
const options = getOptionsFromEnv()
69-
70+
7071
expect(options.connector.name).toBe('sqlite')
7172
expect(options.connector.options.path).toBe('./data/users.sqlite3')
7273
expect(options.tables).toEqual(defaultOptions.tables)
@@ -75,9 +76,9 @@ describe('CLI: Configuration Loading', () => {
7576
it('should use custom SQLite path from environment', () => {
7677
process.env.DB_CONNECTOR = 'sqlite'
7778
process.env.DB_PATH = './custom.sqlite3'
78-
79+
7980
const options = getOptionsFromEnv()
80-
81+
8182
expect(options.connector.name).toBe('sqlite')
8283
expect(options.connector.options.path).toBe('./custom.sqlite3')
8384
})
@@ -89,9 +90,9 @@ describe('CLI: Configuration Loading', () => {
8990
process.env.DB_USER = 'testuser'
9091
process.env.DB_PASSWORD = 'testpass'
9192
process.env.DB_NAME = 'testdb'
92-
93+
9394
const options = getOptionsFromEnv()
94-
95+
9596
expect(options.connector.name).toBe('mysql')
9697
expect(options.connector.options).toEqual({
9798
host: 'localhost',
@@ -109,9 +110,9 @@ describe('CLI: Configuration Loading', () => {
109110
process.env.DB_USER = 'postgres'
110111
process.env.DB_PASSWORD = 'pgpass'
111112
process.env.DB_NAME = 'pgdb'
112-
113+
113114
const options = getOptionsFromEnv()
114-
115+
115116
expect(options.connector.name).toBe('postgresql')
116117
expect(options.connector.options).toEqual({
117118
host: 'localhost',
@@ -124,7 +125,7 @@ describe('CLI: Configuration Loading', () => {
124125

125126
it('should throw error for unsupported database connector', () => {
126127
process.env.DB_CONNECTOR = 'unsupported'
127-
128+
128129
expect(() => getOptionsFromEnv()).toThrow('Unsupported database connector: unsupported')
129130
})
130131
})
@@ -134,9 +135,9 @@ describe('CLI: Configuration Loading', () => {
134135
// No nuxt.config.ts file exists
135136
process.env.DB_CONNECTOR = 'sqlite'
136137
process.env.DB_PATH = './env-fallback.sqlite3'
137-
138+
138139
const options = await loadOptions()
139-
140+
140141
expect(options.connector.name).toBe('sqlite')
141142
expect(options.connector.options.path).toBe('./env-fallback.sqlite3')
142143
})
@@ -164,9 +165,9 @@ export default defineNuxtConfig({
164165
})
165166
`
166167
writeFileSync(configPath, configContent)
167-
168+
168169
const options = await loadOptions()
169-
170+
170171
expect(options.connector.name).toBe('mysql')
171172
expect(options.connector.options).toEqual({
172173
host: 'mysql-host',
@@ -203,9 +204,9 @@ export default defineNuxtConfig({
203204
})
204205
`
205206
writeFileSync(configPath, configContent)
206-
207+
207208
const options = await loadOptions()
208-
209+
209210
expect(options.connector.name).toBe('mysql')
210211
expect(options.connector.options).toEqual({
211212
host: 'runtime-mysql-host',
@@ -257,9 +258,9 @@ export default defineNuxtConfig({
257258
})
258259
`
259260
writeFileSync(configPath, configContent)
260-
261+
261262
const options = await loadOptions()
262-
263+
263264
// Top-level config should take priority
264265
expect(options.connector.name).toBe('postgresql')
265266
expect(options.connector.options).toEqual({
@@ -295,9 +296,9 @@ export default defineNuxtConfig({
295296
})
296297
`
297298
writeFileSync(configPath, configContent)
298-
299+
299300
const options = await loadOptions()
300-
301+
301302
// Should have our custom connector
302303
expect(options.connector.name).toBe('mysql')
303304
expect(options.connector.options).toEqual({
@@ -307,7 +308,7 @@ export default defineNuxtConfig({
307308
password: 'test-pass',
308309
database: 'test-db'
309310
})
310-
311+
311312
// Should have defaults for missing config
312313
expect(options.tables).toEqual(defaultOptions.tables)
313314
expect(options.auth.tokenExpiration).toBe(defaultOptions.auth.tokenExpiration)
@@ -337,26 +338,26 @@ export default defineNuxtConfig({
337338
})
338339
`
339340
writeFileSync(configPath, configContent)
340-
341+
341342
// Set environment variables like in the user's scenario
342343
process.env.NUXT_MYSQL_HOST = 'localhost'
343344
process.env.NUXT_MYSQL_USER = 'myuser'
344345
process.env.NUXT_MYSQL_PASSWORD = 'mypass'
345346
process.env.NUXT_MYSQL_DATABASE = 'mydb'
346-
347+
347348
const options = await loadOptions()
348-
349+
349350
// CRITICAL: Should NOT have sqlite defaults mixed in
350351
expect(options.connector.name).toBe('mysql')
351352
expect(options.connector.options.path).toBeUndefined() // SQLite path should not exist
352-
353+
353354
// Should have proper MySQL config
354355
expect(options.connector.options.host).toBe('localhost')
355356
expect(options.connector.options.port).toBe(3306)
356357
expect(options.connector.options.user).toBe('myuser')
357358
expect(options.connector.options.password).toBe('mypass')
358359
expect(options.connector.options.database).toBe('mydb')
359-
360+
360361
// Clean up env vars
361362
delete process.env.NUXT_MYSQL_HOST
362363
delete process.env.NUXT_MYSQL_USER
@@ -376,13 +377,13 @@ export default defineNuxtConfig({
376377
})
377378
`
378379
writeFileSync(configPath, configContent)
379-
380+
380381
// Set environment fallback
381382
process.env.DB_CONNECTOR = 'sqlite'
382383
process.env.DB_PATH = './fallback.sqlite3'
383-
384+
384385
const options = await loadOptions()
385-
386+
386387
// Should fall back to environment variables
387388
expect(options.connector.name).toBe('sqlite')
388389
expect(options.connector.options.path).toBe('./fallback.sqlite3')
@@ -399,10 +400,10 @@ export default defineNuxtConfig({
399400
delete process.env.DB_USER
400401
delete process.env.DB_PASSWORD
401402
delete process.env.DB_NAME
402-
403+
403404
// No config file, no env vars - should use complete defaults
404405
const options = await loadOptions()
405-
406+
406407
expect(options).toEqual(defaultOptions)
407408
})
408409

@@ -438,24 +439,24 @@ export default defineNuxtConfig({
438439
})
439440
`
440441
writeFileSync(configPath, configContent)
441-
442+
442443
const options = await loadOptions()
443-
444+
444445
// Should get connector from top-level
445446
expect(options.connector.name).toBe('postgresql')
446447
expect(options.connector.options.host).toBe('pg-host')
447-
448+
448449
// Should get auth from runtime config
449450
expect(options.auth.tokenExpiration).toBe(240)
450451
expect(options.auth.whitelist).toEqual(['/public', '/api/health'])
451-
452+
452453
// Should get tables from runtime config
453454
expect(options.tables.users).toBe('custom_users')
454455
expect(options.tables.migrations).toBe('custom_migrations')
455-
456+
456457
// Should get defaults for unspecified values
457458
expect(options.tables.personalAccessTokens).toBe(defaultOptions.tables.personalAccessTokens)
458459
expect(options.apiBasePath).toBe(defaultOptions.apiBasePath)
459460
})
460461
})
461-
})
462+
})

0 commit comments

Comments
 (0)