Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Laravel Herd #233

Merged
merged 3 commits into from
Jul 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,18 @@ interface PluginConfig {
refresh?: boolean|string|string[]|RefreshConfig|RefreshConfig[]

/**
* Utilise the valet TLS certificates.
* Utilise the Herd or Valet TLS certificates.
*
* @default false
*/
detectTls?: string|boolean,

/**
* Utilise the Herd or Valet TLS certificates.
*
* @default false
* @deprecated use "detectTls" instead
*/
valetTls?: string|boolean,

/**
Expand Down Expand Up @@ -121,7 +129,7 @@ function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): 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.detectTls) ?? resolveEnvironmentServerConfig(env))
: undefined

ensureCommandShouldRunInEnvironment(command, env)
Expand Down Expand Up @@ -333,6 +341,7 @@ function resolvePluginConfig(config: string|string[]|PluginConfig): Required<Plu
refresh: config.refresh ?? false,
hotFile: config.hotFile ?? path.join((config.publicDirectory ?? 'public'), 'hot'),
valetTls: config.valetTls ?? false,
detectTls: config.detectTls ?? config.valetTls ?? false,
transformOnServe: config.transformOnServe ?? ((code) => code),
}
}
Expand Down Expand Up @@ -492,11 +501,10 @@ function resolveHostFromEnv(env: Record<string, string>): 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 }
Expand All @@ -505,13 +513,15 @@ function resolveValetServerConfig(host: string|boolean): {
return
}

host = host === true ? resolveValetHost() : host
const configPath = determineDevelopmentEnvironmentConfigPath();

host = host === true ? resolveDevelopmentEnvironmentHost(configPath) : host

const keyPath = path.resolve(os.homedir(), `.config/valet/Certificates/${host}.key`)
const certPath = path.resolve(os.homedir(), `.config/valet/Certificates/${host}.crt`)
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 secured the site via the Herd UI or run \`valet secure\`.`)
}

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

/**
* Resolve the valet valet host for the current directory.
* Resolve the path to the Herd or Valet configuration directory.
*/
function determineDevelopmentEnvironmentConfigPath(): 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 Herd or Valet host for the current directory.
*/
function resolveValetHost(): string {
const configPath = os.homedir() + `/.config/valet/config.json`
function resolveDevelopmentEnvironmentHost(configPath: string): string {
const configFile = path.resolve(configPath, 'config.json')

if (! fs.existsSync(configPath)) {
throw Error('Unable to find the Valet configuration file. You will need to manually specify the host in the `valetTls` configuration option.')
if (! fs.existsSync(configFile)) {
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(configPath, 'utf-8'))
const config: { tld: string } = JSON.parse(fs.readFileSync(configFile, 'utf-8'))

return path.basename(process.cwd()) + '.' + config.tld
}
Expand Down