-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
117 lines (99 loc) · 3.42 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env node
import { existsSync } from 'fs';
import { mkdir, writeFile } from 'fs/promises';
import minimist from 'minimist';
import pc from 'picocolors';
import { createCertificate } from './src/index.js';
import { DATA_DIR, pkgVersion, resolvePath } from './src/utils.js';
/**
* Init, read variables and create folders
*/
const defaults = {
dir: resolvePath('certs', DATA_DIR),
key: 'dev.key',
cert: 'dev.cert',
};
const argv = minimist(process.argv.slice(2));
const cwd = process.cwd();
// Print version and exit
if (argv.version) {
console.log(pkgVersion);
process.exit();
}
// Display help and exit
if (argv.h || argv.help) {
console.log(`
${pc.bold('Usage')}
$ npx mkcert-cli <options>
${pc.bold('Options')}
${pc.blue('--outDir, -o')} Certificates output dir, defaults to ${pc.bold(defaults.dir)}
${pc.blue('--host')} Add custom host, defaults to ${pc.bold('[localhost, ...IPv4]')}
${pc.blue('--keyFile, -k')} Name of key file, defaults to "${pc.bold(defaults.key)}"
${pc.blue('--certFile, -c')} Name of cert file, defaults to "${pc.bold(defaults.cert)}"
${pc.blue('--force, -f')} Force recreate certificates
${pc.blue('--upgrade, -u')} Upgrade mkcert binary if new version is available
${pc.blue('--dryRun, -d')} Print all parameters without doing anything
${pc.bold('Examples')}
$ npx mkcert-cli --host my-domain.local --host my-other-domain.local --outDir .
$ npx mkcert-cli -c myCert.pem -k myKey.key
$ npx mkcert-cli -fu -o .
`);
process.exit();
}
const verbose = argv.v ?? false;
const host = argv.host;
const force = (argv.force || argv.f) ?? false;
const autoUpgrade = (argv.upgrade || argv.u) ?? false;
const outDir = argv.outDir || argv.o ? resolvePath(argv.outDir || argv.o, cwd) : defaults.dir;
const hosts = host ? (Array.isArray(host) ? host : [host]) : [];
const certFile = (argv.c || argv.cert) ?? defaults.cert;
const keyFile = (argv.k || argv.key) ?? defaults.key;
const certFilePath = resolvePath(certFile, outDir);
const keyFilePath = resolvePath(keyFile, outDir);
const dryRun = (argv.d || argv.dryRun) ?? false;
console.log(`\nRunning ${pc.green(`${pc.bold('mkcert-cli')}`)} (${pkgVersion})\n`);
(dryRun || verbose) &&
console.log(`${pc.bold('With options:')}
- ${pc.blue('cwd')}: ${pc.yellow(cwd)}
- ${pc.blue('outDir')}: ${pc.yellow(outDir)}
- ${pc.blue('hosts')}: ${JSON.stringify(hosts)}
- ${pc.blue('cert')}: ${certFile}
- ${pc.blue('key')}: ${keyFile}
- ${pc.blue('force')}: ${force}
- ${pc.blue('autoUpgrade')}: ${autoUpgrade}
`);
if (dryRun) {
process.exit();
}
if (!existsSync(outDir)) {
await mkdir(outDir, { recursive: true });
}
/**
* Create certificates
*/
const filesExist = existsSync(certFilePath) && existsSync(keyFilePath);
const writeFiles = force || !filesExist;
if (!writeFiles) {
console.log(`🎉 Files "${pc.magenta(certFile)}" and "${pc.magenta(keyFile)}" already exist
in ${pc.yellow(outDir)}`);
process.exit(0);
}
try {
const { cert, key } = await createCertificate(
{
force,
autoUpgrade,
keyFilePath,
certFilePath,
},
hosts
);
await writeFile(keyFilePath, key, { encoding: 'utf-8' });
await writeFile(certFilePath, cert, { encoding: 'utf-8' });
} catch (/** @type {any}*/ writeErr) {
console.error(writeErr.toString());
process.exit(1);
}
console.log(`🎉 Created "${pc.magenta(certFile)}" and "${pc.magenta(keyFile)}"
in ${pc.yellow(outDir)}
`);