diff --git a/packages/plugin/src/index.ts b/packages/plugin/src/index.ts index 7e4326f..7c0eed9 100644 --- a/packages/plugin/src/index.ts +++ b/packages/plugin/src/index.ts @@ -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 { diff --git a/packages/plugin/src/lib/util.ts b/packages/plugin/src/lib/util.ts index 9999781..c92bb67 100644 --- a/packages/plugin/src/lib/util.ts +++ b/packages/plugin/src/lib/util.ts @@ -92,6 +92,7 @@ export const getHash = async (filePath: string) => { hash.update(content) return hash.digest('hex') } + return undefined } @@ -124,3 +125,7 @@ export const deepMerge = (target: any, ...source: any[]) => { export const prettyLog = (obj?: Record) => { return JSON.stringify(obj, null, 2) } + +export const escape = (path?: string) => { + return `"${path}"` +} diff --git a/packages/plugin/src/mkcert/index.ts b/packages/plugin/src/mkcert/index.ts index 0a777fb..e53e3b0 100644 --- a/packages/plugin/src/mkcert/index.ts +++ b/packages/plugin/src/mkcert/index.ts @@ -6,6 +6,7 @@ import { Logger } from 'vite' import { debug } from '../lib/logger' import { ensureDirExist, + escape, exec, exists, getHash, @@ -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)