Skip to content

Commit b2daa54

Browse files
authored
[1.x] Auto detect Valet / Herd TLS certificates (#180)
* Adds Valet certificate auto-detection. * wip * wip * wip * wip * wip * Improve error messages
1 parent eced3cb commit b2daa54

File tree

1 file changed

+70
-26
lines changed

1 file changed

+70
-26
lines changed

src/index.ts

Lines changed: 70 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ interface PluginConfig {
5757
/**
5858
* Utilise the Herd or Valet TLS certificates.
5959
*
60-
* @default false
60+
* @default null
6161
*/
62-
detectTls?: string|boolean,
62+
detectTls?: string|boolean|null,
6363

6464
/**
6565
* Utilise the Herd or Valet TLS certificates.
6666
*
67-
* @default false
67+
* @default null
6868
* @deprecated use "detectTls" instead
6969
*/
70-
valetTls?: string|boolean,
70+
valetTls?: string|boolean|null,
7171

7272
/**
7373
* Transform the code while serving.
@@ -211,6 +211,16 @@ function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlug
211211
server.config.logger.info(`\n ${colors.red(`${colors.bold('LARAVEL')} ${laravelVersion()}`)} ${colors.dim('plugin')} ${colors.bold(`v${pluginVersion()}`)}`)
212212
server.config.logger.info('')
213213
server.config.logger.info(` ${colors.green('➜')} ${colors.bold('APP_URL')}: ${colors.cyan(appUrl.replace(/:(\d+)/, (_, port) => `:${colors.bold(port)}`))}`)
214+
215+
if (typeof resolvedConfig.server.https === 'object' && typeof resolvedConfig.server.https.key === 'string') {
216+
if (resolvedConfig.server.https.key.startsWith(herdConfigPath())) {
217+
server.config.logger.info(` ${colors.green('➜')} Using Herd certificate to secure Vite.`)
218+
}
219+
220+
if (resolvedConfig.server.https.key.startsWith(valetConfigPath())) {
221+
server.config.logger.info(` ${colors.green('➜')} Using Valet certificate to secure Vite.`)
222+
}
223+
}
214224
}, 100)
215225
}
216226
})
@@ -342,8 +352,8 @@ function resolvePluginConfig(config: string|string[]|PluginConfig): Required<Plu
342352
ssrOutputDirectory: config.ssrOutputDirectory ?? 'bootstrap/ssr',
343353
refresh: config.refresh ?? false,
344354
hotFile: config.hotFile ?? path.join((config.publicDirectory ?? 'public'), 'hot'),
345-
valetTls: config.valetTls ?? false,
346-
detectTls: config.detectTls ?? config.valetTls ?? false,
355+
valetTls: config.valetTls ?? null,
356+
detectTls: config.detectTls ?? config.valetTls ?? null,
347357
transformOnServe: config.transformOnServe ?? ((code) => code),
348358
}
349359
}
@@ -507,62 +517,82 @@ function resolveHostFromEnv(env: Record<string, string>): string|undefined
507517
/**
508518
* Resolve the Herd or Valet server config for the given host.
509519
*/
510-
function resolveDevelopmentEnvironmentServerConfig(host: string|boolean): {
520+
function resolveDevelopmentEnvironmentServerConfig(host: string|boolean|null): {
511521
hmr?: { host: string }
512522
host?: string,
513-
https?: { cert: Buffer, key: Buffer }
523+
https?: { cert: string, key: string }
514524
}|undefined {
515525
if (host === false) {
516526
return
517527
}
518528

519529
const configPath = determineDevelopmentEnvironmentConfigPath();
520530

521-
host = host === true ? resolveDevelopmentEnvironmentHost(configPath) : host
531+
if (typeof configPath === 'undefined' && host === null) {
532+
return
533+
}
534+
535+
if (typeof configPath === 'undefined') {
536+
throw Error(`Unable to find the Herd or Valet configuration directory. Please check they are correctly installed.`)
537+
}
522538

523-
const keyPath = path.resolve(configPath, 'Certificates', `${host}.key`)
524-
const certPath = path.resolve(configPath, 'Certificates', `${host}.crt`)
539+
const resolvedHost = host === true || host === null
540+
? path.basename(process.cwd()) + '.' + resolveDevelopmentEnvironmentTld(configPath)
541+
: host
542+
543+
const keyPath = path.resolve(configPath, 'Certificates', `${resolvedHost}.key`)
544+
const certPath = path.resolve(configPath, 'Certificates', `${resolvedHost}.crt`)
525545

526546
if (! fs.existsSync(keyPath) || ! fs.existsSync(certPath)) {
527-
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\`.`)
547+
if (host === null) {
548+
return
549+
}
550+
551+
if (configPath === herdConfigPath()) {
552+
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.`)
553+
} else if (typeof host === 'string') {
554+
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}\`.`)
555+
} else {
556+
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\`.`)
557+
}
528558
}
529559

530560
return {
531-
hmr: { host },
532-
host,
561+
hmr: { host: resolvedHost },
562+
host: resolvedHost,
533563
https: {
534-
key: fs.readFileSync(keyPath),
535-
cert: fs.readFileSync(certPath),
564+
key: keyPath,
565+
cert: certPath,
536566
},
537567
}
538568
}
539569

540570
/**
541571
* Resolve the path to the Herd or Valet configuration directory.
542572
*/
543-
function determineDevelopmentEnvironmentConfigPath(): string {
544-
const herdConfigPath = path.resolve(os.homedir(), 'Library', 'Application Support', 'Herd', 'config', 'valet')
545-
546-
if (fs.existsSync(herdConfigPath)) {
547-
return herdConfigPath
573+
function determineDevelopmentEnvironmentConfigPath(): string|undefined {
574+
if (fs.existsSync(herdConfigPath())) {
575+
return herdConfigPath()
548576
}
549577

550-
return path.resolve(os.homedir(), '.config', 'valet');
578+
if (fs.existsSync(valetConfigPath())) {
579+
return valetConfigPath()
580+
}
551581
}
552582

553583
/**
554-
* Resolve the Herd or Valet host for the current directory.
584+
* Resolve the TLD via the config path.
555585
*/
556-
function resolveDevelopmentEnvironmentHost(configPath: string): string {
586+
function resolveDevelopmentEnvironmentTld(configPath: string): string {
557587
const configFile = path.resolve(configPath, 'config.json')
558588

559589
if (! fs.existsSync(configFile)) {
560-
throw Error(`Unable to find the configuration file [${configFile}]. You will need to manually specify the host in the \`detectTls\` configuration option.`)
590+
throw Error(`Unable to find the configuration file [${configFile}].`)
561591
}
562592

563593
const config: { tld: string } = JSON.parse(fs.readFileSync(configFile, 'utf-8'))
564594

565-
return path.basename(process.cwd()) + '.' + config.tld
595+
return config.tld
566596
}
567597

568598
/**
@@ -571,3 +601,17 @@ function resolveDevelopmentEnvironmentHost(configPath: string): string {
571601
function dirname(): string {
572602
return fileURLToPath(new URL('.', import.meta.url))
573603
}
604+
605+
/**
606+
* Herd's configuration directory.
607+
*/
608+
function herdConfigPath(): string {
609+
return path.resolve(os.homedir(), 'Library', 'Application Support', 'Herd', 'config', 'valet')
610+
}
611+
612+
/**
613+
* Valet's configuration directory.
614+
*/
615+
function valetConfigPath(): string {
616+
return path.resolve(os.homedir(), '.config', 'valet')
617+
}

0 commit comments

Comments
 (0)