Skip to content

Commit

Permalink
fix: fix the error caused by the path containing spaces
Browse files Browse the repository at this point in the history
close #17
  • Loading branch information
liuweiGL committed Oct 8, 2021
1 parent 1ea70c1 commit 655bfa4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const plugin = (options: ViteCertificateOptions = {}): Plugin => {
const allHosts =
typeof server.host === 'string' ? [...hosts, server.host] : hosts
const uniqueHosts = Array.from(new Set(allHosts)).filter(item => !!item)

const certificate = await mkcert.install(uniqueHosts)

return {
Expand Down
5 changes: 5 additions & 0 deletions packages/plugin/src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export const getHash = async (filePath: string) => {
hash.update(content)
return hash.digest('hex')
}

return undefined
}

Expand Down Expand Up @@ -124,3 +125,7 @@ export const deepMerge = (target: any, ...source: any[]) => {
export const prettyLog = (obj?: Record<string, any>) => {
return JSON.stringify(obj, null, 2)
}

export const escape = (path?: string) => {
return `"${path}"`
}
12 changes: 8 additions & 4 deletions packages/plugin/src/mkcert/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Logger } from 'vite'
import { debug } from '../lib/logger'
import {
ensureDirExist,
escape,
exec,
exists,
getHash,
Expand Down Expand Up @@ -126,20 +127,23 @@ class Mkcert {
}
}

private async createCertificate(hostnames: string[]) {
const hostlist = hostnames.join(' ')
private async createCertificate(hosts: string[]) {
const names = hosts.join(' ')

const mkcertBinnary = await this.getMkcertBinnary()

if (!mkcertBinnary) {
debug(
`Mkcert does not exist, unable to generate certificate for ${hostlist}`
`Mkcert does not exist, unable to generate certificate for ${names}`
)
}

await ensureDirExist(KEY_FILE_PATH)
await ensureDirExist(CERT_FILE_PATH)

const cmd = `${mkcertBinnary} -install -key-file ${KEY_FILE_PATH} -cert-file ${CERT_FILE_PATH} ${hostlist}`
const cmd = `${escape(mkcertBinnary)} -install -key-file ${escape(
KEY_FILE_PATH
)} -cert-file ${escape(CERT_FILE_PATH)} ${names}`

await exec(cmd)

Expand Down

0 comments on commit 655bfa4

Please sign in to comment.