Skip to content

Commit f89fcde

Browse files
Support Laravel Herd (#233)
* Support Laravel Herd * Formatting * Formatting --------- Co-authored-by: Tim MacDonald <hello@timacdonald.me>
1 parent a672461 commit f89fcde

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

src/index.ts

+38-15
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,18 @@ interface PluginConfig {
5555
refresh?: boolean|string|string[]|RefreshConfig|RefreshConfig[]
5656

5757
/**
58-
* Utilise the valet TLS certificates.
58+
* Utilise the Herd or Valet TLS certificates.
5959
*
6060
* @default false
6161
*/
62+
detectTls?: string|boolean,
63+
64+
/**
65+
* Utilise the Herd or Valet TLS certificates.
66+
*
67+
* @default false
68+
* @deprecated use "detectTls" instead
69+
*/
6270
valetTls?: string|boolean,
6371

6472
/**
@@ -121,7 +129,7 @@ function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlug
121129
const env = loadEnv(mode, userConfig.envDir || process.cwd(), '')
122130
const assetUrl = env.ASSET_URL ?? ''
123131
const serverConfig = command === 'serve'
124-
? (resolveValetServerConfig(pluginConfig.valetTls) ?? resolveEnvironmentServerConfig(env))
132+
? (resolveDevelopmentEnvironmentServerConfig(pluginConfig.detectTls) ?? resolveEnvironmentServerConfig(env))
125133
: undefined
126134

127135
ensureCommandShouldRunInEnvironment(command, env)
@@ -333,6 +341,7 @@ function resolvePluginConfig(config: string|string[]|PluginConfig): Required<Plu
333341
refresh: config.refresh ?? false,
334342
hotFile: config.hotFile ?? path.join((config.publicDirectory ?? 'public'), 'hot'),
335343
valetTls: config.valetTls ?? false,
344+
detectTls: config.detectTls ?? config.valetTls ?? false,
336345
transformOnServe: config.transformOnServe ?? ((code) => code),
337346
}
338347
}
@@ -492,11 +501,10 @@ function resolveHostFromEnv(env: Record<string, string>): string|undefined
492501
}
493502
}
494503

495-
496504
/**
497-
* Resolve the valet server config for the given host.
505+
* Resolve the Herd or Valet server config for the given host.
498506
*/
499-
function resolveValetServerConfig(host: string|boolean): {
507+
function resolveDevelopmentEnvironmentServerConfig(host: string|boolean): {
500508
hmr?: { host: string }
501509
host?: string,
502510
https?: { cert: Buffer, key: Buffer }
@@ -505,13 +513,15 @@ function resolveValetServerConfig(host: string|boolean): {
505513
return
506514
}
507515

508-
host = host === true ? resolveValetHost() : host
516+
const configPath = determineDevelopmentEnvironmentConfigPath();
517+
518+
host = host === true ? resolveDevelopmentEnvironmentHost(configPath) : host
509519

510-
const keyPath = path.resolve(os.homedir(), `.config/valet/Certificates/${host}.key`)
511-
const certPath = path.resolve(os.homedir(), `.config/valet/Certificates/${host}.crt`)
520+
const keyPath = path.resolve(configPath, 'Certificates', `${host}.key`)
521+
const certPath = path.resolve(configPath, 'Certificates', `${host}.crt`)
512522

513523
if (! fs.existsSync(keyPath) || ! fs.existsSync(certPath)) {
514-
throw Error(`Unable to find Valet certificate files for your host [${host}]. Ensure you have run "valet secure".`)
524+
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\`.`)
515525
}
516526

517527
return {
@@ -525,16 +535,29 @@ function resolveValetServerConfig(host: string|boolean): {
525535
}
526536

527537
/**
528-
* Resolve the valet valet host for the current directory.
538+
* Resolve the path to the Herd or Valet configuration directory.
539+
*/
540+
function determineDevelopmentEnvironmentConfigPath(): string {
541+
const herdConfigPath = path.resolve(os.homedir(), 'Library', 'Application Support', 'Herd', 'config', 'valet')
542+
543+
if (fs.existsSync(herdConfigPath)) {
544+
return herdConfigPath
545+
}
546+
547+
return path.resolve(os.homedir(), '.config', 'valet');
548+
}
549+
550+
/**
551+
* Resolve the Herd or Valet host for the current directory.
529552
*/
530-
function resolveValetHost(): string {
531-
const configPath = os.homedir() + `/.config/valet/config.json`
553+
function resolveDevelopmentEnvironmentHost(configPath: string): string {
554+
const configFile = path.resolve(configPath, 'config.json')
532555

533-
if (! fs.existsSync(configPath)) {
534-
throw Error('Unable to find the Valet configuration file. You will need to manually specify the host in the `valetTls` configuration option.')
556+
if (! fs.existsSync(configFile)) {
557+
throw Error(`Unable to find the configuration file [${configFile}]. You will need to manually specify the host in the \`detectTls\` configuration option.`)
535558
}
536559

537-
const config: { tld: string } = JSON.parse(fs.readFileSync(configPath, 'utf-8'))
560+
const config: { tld: string } = JSON.parse(fs.readFileSync(configFile, 'utf-8'))
538561

539562
return path.basename(process.cwd()) + '.' + config.tld
540563
}

0 commit comments

Comments
 (0)