Skip to content

Commit d3de841

Browse files
authored
Merge pull request #51 from daquinoaldo/fix-issue-28
Fix issue #28: the file is being used by another process on Windows
2 parents 770a5b5 + 1574929 commit d3de841

File tree

6 files changed

+221
-157
lines changed

6 files changed

+221
-157
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"mocha": true
77
},
88
"parserOptions": {
9-
"ecmaVersion": 8
9+
"ecmaVersion": 2018
1010
},
1111
"rules": {
1212
"semi": ["warn", "never"],

.github/workflows/test.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ jobs:
1010
strategy:
1111
matrix:
1212
os: [ubuntu-latest, macOS-latest]
13-
node: [10, 12, 13]
14-
exclude:
15-
- os: ubuntu-latest
16-
node: 8
13+
node: [10, 12, 14]
1714

1815
steps:
1916
- uses: actions/checkout@v2

certs.js

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function getExe() {
4646
if (process.arch === "arm" || process.arch === "arm64")
4747
return "mkcert-" + MKCERT_VERSION + "-linux-arm"
4848
else return "mkcert-" + MKCERT_VERSION + "-linux-amd64"
49-
/* falls through */
49+
/* falls through */
5050
case "win32":
5151
return "mkcert-" + MKCERT_VERSION + "-windows-amd64.exe"
5252
default:
@@ -72,39 +72,43 @@ function download(url, path) {
7272
}
7373

7474
// execute the binary executable to generate the certificates
75-
function mkcert(appDataPath, exe, domain) {
75+
async function mkcert(appDataPath, exe, domain) {
7676
// fix problems with spaces
7777
/* istanbul ignore next: platform dependent */
78-
const ensureValidPath = function(path) {
78+
const escapeSpaces = function(path) {
79+
// escape spaces (not already escaped)
80+
if (process.platform === "darwin" || process.platform === "linux")
81+
return path.replace(/(?<!\\) /g, "\\ ")
7982
// use apex on Windows
8083
if (process.platform === "win32")
8184
return "\"" + path + "\""
82-
// escape spaces in Mac OS
83-
if (process.platform === "darwin")
84-
return path.replace(/ /g, "\\ ")
8585
return path
8686
}
8787

88-
const logPath = ensureValidPath(path.join(appDataPath, "mkcert.log"))
89-
const errPath = ensureValidPath(path.join(appDataPath, "mkcert.err"))
90-
const exePath = ensureValidPath(path.join(appDataPath, exe))
91-
const crtPath = ensureValidPath(path.join(appDataPath, domain + ".crt"))
92-
const keyPath = ensureValidPath(path.join(appDataPath, domain + ".key"))
88+
const exePath = escapeSpaces(path.join(appDataPath, exe))
89+
const crtPath = escapeSpaces(path.join(appDataPath, domain + ".crt"))
90+
const keyPath = escapeSpaces(path.join(appDataPath, domain + ".key"))
91+
const cmd = `${exePath} -install -cert-file ${crtPath}` +
92+
` -key-file ${keyPath} ${domain}`
93+
94+
// sleep on windows due to issue #28
95+
/* istanbul ignore if: cannot be tested */
96+
if (process.platform === "win32")
97+
await new Promise(resolve => setTimeout(resolve, 3000))
9398

94-
const cmd = exePath + " -install -cert-file " + crtPath +
95-
" -key-file " + keyPath + " " + domain
9699
return new Promise((resolve, reject) => {
97100
console.log("Running mkcert to generate certificates...")
101+
// run the mkcert command
98102
exec(cmd, (err, stdout, stderr) => {
99-
// log
100-
const errFun = err => {
101-
/* istanbul ignore if: cannot be tested */
102-
if (err) console.error(err)
103-
}
104-
fs.writeFile(logPath, stdout, errFun)
105-
fs.writeFile(errPath, stderr, errFun)
106103
/* istanbul ignore if: cannot be tested */
107-
if (err) reject(err)
104+
if (stdout) console.log(stdout)
105+
/* istanbul ignore next: cannot be tested */
106+
if (stderr) console.error(stderr)
107+
/* istanbul ignore if: cannot be tested */
108+
if (err) {
109+
console.error(err)
110+
reject(err)
111+
}
108112
resolve()
109113
})
110114
})
@@ -141,7 +145,7 @@ async function getCerts(customDomain = undefined) {
141145
if (process.pkg) checkUpdates()
142146
// check if a reinstall is forced or needed by a mkcert update
143147
if (process.env.REINSTALL ||
144-
!fs.existsSync(path.join(certPath, getExe())))
148+
!fs.existsSync(path.join(certPath, getExe())))
145149
await generate(certPath, domain)
146150
try {
147151
return {
@@ -152,7 +156,7 @@ async function getCerts(customDomain = undefined) {
152156
/* istanbul ignore else: should never occur */
153157
if (certPath !== CERT_PATH) {
154158
console.error("Cannot find localhost.key and localhost.crt in the" +
155-
" specified path: " + certPath)
159+
" specified path: " + certPath)
156160
process.exit(1)
157161
} else {
158162
// Missing certificates (first run)

0 commit comments

Comments
 (0)