@kotori-bot/tools
Version:
Tools For Kotori
124 lines (123 loc) • 4.97 kB
JavaScript
/**
* @Package @kotori-bot/tools
* @Version 1.5.2
* @Author Hotaru <me@hotaru.icu>
* @Copyright 2024-2025 Hotaru. All rights reserved.
* @License BAN-ZHINESE-USING
* @Link https://github.com/kotorijs/kotori
* @Date 17:26:22
*/
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var function_exports = {};
__export(function_exports, {
configFileType: () => configFileType,
createConfig: () => createConfig,
executeCommand: () => executeCommand,
loadConfig: () => loadConfig,
saveConfig: () => saveConfig,
supportTs: () => supportTs
});
module.exports = __toCommonJS(function_exports);
var import_node_fs = __toESM(require("node:fs"));
var import_node_path = __toESM(require("node:path"));
var import_toml = __toESM(require("@iarna/toml"));
var import_yaml = __toESM(require("yaml"));
var import_node_child_process = require("node:child_process");
const configFileType = ["json", "toml", "yaml", "yml", "text"];
function loadConfig(filename, type = "json", init = void 0) {
const dirname = import_node_path.default.dirname(filename);
if (!import_node_fs.default.existsSync(dirname)) import_node_fs.default.mkdirSync(dirname, { recursive: true });
const isExists = import_node_fs.default.existsSync(filename);
const defaultValue = type === "text" ? String(init ?? "") : JSON.stringify(init ?? {});
if (!isExists && init !== void 0) import_node_fs.default.writeFileSync(filename, defaultValue);
const data = isExists ? import_node_fs.default.readFileSync(filename).toString() : defaultValue;
if (["yaml", "yml"].includes(type)) return import_yaml.default.parse(data);
if (type === "toml") return import_toml.default.parse(data);
if (type === "text") return data;
return JSON.parse(data);
}
function saveConfig(filename, data, type = "json") {
let content = "";
if (typeof data === "object") {
switch (type) {
case "json":
content = JSON.stringify(data, null, 2);
break;
case "yaml":
content = import_yaml.default.stringify(data);
break;
case "toml":
content = import_toml.default.stringify(data);
break;
default:
content = String(data);
}
} else {
content = String(data);
}
const dirname = import_node_path.default.dirname(filename);
if (!import_node_fs.default.existsSync(dirname)) import_node_fs.default.mkdirSync(dirname, { recursive: true });
import_node_fs.default.writeFileSync(filename, content);
}
function createConfig(filename, data, type = "json") {
let content = "";
if (!import_node_fs.default.existsSync(filename)) {
if (type === "json") content = JSON.stringify(data);
if (type === "yaml") content = import_yaml.default.stringify(data);
import_node_fs.default.writeFileSync(filename, content);
}
}
function supportTs() {
try {
require(import_node_path.default.resolve(__dirname, "../../testing.ts"));
return true;
} catch {
return false;
}
}
function executeCommand(command, options = { cwd: process.cwd() }, callback = (err, stdout, stderr) => {
if (err) console.error(err);
if (stdout) process.stdout.write(stdout);
if (stderr) process.stderr.write(stderr);
}) {
const child = (0, import_node_child_process.exec)(command, options, (error) => callback(error));
if (child.stdout) child.stdout.on("data", (data) => callback(void 0, data));
if (child.stderr) child.stderr.on("data", (data) => callback(void 0, void 0, data));
return child;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
configFileType,
createConfig,
executeCommand,
loadConfig,
saveConfig,
supportTs
});