UNPKG

lxrbckl

Version:

CRUD functionality to enhance readability and abstraction in projects, for both local and remote file management.

421 lines (412 loc) 11.1 kB
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, { get: (a, b) => (typeof require !== "undefined" ? require : a)[b] }) : x)(function(x) { if (typeof require !== "undefined") return require.apply(this, arguments); throw Error('Dynamic require of "' + x + '" is not supported'); }); var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; // node_modules/tsup/assets/esm_shims.js import { fileURLToPath } from "url"; import path from "path"; var import_meta = {}; var getFilename = () => fileURLToPath(import_meta.url); var __filename = /* @__PURE__ */ getFilename(); // src/services/axios.ts var axios = __require("axios"); function axiosGet(url) { return __async(this, null, function* () { try { const response = yield axios.get(url); return response.data; } catch (error) { console.log("Error: ", error, "\nUrl: ", url); } }); } // src/services/local.ts import path2 from "path"; import { mkdir, rmdir, unlink, readdir, readFile, writeFile } from "fs/promises"; function getProjectPath(file = "", delimeter = "/") { const dir = path2.dirname(__filename).split(/[]/); const base = dir.includes("node_modules") ? -4 : -1; return [...dir.slice(0, base), file.split(/[/\\]/)].join(delimeter); } function fileSet(_0, _1) { return __async(this, arguments, function* (data, file, indent = 3, path3 = getProjectPath()) { try { yield writeFile( path3 + file, file.endsWith(".json") ? JSON.stringify(data, null, indent) : data ); } catch (error) { console.log(`Error: ${error} Path: ${path3 + file}`); } }); } function fileGet(_0) { return __async(this, arguments, function* (file, path3 = getProjectPath(), encoding = "utf8") { try { const fin = yield readFile(path3 + file, encoding); return file.endsWith(".json") ? JSON.parse(fin.toString()) : fin; } catch (error) { console.log(`Error: ${error} Path: ${path3 + file}`); } }); } function fileDel(_0) { return __async(this, arguments, function* (file, path3 = getProjectPath()) { try { yield unlink(path3 + file); } catch (error) { console.log(`Error: ${error} Path: ${path3 + file}`); } }); } function dirSet(_0) { return __async(this, arguments, function* (dir, path3 = getProjectPath()) { try { yield mkdir(path3 + dir); } catch (error) { console.log(`Error: ${error} Path: ${path3 + dir}`); } }); } function dirGet(_0) { return __async(this, arguments, function* (dir, path3 = getProjectPath()) { try { return yield readdir(path3 + dir); } catch (error) { console.log(`Error: ${error} Path: ${path3 + dir}`); } }); } function dirDel(_0) { return __async(this, arguments, function* (dir, path3 = getProjectPath()) { try { yield rmdir(path3 + dir); } catch (error) { console.log(`Error: ${error} Path: ${path3 + dir}`); } }); } // src/services/openai.ts import { promisify } from "util"; import { exec } from "child_process"; var openai = class { constructor({ token, model = "gpt-4o", temperature = 1.5 }) { this._token = token; this._model = model; this._temperature = temperature; } query(prompt) { return __async(this, null, function* () { const query = ` curl -X POST https://api.openai.com/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer ${this._token}" -d '{ "model" : "${this._model}", "temperature" : ${this._temperature}, "messages" : [ { "role" : "system", "content" : "You are a helpful assistant." }, { "role" : "user", "content" : "${prompt}" } ] }' `; const execPromise = promisify(exec); const { stdout, stderr } = yield execPromise(query); return JSON.parse(stdout).choices[0].message.content; }); } }; // src/services/octokit.ts import { Octokit } from "@octokit/rest"; var octokit = class { constructor({ token, owner, indent = 3, stringEncoding = "utf8", bufferEncoding = "base64" }) { this._token = token; this._owner = owner; this._indent = indent; this._stringEncoding = stringEncoding; this._bufferEncoding = bufferEncoding; this._octokit = new Octokit({ auth: this._token }); } set owner(newOwner) { this._owner = newOwner; } request(_0) { return __async(this, arguments, function* ({ endpoint, parameters = {}, elementFromProperty = "", propertyFromResult = "data" }) { let response = yield this._octokit.request(endpoint, parameters); switch (elementFromProperty == "") { case true: return response[propertyFromResult]; case false: return response[propertyFromResult].map((i) => i[elementFromProperty]); } }); } endpointFile({ file, method, repository, branch = void 0 }) { let endpoint = ""; endpoint += `${method} /repos/${this._owner}/${repository}/contents/${file}`; endpoint += branch ? `?ref=${branch}` : ""; return endpoint; } repositoryGet(_0) { return __async(this, arguments, function* ({ file, branch, repository, displayError = false, propertyFromResult = "content" }) { let endpoint = this.endpointFile({ file, method: "GET", branch, repository }); try { let data = yield this.request({ endpoint, parameters: { ref: branch } }); switch (propertyFromResult == "content") { case false: return data[propertyFromResult]; case true: let encoding = data.encoding; let buffer = Buffer.from(data.content, encoding); let fileEnding = file.split(".").splice(-1)[0]; let result = buffer.toString(this._stringEncoding); switch (fileEnding) { case "json": return JSON.parse(result); default: return result; } } } catch (error) { displayError ? console.log("Error: ", error) : void 0; return false; } }); } respositorySet(_0) { return __async(this, arguments, function* ({ data, file, branch, repository, retryOnError = 0, displayError = false, commitMessage = "Automated Action" }) { let fileEnding = file.split(".").splice(-1)[0]; switch (fileEnding) { case "json": data = JSON.stringify(data, null, this._indent); break; } let endpoint = this.endpointFile({ file, method: "PUT", branch, repository }); try { yield this._octokit.request(endpoint, { branch, owner: this._owner, message: commitMessage, content: Buffer.from(data).toString(this._bufferEncoding), sha: yield this.repositoryGet({ file, branch, repository, propertyFromResult: "sha", displayError }) }); return true; } catch (error) { displayError ? console.log("Error: ", error) : void 0; if (retryOnError == 0) { return false; } else { return yield this.respositorySet({ data, file, branch, repository, displayError, commitMessage, retryOnError: retryOnError - 1 }); } } }); } }; // src/services/discord.ts import { Client, Routes, TextChannel, IntentsBitField } from "discord.js"; var discord = class { constructor({ guildId, channelId, applicationId, commands = {}, version = "10", intents = [ IntentsBitField.Flags.Guilds, IntentsBitField.Flags.GuildMembers, IntentsBitField.Flags.GuildMessages, IntentsBitField.Flags.MessageContent ] }) { this._version = version; this._intents = intents; this._guildId = guildId; this.commands = commands; this._channelId = channelId; this._applicationId = applicationId; this._client = new Client({ intents: this._intents, rest: { version: this._version } }); } // required // login({ token }) { this._client.login(token); } // required // registerCommands() { this._client.rest.put( Routes.applicationGuildCommands( this._applicationId, this._guildId ), { body: Object.values(this.commands).map((c) => c.context()) } ); } // suggested // registerOnReady(callback) { return __async(this, null, function* () { this._client.on("ready", () => __async(this, null, function* () { yield callback(); })); }); } // suggested // registerInteractionCreate(callback) { return __async(this, null, function* () { this._client.on("interactionCreate", (interaction) => __async(this, null, function* () { yield callback(interaction); })); }); } // optional // registerCommandChoices() { return __async(this, null, function* () { yield Promise.all( Object.values(this.commands).filter((c) => c.registerChoices).map((c) => c.registerChoices()) ); }); } // optional // messageChannel({ content, codeBlock = "", isInline = false, isSpoiler = false }) { try { if (content.length > 0) { let channel = this._client.channels.cache.get(this._channelId); if (channel instanceof TextChannel) { content = isInline ? `\` ${content} \`` : content; content = isSpoiler ? `|| ${content} ||` : content; content = codeBlock ? `\`\`\`${codeBlock} ${content}\`\`\`` : content; channel.send(content); } } else { console.log("There is no content."); } } catch (error) { console.log("Error: ", error, "\nChannelId: ", this._channelId); } } // optional // terminate() { this._client.destroy(); } }; export { axiosGet, dirDel, dirGet, dirSet, discord, fileDel, fileGet, fileSet, getProjectPath, octokit, openai };