Skip to content

Commit

Permalink
feat: retain the existing ca
Browse files Browse the repository at this point in the history
close #59
  • Loading branch information
liuweiGL committed Feb 4, 2023
1 parent 33ece72 commit ee85a33
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 18 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"homepage": "https://github.com/liuweiGL/vite-plugin-mkcert#readme",
"engines": {
"node": ">=v16.0.0"
"node": ">=v16.7.0"
},
"packageManager": "[email protected]+",
"types": "./dist/index.d.ts",
Expand All @@ -36,7 +36,7 @@
}
},
"scripts": {
"test": "vite -c playground/vite.config.ts",
"test": "pnpm build && vite -c playground/vite.config.ts",
"build": "tsx script/build.mts",
"lint": "tsc --noEmit && eslint \"plugin/**/*.ts\" --color --quiet",
"release": "semantic-release"
Expand Down
17 changes: 17 additions & 0 deletions plugin/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import os from 'os'
import path from 'path'
import util from 'util'

import { PLUGIN_NAME } from './constant'

/**
* Check if file exists
*
Expand Down Expand Up @@ -47,6 +49,21 @@ export const writeFile = async (
await fs.promises.chmod(filePath, 0o777)
}

export const readDir = async (source: string) => {
return fs.promises.readdir(source)
}

export const copyDir = async (source: string, dest: string) => {
try {
await fs.promises.cp(source, dest, {
recursive: true
})
} catch (error: any) {
//Fails when nodejs version < 16.7.0, ignore?
console.log(`${PLUGIN_NAME}:`, error)
}
}

export const exec = async (cmd: string, options?: ExecOptions) => {
return util.promisify(child_process.exec)(cmd, options)
}
Expand Down
59 changes: 43 additions & 16 deletions plugin/mkcert/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fs from 'fs'
import path from 'path'
import process from 'process'

Expand All @@ -8,12 +7,15 @@ import { Logger } from 'vite'
import { PLUGIN_DATA_DIR } from '../lib/constant'
import { debug } from '../lib/logger'
import {
copyDir,
ensureDirExist,
escape,
exec,
exists,
getHash,
prettyLog
prettyLog,
readDir,
readFile
} from '../lib/util'

import Config from './config'
Expand Down Expand Up @@ -106,7 +108,7 @@ class Mkcert {
this.logger = logger
this.autoUpgrade = autoUpgrade
this.localMkcert = mkcertPath
this.mkcertLocalPath = mkcertPath
this.savePath = path.resolve(savePath)
this.keyFilePath = path.resolve(savePath, keyFileName)
this.certFilePath = path.resolve(savePath, certFileName)
this.sourceType = source || 'github'
Expand All @@ -124,32 +126,58 @@ class Mkcert {
process.platform === 'win32' ? 'mkcert.exe' : 'mkcert'
)

this.config = new Config({ savePath })
this.config = new Config({ savePath: this.savePath })
}

private async getMkcertBinnary() {
if (this.localMkcert) {
if (await exists(this.localMkcert)) {
return this.localMkcert
}
this.logger.error(
pc.red(
}
this.logger.error(
pc.red(
`${this.localMkcert} does not exist, please check the mkcertPath parameter`
)
)
)
return undefined
} else if (await exists(this.savedMkcert)) {
return this.savedMkcert
}
}
return undefined
}

private async checkCAExists() {
const files = await readDir(this.savePath)
return files.some(file => file.includes('rootCA'))
}

private async retainExistedCA() {
if (await this.checkCAExists()) {
return
}
return exist

const mkcertBinnary = await this.getMkcertBinnary()
const commandResult = await (await exec(`${mkcertBinnary} -CAROOT`)).stdout
const caDirPath = path.resolve(
commandResult.toString().replaceAll('\n', '')
)

if (caDirPath === this.savePath) {
return
}

const caDirExists = await exists(caDirPath)

if (!caDirExists) {
return
}

await copyDir(caDirPath, this.savePath)
}

private async getCertificate() {
const key = await fs.promises.readFile(this.keyFilePath)
const cert = await fs.promises.readFile(this.certFilePath)
const key = await readFile(this.keyFilePath)
const cert = await readFile(this.certFilePath)

return {
key,
Expand All @@ -168,10 +196,8 @@ class Mkcert {
)
}

await Promise.all([
ensureDirExist(this.keyFilePath),
ensureDirExist(this.certFilePath)
])
await ensureDirExist(this.savePath)
await this.retainExistedCA()

const cmd = `${escape(mkcertBinnary)} -install -key-file ${escape(
this.keyFilePath
Expand Down Expand Up @@ -240,6 +266,7 @@ class Mkcert {

return sourceInfo
}

private async initMkcert() {
const sourceInfo = await this.getSourceInfo()

Expand Down

0 comments on commit ee85a33

Please sign in to comment.