UNPKG

dizipal-cli

Version:
86 lines (85 loc) 2.78 kB
import { fileURLToPath } from 'url'; import { request } from 'undici'; import fs from 'fs/promises'; import { Logil } from 'logil'; import config from './config.js'; import * as path from 'path'; import os from 'os'; /*** VARIABLES ***/ // @ts-ignore: no problem export const __filename = fileURLToPath(import.meta.url); export const __dirname = path.dirname(__filename); export const __baseapi = "https://raw.githubusercontent.com/dizipaltv/api/main/dizipal.json"; export const __configdir = path.join(os.homedir(), ".config"); export const __configfile = path.join(__configdir, "dizipal.json"); export let __baseconfig = { latestCLI: undefined, currentSiteURL: undefined, firstProcess: 0 }; /*** LOGIL ***/ export const log = new Logil({ prefix: "izipal", icon: "ↇ", level: 0 }); /*** METADATA ***/ export async function Metadata() { try { const data = await fs.readFile(path.join(__dirname, "..", "package.json"), "utf-8"); return JSON.parse(data); } catch (error) { log.error("An error occurred while reading the package.json file:", error.message); throw error; } } /*** CHECKER ***/ class Checker { async check_config_dir() { await fs.access(__configdir, fs.constants.F_OK) .then(() => { }) .catch(async () => { await fs.mkdir(__configdir); }); } async check_config_file() { let file_is_here = false; await fs.access(__configfile, fs.constants.F_OK) .then(() => { file_is_here = true; }) .catch(async () => { file_is_here = false; await config.update(); }); return file_is_here; } async check_first_process() { if (__baseconfig.firstProcess === 0) { await this.#fetch_api(); __baseconfig.firstProcess += 1; } } async #fetch_api() { try { const { statusCode, body } = await request(__baseapi); if (statusCode === 200) { if (body) { const inner = await body.json(); // @ts-ignore: dont problem! if (typeof inner === "object" && typeof inner.currentSiteURL === "string" && typeof inner.latestCLI === "string") { // @ts-ignore: dont problem! __baseconfig.currentSiteURL = inner.currentSiteURL; // @ts-ignore: dont problem! __baseconfig.latestCLI = inner.latestCLI; } } } } catch (err) { log.error("Something went wrong! on the fetching api."); } } } export const checker = new Checker();