Skip to content

Commit fdadacb

Browse files
committed
chore: adjust sudo check
1 parent 9dec0c9 commit fdadacb

File tree

1 file changed

+43
-56
lines changed

1 file changed

+43
-56
lines changed

src/index.ts

+43-56
Original file line numberDiff line numberDiff line change
@@ -74,47 +74,6 @@ export function VitePluginLocal(options: VitePluginLocalOptions): Plugin {
7474
let cleanupPromise: Promise<void> | null = null
7575
let server: ViteDevServer | undefined
7676

77-
// sudo check in plugin initialization
78-
let sudoPromise: Promise<void> | null = null
79-
80-
if (enabled) {
81-
sudoPromise = (async () => {
82-
const config = buildConfig(options)
83-
const domain = config.to
84-
85-
const needsSudo = await needsSudoAccess(options, domain)
86-
if (needsSudo) {
87-
hasSudoAccess = await checkInitialSudo()
88-
89-
if (!hasSudoAccess) {
90-
// Temporarily disable console.log to prevent VitePress output
91-
const origLog = console.log
92-
console.log = () => { }
93-
94-
process.stdout.write('\nSudo access required for proxy setup.\n')
95-
96-
hasSudoAccess = await new Promise<boolean>((resolve) => {
97-
const sudo = spawn('sudo', ['true'], {
98-
stdio: 'inherit',
99-
})
100-
101-
sudo.on('exit', (code) => {
102-
resolve(code === 0)
103-
})
104-
})
105-
106-
// Restore console.log
107-
console.log = origLog
108-
109-
if (!hasSudoAccess) {
110-
console.error('Failed to get sudo access. Please try again.')
111-
process.exit(1)
112-
}
113-
}
114-
}
115-
})()
116-
}
117-
11877
const debug = (...args: any[]) => {
11978
if (verbose)
12079
originalConsole.log('[vite-plugin-local]', ...args)
@@ -130,7 +89,6 @@ export function VitePluginLocal(options: VitePluginLocalOptions): Plugin {
13089
debug('Starting cleanup process')
13190

13291
try {
133-
// Store the cleanup promise
13492
cleanupPromise = cleanup({
13593
domains,
13694
hosts: typeof cleanupOpts === 'boolean' ? cleanupOpts : cleanupOpts?.hosts,
@@ -144,7 +102,7 @@ export function VitePluginLocal(options: VitePluginLocalOptions): Plugin {
144102
}
145103
catch (error) {
146104
console.error('Error during cleanup:', error)
147-
throw error // Re-throw to ensure process exits with error
105+
throw error
148106
}
149107
finally {
150108
isCleaningUp = false
@@ -164,35 +122,64 @@ export function VitePluginLocal(options: VitePluginLocalOptions): Plugin {
164122
process.exit(1)
165123
}
166124

167-
if (server?.httpServer) {
125+
if (server?.httpServer)
168126
server.httpServer.close()
169-
}
170127

171-
// Only exit if we're handling a signal
172-
if (signal !== 'CLOSE') {
128+
if (signal !== 'CLOSE')
173129
process.exit(0)
174-
}
175130
}
176131

177132
return {
178133
name: 'vite-plugin-local',
179134
enforce: 'pre',
135+
apply: 'serve',
136+
137+
configResolved(resolvedConfig) {
138+
// Early exit if we're in build mode
139+
if (resolvedConfig.command === 'build')
140+
return
141+
},
180142

181143
async configureServer(viteServer: ViteDevServer) {
182144
if (!enabled)
183145
return
184146

185-
// Wait for sudo access before continuing
186-
if (sudoPromise) {
187-
await sudoPromise
188-
}
189-
190147
server = viteServer
191-
192-
// Store original console for debug
193148
originalConsole = { ...console }
194149

195-
// Add cleanup handlers for the server
150+
// Move sudo check here
151+
const config = buildConfig(options)
152+
const domain = config.to
153+
154+
const needsSudo = await needsSudoAccess(options, domain)
155+
if (needsSudo) {
156+
hasSudoAccess = await checkInitialSudo()
157+
158+
if (!hasSudoAccess) {
159+
const origLog = console.log
160+
console.log = () => { }
161+
162+
process.stdout.write('\nSudo access required for proxy setup.\n')
163+
164+
hasSudoAccess = await new Promise<boolean>((resolve) => {
165+
const sudo = spawn('sudo', ['true'], {
166+
stdio: 'inherit',
167+
})
168+
169+
sudo.on('exit', (code) => {
170+
resolve(code === 0)
171+
})
172+
})
173+
174+
console.log = origLog
175+
176+
if (!hasSudoAccess) {
177+
console.error('Failed to get sudo access. Please try again.')
178+
process.exit(1)
179+
}
180+
}
181+
}
182+
196183
server.httpServer?.on('close', () => {
197184
debug('Server closing, cleaning up...')
198185
handleSignal('CLOSE')

0 commit comments

Comments
 (0)