From b12de0786d88cab05836135f889add45d6e6b87f Mon Sep 17 00:00:00 2001 From: liuwei Date: Thu, 12 Jan 2023 22:51:33 +0800 Subject: [PATCH] feat: add `savePath` `keyFileName` `certFileName` parameters close #56 --- plugin/lib/util.ts | 12 -------- plugin/mkcert/config.ts | 25 ++++++++++------- plugin/mkcert/index.ts | 61 +++++++++++++++++++++++++++++------------ 3 files changed, 59 insertions(+), 39 deletions(-) diff --git a/plugin/lib/util.ts b/plugin/lib/util.ts index 0f8eb79..9aef926 100644 --- a/plugin/lib/util.ts +++ b/plugin/lib/util.ts @@ -5,8 +5,6 @@ import os from 'os' import path from 'path' import util from 'util' -import { PLUGIN_DATA_DIR } from './constant' - /** * Check if file exists * @@ -22,16 +20,6 @@ export const exists = async (filePath: string) => { } } -/** - * Resolve file path with `PLUGIN_DATA_DIR` - * - * @param fileName file name - * @returns absolute path - */ -export const resolvePath = (fileName: string) => { - return path.resolve(PLUGIN_DATA_DIR, fileName) -} - export const mkdir = async (dirname: string) => { const isExist = await exists(dirname) diff --git a/plugin/mkcert/config.ts b/plugin/mkcert/config.ts index 9251af3..2d27490 100644 --- a/plugin/mkcert/config.ts +++ b/plugin/mkcert/config.ts @@ -1,11 +1,7 @@ +import path from 'path' + import { debug } from '../lib/logger' -import { - resolvePath, - readFile, - writeFile, - prettyLog, - deepMerge -} from '../lib/util' +import { readFile, writeFile, prettyLog, deepMerge } from '../lib/util' export type RecordMate = { /** @@ -24,8 +20,11 @@ export type RecordHash = { cert?: string } +export type ConfigOptions = { + savePath: string +} + const CONFIG_FILE_NAME = 'config.json' -const CONFIG_FILE_PATH = resolvePath(CONFIG_FILE_NAME) class Config { /** @@ -35,8 +34,14 @@ class Config { private record: RecordMate | undefined + private configFilePath: string + + constructor({ savePath }: ConfigOptions) { + this.configFilePath = path.resolve(savePath, CONFIG_FILE_NAME) + } + public async init() { - const str = await readFile(CONFIG_FILE_PATH) + const str = await readFile(this.configFilePath) const options = str ? JSON.parse(str) : undefined if (options) { @@ -46,7 +51,7 @@ class Config { } private async serialize() { - await writeFile(CONFIG_FILE_PATH, prettyLog(this)) + await writeFile(this.configFilePath, prettyLog(this)) } // deep merge diff --git a/plugin/mkcert/index.ts b/plugin/mkcert/index.ts index a58371c..dfa3725 100644 --- a/plugin/mkcert/index.ts +++ b/plugin/mkcert/index.ts @@ -1,7 +1,9 @@ import fs from 'fs' +import path from 'path' import process from 'process' import pc from 'picocolors' +import { PLUGIN_DATA_DIR } from 'plugin/lib/constant' import { Logger } from 'vite' import { debug } from '../lib/logger' @@ -11,8 +13,7 @@ import { exec, exists, getHash, - prettyLog, - resolvePath + prettyLog } from '../lib/util' import Config from './config' @@ -49,19 +50,33 @@ export type MkcertBaseOptions = { * @default none */ mkcertPath?: string + + /** + * The location to save the files, such as key and cert files + */ + savePath?: string + + /** + * The name of private key file generated by mkcert + */ + keyFileName?: string + + /** + * The name of cert file generated by mkcert + */ + certFileName?: string } export type MkcertOptions = MkcertBaseOptions & { logger: Logger } -const KEY_FILE_PATH = resolvePath('certs/dev.key') -const CERT_FILE_PATH = resolvePath('certs/dev.pem') - class Mkcert { private force?: boolean private autoUpgrade?: boolean private mkcertLocalPath?: string + private keyFilePath: string + private certFilePath: string private source: BaseSource private logger: Logger @@ -75,12 +90,23 @@ class Mkcert { } private constructor(options: MkcertOptions) { - const { force, autoUpgrade, source, mkcertPath, logger } = options + const { + force, + autoUpgrade, + source, + mkcertPath, + savePath = PLUGIN_DATA_DIR, + keyFileName = 'dev.pem', + certFileName = 'cert.pem', + logger + } = options this.force = force this.logger = logger this.autoUpgrade = autoUpgrade this.mkcertLocalPath = mkcertPath + this.keyFilePath = path.resolve(savePath, keyFileName) + this.certFilePath = path.resolve(savePath, certFileName) this.sourceType = source || 'github' if (this.sourceType === 'github') { @@ -91,11 +117,12 @@ class Mkcert { this.source = this.sourceType } - this.mkcertSavedPath = resolvePath( + this.mkcertSavedPath = path.resolve( + savePath, process.platform === 'win32' ? 'mkcert.exe' : 'mkcert' ) - this.config = new Config() + this.config = new Config({ savePath }) } private async getMkcertBinnary() { @@ -125,8 +152,8 @@ class Mkcert { } private async getCertificate() { - const key = await fs.promises.readFile(KEY_FILE_PATH) - const cert = await fs.promises.readFile(CERT_FILE_PATH) + const key = await fs.promises.readFile(this.keyFilePath) + const cert = await fs.promises.readFile(this.certFilePath) return { key, @@ -145,12 +172,12 @@ class Mkcert { ) } - await ensureDirExist(KEY_FILE_PATH) - await ensureDirExist(CERT_FILE_PATH) + await ensureDirExist(this.keyFilePath) + await ensureDirExist(this.certFilePath) const cmd = `${escape(mkcertBinnary)} -install -key-file ${escape( - KEY_FILE_PATH - )} -cert-file ${escape(CERT_FILE_PATH)} ${names}` + this.keyFilePath + )} -cert-file ${escape(this.certFilePath)} ${names}` await exec(cmd, { env: { @@ -160,14 +187,14 @@ class Mkcert { }) this.logger.info( - `The certificate is saved in:\n${KEY_FILE_PATH}\n${CERT_FILE_PATH}` + `The list of generated files:\n${this.keyFilePath}\n${this.certFilePath}` ) } private getLatestHash = async () => { return { - key: await getHash(KEY_FILE_PATH), - cert: await getHash(CERT_FILE_PATH) + key: await getHash(this.keyFilePath), + cert: await getHash(this.certFilePath) } }