Skip to content

Commit a8b06aa

Browse files
authored
Replace axios with native https (#7)
* update readme with all flags * add pkgVersion export from utils * Support `-h` for help section and `--version` Add shorthands for all options * remove axios add naive redirect-handling in request add default headers and ok user-agent * add tests
1 parent 9450180 commit a8b06aa

File tree

10 files changed

+173
-189
lines changed

10 files changed

+173
-189
lines changed

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Create locally trusted development certificates in default folder `$HOME/.mkcert
3333

3434
## Options
3535

36-
### `--outDir`
36+
### `--outDir` or `-o`
3737

3838
Explicitly define output directory for files
3939

@@ -45,7 +45,7 @@ Explicitly define output directory for files
4545

4646
```
4747

48-
### `--cert` & `--key`
48+
### `--cert` / `-c` & `--key` / `-k`
4949

5050
Set file names, default to `dev.cert` and `dev.key`
5151

@@ -70,6 +70,30 @@ To pass multiple values, just pass several named args.
7070
# => Created "dev.cert" and "dev.key" for ["localhost", "my-site.local"]
7171
```
7272

73+
### Other flags
74+
75+
#### `--help` / `-h`: Display help text
76+
77+
```sh
78+
npx -y mkcert-cli -h
79+
```
80+
81+
#### `-f` / `--force`: Force
82+
83+
Force regenerate certificate files
84+
85+
```sh
86+
npx -y mkcert-cli -f
87+
```
88+
89+
#### `-u` / `--upgrade`: Auto upgrade mkcert bin
90+
91+
Install latest version of mkcert that's available before generating certificates.
92+
93+
```sh
94+
npx -y mkcert-cli -u
95+
```
96+
7397
## Thanks
7498

7599
- [vite-plugin-mkcert][vite-plugin-mkcert]

cli.js

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,82 @@ import { existsSync } from "fs";
33
import { mkdir, writeFile } from "fs/promises";
44
import minimist from "minimist";
55
import pc from "picocolors";
6-
import { DATA_DIR, resolvePath } from "./src/utils.js";
6+
import { DATA_DIR, resolvePath, readFile, pkgVersion } from "./src/utils.js";
77
import { createCertificate } from "./src/index.js";
88

99
/**
1010
* Init, read variables and create folders
1111
*/
12-
const defaultOutDir = resolvePath("certs", DATA_DIR);
12+
const defaults = {
13+
dir: resolvePath("certs", DATA_DIR),
14+
key: 'dev.key',
15+
cert: 'dev.cert'
16+
}
17+
1318
const argv = minimist(process.argv.slice(2));
1419
const cwd = process.cwd();
1520

16-
const force = argv.f ?? false;
21+
// Print version and exit
22+
if (argv.version) {
23+
console.log(pkgVersion);
24+
process.exit();
25+
}
26+
27+
// Display help and exit
28+
if (argv.h || argv.help) {
29+
30+
console.log(`
31+
${pc.bold('Usage')}
32+
$ npx mkcert-cli <options>
33+
34+
${pc.bold('Options')}
35+
${pc.blue('--outDir, -o')} Certificates output dir, defaults to ${pc.bold(defaults.dir)}
36+
${pc.blue('--host')} Add custom host, defaults to ${pc.bold('[localhost, ...IPv4]')}
37+
${pc.blue('--keyFile, -k')} Name of key file, defaults to "${pc.bold(defaults.key)}"
38+
${pc.blue('--certFile, -c')} Name of cert file, defaults to "${pc.bold(defaults.cert)}"
39+
${pc.blue('--force, -f')} Force recreate certificates
40+
${pc.blue('--upgrade, -u')} Upgrade mkcert binary if new version is available
41+
${pc.blue('--dryRun, -d')} Print all parameters without doing anything
42+
43+
${pc.bold('Examples')}
44+
$ npx mkcert-cli --host my-domain.local --host my-other-domain.local --outDir .
45+
$ npx mkcert-cli -c myCert.pem -k myKey.key
46+
$ npx mkcert-cli -fu -o .
47+
48+
`);
49+
process.exit();
50+
}
51+
1752
const verbose = argv.v ?? false;
18-
const outDir = argv.outDir ? resolvePath(argv.outDir, cwd) : defaultOutDir;
19-
const hosts = argv.host ? (Array.isArray(argv.host) ? argv.host : [argv.host]) : [];
20-
const certFile = argv.cert ?? "dev.cert";
21-
const keyFile = argv.key ?? "dev.key";
53+
54+
const host = argv.host;
55+
const force = (argv.force || argv.f) ?? false;
56+
const autoUpgrade = (argv.upgrade || argv.u) ?? false;
57+
const outDir = (argv.outDir || argv.o) ? resolvePath(argv.outDir || argv.o, cwd) : defaults.dir;
58+
const hosts = host
59+
? Array.isArray(host)
60+
? host
61+
: [host]
62+
: [];
63+
const certFile = (argv.c || argv.cert) ?? defaults.cert;
64+
const keyFile = (argv.k || argv.key) ?? defaults.key;
2265
const certFilePath = resolvePath(certFile, outDir);
2366
const keyFilePath = resolvePath(keyFile, outDir);
67+
const dryRun = (argv.d || argv.dryRun) ?? false;
2468

25-
console.log(`
26-
Running ${pc.green(`${pc.bold("mkcert-cli")}`)}...
27-
`);
69+
console.log(`\nRunning ${pc.green(`${pc.bold("mkcert-cli")}`)} (${pkgVersion})\n`);
2870

29-
verbose &&
71+
(dryRun || verbose) &&
3072
console.log(`${pc.bold("With options:")}
3173
- ${pc.blue("cwd")}: ${pc.yellow(cwd)}
3274
- ${pc.blue("outDir")}: ${pc.yellow(outDir)}
33-
- ${pc.blue("host")}: ${JSON.stringify(hosts)}
34-
- ${pc.blue("force")}: ${force}
75+
- ${pc.blue("hosts")}: ${JSON.stringify(hosts)}
3576
- ${pc.blue("cert")}: ${certFile}
3677
- ${pc.blue("key")}: ${keyFile}
78+
- ${pc.blue("force")}: ${force}
79+
- ${pc.blue("autoUpgrade")}: ${autoUpgrade}
3780
`);
81+
if (dryRun) { process.exit() }
3882

3983
if (!existsSync(outDir)) {
4084
await mkdir(outDir, { recursive: true });
@@ -46,13 +90,20 @@ if (!existsSync(outDir)) {
4690
const filesExist = existsSync(certFilePath) && existsSync(keyFilePath);
4791
const writeFiles = force || !filesExist;
4892
if (!writeFiles) {
49-
console.log(`🎉 Files "${pc.magenta(certFile)}" and "${pc.magenta(keyFile)}" already exist
93+
console.log(`🎉 Files "${pc.magenta(certFile)}" and "${pc.magenta(
94+
keyFile
95+
)}" already exist
5096
in ${pc.yellow(outDir)}`);
5197
process.exit(0);
5298
}
5399

54100
try {
55-
const { cert, key } = await createCertificate({ force, autoUpgrade: false, keyFilePath, certFilePath });
101+
const { cert, key } = await createCertificate({
102+
force,
103+
autoUpgrade,
104+
keyFilePath,
105+
certFilePath,
106+
});
56107
await writeFile(keyFilePath, key, { encoding: "utf-8" });
57108
await writeFile(certFilePath, cert, { encoding: "utf-8" });
58109
} catch (/** @type {any}*/ writeErr) {

0 commit comments

Comments
 (0)