UNPKG

locadot

Version:

Secure your local development environment with HTTPS and custom domains like dev.localhost.

117 lines (116 loc) 3.89 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const file_1 = __importDefault(require("../utils/file")); const logger_1 = __importDefault(require("../utils/logger")); const path_1 = __importDefault(require("path")); class locadotFile { static async createLockFile(processId) { await file_1.default.writeFileSync("LOCK_FILE", processId); } static async getProcessId() { try { const lockFile = await file_1.default.readFileSync("LOCK_FILE"); return parseInt(lockFile).toString(); } catch (err) { await file_1.default.removeFileSync("LOCK_FILE"); return; } } static async deleteLockFile() { await file_1.default.removeFileSync("LOCK_FILE"); } static async getRegistry() { try { return JSON.parse(await file_1.default.readFileSync("REGISTRY_FILE")); } catch (error) { logger_1.default.error(error); return {}; } } static async addRegistry(domain, port) { const registry = await locadotFile.getRegistry(); registry[domain] = port; file_1.default.writeFileSync("REGISTRY_FILE", JSON.stringify(registry)); } static async removeAllRegistry() { file_1.default.writeFileSync("REGISTRY_FILE", "{}"); } static async updateRegistry(domain, port) { const registry = await locadotFile.getRegistry(); registry[domain] = port; file_1.default.writeFileSync("REGISTRY_FILE", JSON.stringify(registry)); } static async deleteRegistry(domain) { const registry = await locadotFile.getRegistry(); delete registry[domain]; file_1.default.writeFileSync("REGISTRY_FILE", JSON.stringify(registry)); } static async watchRegistry(callBackOnChange) { return file_1.default.watchFileWithInit("REGISTRY_FILE", () => callBackOnChange?.()); } static async getLogs() { try { return await file_1.default.readFileSync("LOGS"); } catch (error) { file_1.default.removeFileSync("LOGS"); return ""; } } static async clearLogs() { file_1.default.writeFileSync("LOGS", ""); } static async watchLogs() { file_1.default.tailFile("LOGS"); } static getStartProxyFile() { if (process.env.NODE_ENV === "production") { const filePath = path_1.default.join(__dirname, "..", "core.js"); return { command: "node", path: [filePath], }; } const filePath = path_1.default.join(__dirname, "../../", "dist", "core.js"); return { command: "node", path: [filePath], }; } static async destroy(watcher) { try { const pid = await locadotFile.getProcessId(); await locadotFile.deleteLockFile(); await locadotFile.removeAllRegistry(); await locadotFile.clearLogs(); await watcher?.close(); if (pid) { process.kill(parseInt(pid)); } } catch (error) { } } static async softDestroy(watcher) { try { const pid = await locadotFile.getProcessId(); await locadotFile.deleteLockFile(); await locadotFile.clearLogs(); await watcher?.close(); if (pid) { try { process.kill(parseInt(pid)); } catch (error) { } } } catch (error) { logger_1.default.error(error); } } } exports.default = locadotFile;