Skip to content

Commit

Permalink
feat: add savePath keyFileName certFileName parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
liuweiGL committed Jan 12, 2023
1 parent fd5a8c0 commit b12de07
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 39 deletions.
12 changes: 0 additions & 12 deletions plugin/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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)

Expand Down
25 changes: 15 additions & 10 deletions plugin/mkcert/config.ts
Original file line number Diff line number Diff line change
@@ -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 = {
/**
Expand All @@ -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 {
/**
Expand All @@ -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) {
Expand All @@ -46,7 +51,7 @@ class Config {
}

private async serialize() {
await writeFile(CONFIG_FILE_PATH, prettyLog(this))
await writeFile(this.configFilePath, prettyLog(this))
}

// deep merge
Expand Down
61 changes: 44 additions & 17 deletions plugin/mkcert/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -11,8 +13,7 @@ import {
exec,
exists,
getHash,
prettyLog,
resolvePath
prettyLog
} from '../lib/util'

import Config from './config'
Expand Down Expand Up @@ -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

Expand All @@ -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') {
Expand All @@ -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() {
Expand Down Expand Up @@ -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,
Expand All @@ -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: {
Expand All @@ -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)
}
}

Expand Down

0 comments on commit b12de07

Please sign in to comment.