106 lines
5.0 KiB
JavaScript
106 lines
5.0 KiB
JavaScript
"use strict";
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
// Licensed under the MIT license.
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
});
|
|
};
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.generateCertificates = void 0;
|
|
const fs_1 = __importDefault(require("fs"));
|
|
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
const mkcert = __importStar(require("mkcert"));
|
|
const path_1 = __importDefault(require("path"));
|
|
const defaults = __importStar(require("./defaults"));
|
|
/* global console */
|
|
/* Generate operation will check if there is already valid certificate installed.
|
|
if yes, then this operation will be no op.
|
|
else, new certificates are generated and installed if --install was provided.
|
|
*/
|
|
function generateCertificates(caCertificatePath = defaults.caCertificatePath, localhostCertificatePath = defaults.localhostCertificatePath, localhostKeyPath = defaults.localhostKeyPath, daysUntilCertificateExpires = defaults.daysUntilCertificateExpires, domains = defaults.domain) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
try {
|
|
fs_extra_1.default.ensureDirSync(path_1.default.dirname(caCertificatePath));
|
|
fs_extra_1.default.ensureDirSync(path_1.default.dirname(localhostCertificatePath));
|
|
fs_extra_1.default.ensureDirSync(path_1.default.dirname(localhostKeyPath));
|
|
}
|
|
catch (err) {
|
|
throw new Error(`Unable to create the directory.\n${err}`);
|
|
}
|
|
const cACertificateInfo = {
|
|
countryCode: defaults.countryCode,
|
|
locality: defaults.locality,
|
|
organization: defaults.certificateName,
|
|
state: defaults.state,
|
|
validity: daysUntilCertificateExpires,
|
|
};
|
|
let caCertificate;
|
|
try {
|
|
caCertificate = yield mkcert.createCA(cACertificateInfo);
|
|
}
|
|
catch (err) {
|
|
throw new Error(`Unable to generate the CA certificate.\n${err}`);
|
|
}
|
|
const localhostCertificateInfo = {
|
|
ca: caCertificate,
|
|
domains,
|
|
validity: daysUntilCertificateExpires,
|
|
};
|
|
let localhostCertificate;
|
|
try {
|
|
localhostCertificate = yield mkcert.createCert(localhostCertificateInfo);
|
|
}
|
|
catch (err) {
|
|
throw new Error(`Unable to generate the localhost certificate.\n${err}`);
|
|
}
|
|
try {
|
|
if (!fs_1.default.existsSync(caCertificatePath)) {
|
|
fs_1.default.writeFileSync(`${caCertificatePath}`, caCertificate.cert);
|
|
fs_1.default.writeFileSync(`${localhostCertificatePath}`, localhostCertificate.cert);
|
|
fs_1.default.writeFileSync(`${localhostKeyPath}`, localhostCertificate.key);
|
|
}
|
|
}
|
|
catch (err) {
|
|
throw new Error(`Unable to write generated certificates.\n${err}`);
|
|
}
|
|
if (caCertificatePath === defaults.caCertificatePath) {
|
|
console.log("The developer certificates have been generated in " + defaults.certificateDirectory);
|
|
}
|
|
else {
|
|
console.log("The developer certificates have been generated.");
|
|
}
|
|
});
|
|
}
|
|
exports.generateCertificates = generateCertificates;
|
|
//# sourceMappingURL=generate.js.map
|