@knapsack/https
Version:
Automatically provisions and installs locally-trusted TLS certificates for Node.js https servers (including Express.js, etc.) using mkcert.
36 lines • 1.97 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setupLocalCertAuthority = setupLocalCertAuthority;
const fs_extra_1 = __importDefault(require("fs-extra"));
const chalk_1 = __importDefault(require("chalk"));
const child_process_1 = __importDefault(require("child_process"));
const log_1 = require("../log");
const constants_1 = require("../constants");
const { exec } = child_process_1.default;
async function setupLocalCertAuthority({ mkcertBinary, certFilePath, keyFilePath, }) {
return new Promise((resolve) => {
// mkcert uses the CAROOT environment variable to know where to create/find the certificate authority.
// We also pass the rest of the system environment to the spawned processes.
// Create the local certificate authority.
log_1.log.info(' 📜 ❨auto-encrypt-localhost❩ Creating local certificate authority (local CA) using mkcert…');
exec(`${mkcertBinary} -install`, constants_1.mkcertProcessOptions, (error) => {
if (error) {
log_1.log.error(chalk_1.default.red(`${error}. Cleaning up so we can try again later...`));
// auto-cleanup so re-running knapsack start will try to re-configure
if (fs_extra_1.default.existsSync(certFilePath)) {
fs_extra_1.default.removeSync(certFilePath);
}
if (fs_extra_1.default.existsSync(keyFilePath)) {
fs_extra_1.default.removeSync(keyFilePath);
}
return resolve(false);
}
log_1.log.info(chalk_1.default.green(' 📜 ❨auto-encrypt-localhost❩ Local certificate authority created.'));
return resolve(true);
});
});
}
//# sourceMappingURL=setup-local-cert-auth.js.map