UNPKG

fallout-utility

Version:
106 lines 4.53 kB
"use strict"; 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isValidIPv4 = exports.createReadFileAsync = exports.createReadFile = exports.path = exports.isDebugging = exports.getOperatingSystem = exports.OS = void 0; const node_fs_1 = require("node:fs"); const promises_1 = require("node:fs/promises"); const node_path_1 = __importStar(require("node:path")); const node_inspector_1 = __importDefault(require("node:inspector")); const node_os_1 = __importDefault(require("node:os")); var OS; (function (OS) { OS["WINDOWS"] = "win32"; OS["LINUX"] = "linux"; OS["MACOS"] = "darwin"; OS["ANDROID"] = "android"; OS["OPENBSD"] = "openbsd"; OS["FREEBSD"] = "freebsd"; OS["OTHER"] = "other"; })(OS || (exports.OS = OS = {})); /** * Get operation system */ function getOperatingSystem() { switch (node_os_1.default.platform()) { case 'win32': return OS.WINDOWS; case 'linux': return OS.LINUX; case 'darwin': return OS.MACOS; case 'android': return OS.ANDROID; case 'openbsd': return OS.OPENBSD; case 'freebsd': return OS.FREEBSD; default: return OS.OTHER; } } exports.getOperatingSystem = getOperatingSystem; /** * Checks if debug mode is enabled * @experimental */ function isDebugging() { return !!node_inspector_1.default.url() || /--debug|--inspect/g.test(process.execArgv.join('')); } exports.isDebugging = isDebugging; /** * @deprecated This is a dumb idea (Already implemented in path module) */ exports.path = getOperatingSystem() === OS.WINDOWS ? node_path_1.default.win32 : node_path_1.default.posix; function createReadFile(filePath, defaultContent, options) { if (!(0, node_fs_1.existsSync)(filePath)) { (0, node_fs_1.mkdirSync)((0, node_path_1.dirname)(filePath), { recursive: true }); (0, node_fs_1.writeFileSync)(filePath, options?.encodeFileData ? options?.encodeFileData(defaultContent) : String(defaultContent), options?.encoding); } const fileData = (0, node_fs_1.readFileSync)(filePath, options?.encoding); return options?.formatReadData ? options.formatReadData(fileData, defaultContent) : fileData; } exports.createReadFile = createReadFile; async function createReadFileAsync(filePath, defaultContent, options) { if (!(await (0, promises_1.stat)(filePath).catch(() => false))) { await (0, promises_1.mkdir)((0, node_path_1.dirname)(filePath), { recursive: true }); await (0, promises_1.writeFile)(filePath, options?.encodeFileData ? await Promise.resolve(options?.encodeFileData(defaultContent)) : String(defaultContent), options?.encoding); } const fileData = await (0, promises_1.readFile)(filePath, options?.encoding); return options?.formatReadData ? Promise.resolve(options.formatReadData(fileData, defaultContent)) : fileData; } exports.createReadFileAsync = createReadFileAsync; /** * Checks if a string is a valid IPv4 * @param ip String to check * @deprecated This is crazy */ function isValidIPv4(ip) { return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ip); } exports.isValidIPv4 = isValidIPv4; //# sourceMappingURL=system.js.map