32 lines
959 B
JavaScript
32 lines
959 B
JavaScript
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import shell from 'shelljs';
|
|
import fsExtra from 'fs-extra';
|
|
|
|
|
|
export function getDefaultHosts() {
|
|
const interfaceDict = os.networkInterfaces();
|
|
const addresses = [];
|
|
for (const key in interfaceDict) {
|
|
const interfaces = interfaceDict[key];
|
|
if (interfaces) {
|
|
for (const item of interfaces) {
|
|
const family = item.family;
|
|
if (family === 'IPv4') {
|
|
addresses.push(item.address);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return ['localhost', ...addresses];
|
|
}
|
|
|
|
export function generateCertificate() {
|
|
const certPath = path.resolve(process.cwd(), 'certs');
|
|
fsExtra.ensureDirSync(certPath);
|
|
shell.cd(certPath);
|
|
shell.exec(`mkcert -key-file server.key -cert-file server.crt ${getDefaultHosts().join(' ')}`);
|
|
console.log('new certificate generated successfully!');
|
|
}
|
|
|
|
generateCertificate(); |