|
1 | 1 | #!/usr/bin/env node |
2 | | -//@ts-check |
3 | | -import { homedir } from "os"; |
4 | | -import { join } from "path"; |
5 | 2 | import { existsSync } from "fs"; |
6 | 3 | import { mkdir, writeFile } from "fs/promises"; |
7 | 4 | import minimist from "minimist"; |
8 | 5 | import pc from "picocolors"; |
9 | | -import mkcert from "vite-plugin-mkcert"; |
| 6 | +import { DATA_DIR, resolvePath } from "./src/utils.js"; |
| 7 | +import { createCertificate } from "./src/index.js"; |
10 | 8 |
|
11 | 9 | /** |
12 | 10 | * Init, read variables and create folders |
13 | 11 | */ |
14 | | -const defaultOutDir = join(homedir(), ".mkcert-cli", "certs"); |
| 12 | +const defaultOutDir = resolvePath("certs", DATA_DIR); |
15 | 13 | const argv = minimist(process.argv.slice(2)); |
16 | 14 | const cwd = process.cwd(); |
17 | 15 |
|
18 | 16 | const force = argv.f ?? false; |
19 | 17 | const verbose = argv.v ?? false; |
20 | | -const outDir = argv.outDir ? join(cwd, argv.outDir) : defaultOutDir; |
| 18 | +const outDir = argv.outDir ? resolvePath(argv.outDir, cwd) : defaultOutDir; |
21 | 19 | const hosts = argv.host ? (Array.isArray(argv.host) ? argv.host : [argv.host]) : []; |
22 | 20 | const certFile = argv.cert ?? "dev.cert"; |
23 | 21 | const keyFile = argv.key ?? "dev.key"; |
24 | | -const certFilePath = join(outDir, certFile); |
25 | | -const keyFilePath = join(outDir, keyFile); |
| 22 | +const certFilePath = resolvePath(certFile, outDir); |
| 23 | +const keyFilePath = resolvePath(keyFile, outDir); |
26 | 24 |
|
27 | 25 | console.log(` |
28 | 26 | Running ${pc.green(`${pc.bold("mkcert-cli")}`)}... |
@@ -54,20 +52,10 @@ if (!writeFiles) { |
54 | 52 | } |
55 | 53 |
|
56 | 54 | try { |
57 | | - /** @type { any } */ |
58 | | - const { config: installMkCert } = mkcert({ |
59 | | - force, |
60 | | - hosts: hosts.length ? hosts : undefined, |
61 | | - // autoUpgrade: boolean |
62 | | - // source?: SourceType; |
63 | | - // mkcertPath?: string; |
64 | | - }); |
65 | | - const { |
66 | | - server: { https: { key, cert } }, |
67 | | - } = await installMkCert({}); |
| 55 | + const { cert, key } = await createCertificate({ force, autoUpgrade: false, keyFilePath, certFilePath }); |
68 | 56 | await writeFile(keyFilePath, key, { encoding: "utf-8" }); |
69 | 57 | await writeFile(certFilePath, cert, { encoding: "utf-8" }); |
70 | | -} catch (writeErr) { |
| 58 | +} catch (/** @type {any}*/ writeErr) { |
71 | 59 | console.error(writeErr.toString()); |
72 | 60 | process.exit(1); |
73 | 61 | } |
|
0 commit comments