From 1606c81f76854c10b6f984b300a903f3c467f321 Mon Sep 17 00:00:00 2001 From: Jason Ho Date: Wed, 11 Jun 2025 20:34:13 -0700 Subject: [PATCH 1/3] allow configuring whether to choose herd or valet --- src/index.ts | 56 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/src/index.ts b/src/index.ts index ee45b92..125ec3a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -132,7 +132,7 @@ function resolveLaravelPlugin(pluginConfig: Required): LaravelPlug const env = loadEnv(mode, userConfig.envDir || process.cwd(), '') const assetUrl = env.ASSET_URL ?? '' const serverConfig = command === 'serve' - ? (resolveDevelopmentEnvironmentServerConfig(pluginConfig.detectTls) ?? resolveEnvironmentServerConfig(env)) + ? (resolveDevelopmentEnvironmentServerConfig(pluginConfig.detectTls, env.LARAVEL_PREFER_DEVELOPMENT_ENVIRONMENT) ?? resolveEnvironmentServerConfig(env)) : undefined ensureCommandShouldRunInEnvironment(command, env) @@ -524,7 +524,7 @@ function resolveHostFromEnv(env: Record): string|undefined /** * Resolve the Herd or Valet server config for the given host. */ -function resolveDevelopmentEnvironmentServerConfig(host: string|boolean|null): { +function resolveDevelopmentEnvironmentServerConfig(host: string|boolean|null, prefer?: "herd"|"valet"|string): { hmr?: { host: string } host?: string, https?: { cert: string, key: string } @@ -533,7 +533,19 @@ function resolveDevelopmentEnvironmentServerConfig(host: string|boolean|null): { return } - const configPath = determineDevelopmentEnvironmentConfigPath(); + const configsAvailable = determineDevelopmentEnvironmentConfigPath() + const candidateConfigs: string[] = [] + if (configsAvailable.herd.length > 0 && configsAvailable.valet.length > 0) { + if (prefer === 'herd') { + candidateConfigs.push(...configsAvailable.herd) + } else if (prefer === 'valet') { + candidateConfigs.push(...configsAvailable.valet) + } else { + candidateConfigs.push(...configsAvailable.herd, ...configsAvailable.valet) + } + } + + const configPath = candidateConfigs[0] if (typeof configPath === 'undefined' && host === null) { return @@ -574,24 +586,32 @@ function resolveDevelopmentEnvironmentServerConfig(host: string|boolean|null): { } } +function resolveHerdConfigs(): string[] +{ + return [ + herdMacConfigPath(), + herdWindowsConfigPath(), + ].filter(fs.existsSync) +} + +function resolveValetConfigs(): string[] +{ + return [ + valetMacConfigPath(), + valetLinuxConfigPath(), + ].filter(fs.existsSync); +} + /** * Resolve the path to the Herd or Valet configuration directory. */ -function determineDevelopmentEnvironmentConfigPath(): string|undefined { - if (fs.existsSync(herdMacConfigPath())) { - return herdMacConfigPath() - } - - if (fs.existsSync(herdWindowsConfigPath())) { - return herdWindowsConfigPath() - } - - if (fs.existsSync(valetMacConfigPath())) { - return valetMacConfigPath() - } - - if (fs.existsSync(valetLinuxConfigPath())) { - return valetLinuxConfigPath() +function determineDevelopmentEnvironmentConfigPath(): { + herd: string[], + valet: string[] +} { + return { + herd: resolveHerdConfigs(), + valet: resolveValetConfigs(), } } From 716eef3d4477917c33113162defbed3c0eb4ac13 Mon Sep 17 00:00:00 2001 From: Jason Ho Date: Wed, 11 Jun 2025 21:07:17 -0700 Subject: [PATCH 2/3] fix default option --- src/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/index.ts b/src/index.ts index 125ec3a..9661683 100644 --- a/src/index.ts +++ b/src/index.ts @@ -543,6 +543,8 @@ function resolveDevelopmentEnvironmentServerConfig(host: string|boolean|null, pr } else { candidateConfigs.push(...configsAvailable.herd, ...configsAvailable.valet) } + } else { + candidateConfigs.push(...configsAvailable.herd, ...configsAvailable.valet) } const configPath = candidateConfigs[0] From ff0467e2a67837663d293bd7b7e5228d9a41d027 Mon Sep 17 00:00:00 2001 From: Jason Ho Date: Wed, 11 Jun 2025 21:11:37 -0700 Subject: [PATCH 3/3] smarter local ssl resolution --- src/index.ts | 66 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9661683..fd986c4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -521,6 +521,32 @@ function resolveHostFromEnv(env: Record): string|undefined } } +function resolveCertsInEnvironment(host: string|boolean|null, configPath: string): { + key: string, + cert: string, + resolvedHost: string, + configPath: string +}|undefined +{ + const resolvedHost = host === true || host === null + ? path.basename(process.cwd()) + '.' + resolveDevelopmentEnvironmentTld(configPath) + : host as string + + const keyPath = path.resolve(configPath, 'Certificates', `${resolvedHost}.key`) + const certPath = path.resolve(configPath, 'Certificates', `${resolvedHost}.crt`) + const config = { + key: keyPath, + cert: certPath, + resolvedHost: resolvedHost, + configPath: configPath + } + + if (! fs.existsSync(keyPath) || ! fs.existsSync(certPath)) { + return + } + return config +} + /** * Resolve the Herd or Valet server config for the given host. */ @@ -547,36 +573,30 @@ function resolveDevelopmentEnvironmentServerConfig(host: string|boolean|null, pr candidateConfigs.push(...configsAvailable.herd, ...configsAvailable.valet) } - const configPath = candidateConfigs[0] + const configs = candidateConfigs.map(path => resolveCertsInEnvironment(host, path)) + + const resolvedConfig = configs.find(config => config !== undefined) - if (typeof configPath === 'undefined' && host === null) { + if (typeof resolvedConfig === 'undefined' && host === null) { return } - if (typeof configPath === 'undefined') { + if (typeof resolvedConfig === 'undefined') { + if (prefer !== undefined && configsAvailable.herd.length > 0 && configsAvailable.valet.length > 0) { + if (prefer == 'valet') { + throw Error(`Unable to find certificate files for your project in Valet. Ensure you have secured the site with Valet by running by running \`valet secure ${host}\`.`) + } else if (prefer == 'herd') { + throw Error(`Unable to find certificate files for your project in Herd. Ensure you have secured the site with the Herd UI.`) + } else { + throw Error(`Unable to find certificate files for your project in Valet and Herd. Ensure you have secured the site.`) + } + } throw Error(`Unable to find the Herd or Valet configuration directory. Please check they are correctly installed.`) } - const resolvedHost = host === true || host === null - ? path.basename(process.cwd()) + '.' + resolveDevelopmentEnvironmentTld(configPath) - : host - - const keyPath = path.resolve(configPath, 'Certificates', `${resolvedHost}.key`) - const certPath = path.resolve(configPath, 'Certificates', `${resolvedHost}.crt`) - - if (! fs.existsSync(keyPath) || ! fs.existsSync(certPath)) { - if (host === null) { - return - } - - if (configPath === herdMacConfigPath() || configPath === herdWindowsConfigPath()) { - throw Error(`Unable to find certificate files for your host [${resolvedHost}] in the [${configPath}/Certificates] directory. Ensure you have secured the site via the Herd UI.`) - } else if (typeof host === 'string') { - throw Error(`Unable to find certificate files for your host [${resolvedHost}] in the [${configPath}/Certificates] directory. Ensure you have secured the site by running \`valet secure ${host}\`.`) - } else { - throw Error(`Unable to find certificate files for your host [${resolvedHost}] in the [${configPath}/Certificates] directory. Ensure you have secured the site by running \`valet secure\`.`) - } - } + const resolvedHost = resolvedConfig.resolvedHost + const keyPath = resolvedConfig.key + const certPath = resolvedConfig.cert return { hmr: { host: resolvedHost },