From 1e643560b7db012a475228349165b0866ac5829b Mon Sep 17 00:00:00 2001 From: Claudio Dekker Date: Thu, 20 Jul 2023 21:07:11 +0200 Subject: [PATCH 1/3] Support Laravel Herd --- src/index.ts | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 08f54e6..88cd136 100644 --- a/src/index.ts +++ b/src/index.ts @@ -505,10 +505,12 @@ function resolveValetServerConfig(host: string|boolean): { return } - host = host === true ? resolveValetHost() : host + const configPath = determineValetConfigPath(); - const keyPath = path.resolve(os.homedir(), `.config/valet/Certificates/${host}.key`) - const certPath = path.resolve(os.homedir(), `.config/valet/Certificates/${host}.crt`) + host = host === true ? resolveValetHost(configPath) : host + + const keyPath = path.resolve(configPath, 'Certificates', `${host}.key`) + const certPath = path.resolve(configPath, 'Certificates', `${host}.crt`) if (! fs.existsSync(keyPath) || ! fs.existsSync(certPath)) { throw Error(`Unable to find Valet certificate files for your host [${host}]. Ensure you have run "valet secure".`) @@ -524,17 +526,30 @@ function resolveValetServerConfig(host: string|boolean): { } } +/** + * Resolve the path to the Valet configuration directory. + */ +function determineValetConfigPath(): string { + const herdConfigPath = path.resolve(os.homedir(), 'Library', 'Application Support', 'Herd', 'config', 'valet') + + if (fs.existsSync(herdConfigPath)) { + return herdConfigPath + } + + return path.resolve(os.homedir(), '.config', 'valet'); +} + /** * Resolve the valet valet host for the current directory. */ -function resolveValetHost(): string { - const configPath = os.homedir() + `/.config/valet/config.json` +function resolveValetHost(configPath: string): string { + const configFile = path.resolve(configPath, 'config.json') - if (! fs.existsSync(configPath)) { + if (! fs.existsSync(configFile)) { throw Error('Unable to find the Valet configuration file. You will need to manually specify the host in the `valetTls` configuration option.') } - const config: { tld: string } = JSON.parse(fs.readFileSync(configPath, 'utf-8')) + const config: { tld: string } = JSON.parse(fs.readFileSync(configFile, 'utf-8')) return path.basename(process.cwd()) + '.' + config.tld } From 73e271606e79d3c00bc3a4ea473ae7c0690156ab Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Wed, 26 Jul 2023 14:37:57 +1000 Subject: [PATCH 2/3] Formatting --- src/index.ts | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/index.ts b/src/index.ts index 88cd136..805cb89 100644 --- a/src/index.ts +++ b/src/index.ts @@ -55,7 +55,7 @@ interface PluginConfig { refresh?: boolean|string|string[]|RefreshConfig|RefreshConfig[] /** - * Utilise the valet TLS certificates. + * Utilise the Herd or Valet TLS certificates. * * @default false */ @@ -121,7 +121,7 @@ function resolveLaravelPlugin(pluginConfig: Required): LaravelPlug const env = loadEnv(mode, userConfig.envDir || process.cwd(), '') const assetUrl = env.ASSET_URL ?? '' const serverConfig = command === 'serve' - ? (resolveValetServerConfig(pluginConfig.valetTls) ?? resolveEnvironmentServerConfig(env)) + ? (resolveDevelopmentEnvironmentServerConfig(pluginConfig.valetTls) ?? resolveEnvironmentServerConfig(env)) : undefined ensureCommandShouldRunInEnvironment(command, env) @@ -492,11 +492,10 @@ function resolveHostFromEnv(env: Record): string|undefined } } - /** - * Resolve the valet server config for the given host. + * Resolve the Herd or Valet server config for the given host. */ -function resolveValetServerConfig(host: string|boolean): { +function resolveDevelopmentEnvironmentServerConfig(host: string|boolean): { hmr?: { host: string } host?: string, https?: { cert: Buffer, key: Buffer } @@ -505,15 +504,15 @@ function resolveValetServerConfig(host: string|boolean): { return } - const configPath = determineValetConfigPath(); + const configPath = determineDevelopmentEnvironmentConfigPath(); - host = host === true ? resolveValetHost(configPath) : host + host = host === true ? resolveDevelopmentEnvironmentHost(configPath) : host const keyPath = path.resolve(configPath, 'Certificates', `${host}.key`) const certPath = path.resolve(configPath, 'Certificates', `${host}.crt`) if (! fs.existsSync(keyPath) || ! fs.existsSync(certPath)) { - throw Error(`Unable to find Valet certificate files for your host [${host}]. Ensure you have run "valet secure".`) + throw Error(`Unable to find certificate files for your host [${host}] in the [${configPath}/Certificates] directory. Ensure you have run secured the site via the Herd UI or run \`valet secure\`.`) } return { @@ -527,9 +526,9 @@ function resolveValetServerConfig(host: string|boolean): { } /** - * Resolve the path to the Valet configuration directory. + * Resolve the path to the Herd or Valet configuration directory. */ -function determineValetConfigPath(): string { +function determineDevelopmentEnvironmentConfigPath(): string { const herdConfigPath = path.resolve(os.homedir(), 'Library', 'Application Support', 'Herd', 'config', 'valet') if (fs.existsSync(herdConfigPath)) { @@ -540,13 +539,13 @@ function determineValetConfigPath(): string { } /** - * Resolve the valet valet host for the current directory. + * Resolve the Herd or Valet host for the current directory. */ -function resolveValetHost(configPath: string): string { +function resolveDevelopmentEnvironmentHost(configPath: string): string { const configFile = path.resolve(configPath, 'config.json') if (! fs.existsSync(configFile)) { - throw Error('Unable to find the Valet configuration file. You will need to manually specify the host in the `valetTls` configuration option.') + throw Error(`Unable to find the configuration file [${configFile}]. You will need to manually specify the host in the \`valetTls\` configuration option.`) } const config: { tld: string } = JSON.parse(fs.readFileSync(configFile, 'utf-8')) From adbe2559bc8e265039e32e372c41998d89094c48 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Wed, 26 Jul 2023 14:46:14 +1000 Subject: [PATCH 3/3] Formatting --- src/index.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 805cb89..aaef8be 100644 --- a/src/index.ts +++ b/src/index.ts @@ -59,6 +59,14 @@ interface PluginConfig { * * @default false */ + detectTls?: string|boolean, + + /** + * Utilise the Herd or Valet TLS certificates. + * + * @default false + * @deprecated use "detectTls" instead + */ valetTls?: string|boolean, /** @@ -121,7 +129,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.valetTls) ?? resolveEnvironmentServerConfig(env)) + ? (resolveDevelopmentEnvironmentServerConfig(pluginConfig.detectTls) ?? resolveEnvironmentServerConfig(env)) : undefined ensureCommandShouldRunInEnvironment(command, env) @@ -333,6 +341,7 @@ function resolvePluginConfig(config: string|string[]|PluginConfig): Required code), } } @@ -512,7 +521,7 @@ function resolveDevelopmentEnvironmentServerConfig(host: string|boolean): { const certPath = path.resolve(configPath, 'Certificates', `${host}.crt`) if (! fs.existsSync(keyPath) || ! fs.existsSync(certPath)) { - throw Error(`Unable to find certificate files for your host [${host}] in the [${configPath}/Certificates] directory. Ensure you have run secured the site via the Herd UI or run \`valet secure\`.`) + throw Error(`Unable to find certificate files for your host [${host}] in the [${configPath}/Certificates] directory. Ensure you have secured the site via the Herd UI or run \`valet secure\`.`) } return { @@ -545,7 +554,7 @@ function resolveDevelopmentEnvironmentHost(configPath: string): string { const configFile = path.resolve(configPath, 'config.json') if (! fs.existsSync(configFile)) { - throw Error(`Unable to find the configuration file [${configFile}]. You will need to manually specify the host in the \`valetTls\` configuration option.`) + throw Error(`Unable to find the configuration file [${configFile}]. You will need to manually specify the host in the \`detectTls\` configuration option.`) } const config: { tld: string } = JSON.parse(fs.readFileSync(configFile, 'utf-8'))