utils/lib/cjs/utils.js

50 lines
5.6 KiB
JavaScript
Raw Permalink Normal View History

2024-01-27 21:29:33 +00:00
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
2024-01-28 01:42:43 +00:00
exports.readJsonIfExists = exports.log = exports.getTime = exports.convertCamelToUpperSnakeCase = void 0;
2024-01-27 21:29:33 +00:00
const chalk_1 = __importDefault(require("chalk"));
const fs_1 = __importDefault(require("fs"));
const json5_1 = __importDefault(require("json5"));
const convertCamelToUpperSnakeCase = (str) => str.replace(/[A-Z]/g, (letter) => `_${letter}`).toUpperCase();
exports.convertCamelToUpperSnakeCase = convertCamelToUpperSnakeCase;
const getTime = () => {
const now = new Date(), tzo = -now.getTimezoneOffset(), dif = tzo >= 0 ? "+" : "-", pad = (num) => {
const norm = Math.floor(Math.abs(num));
return `${norm < 10 ? "0" : ""}${norm}`;
};
return [
now.getFullYear(),
"-",
pad(now.getMonth() + 1),
"-",
pad(now.getDate()),
"T",
pad(now.getHours()),
":",
pad(now.getMinutes()),
":",
pad(now.getSeconds()),
dif,
pad(tzo / 60),
":",
pad(tzo % 60),
].join("");
};
exports.getTime = getTime;
const log = (...msg) => console.log(`${chalk_1.default.grey(`${(0, exports.getTime)()}:`)} ${msg}`);
exports.log = log;
const readJsonIfExists = (filePath) => {
try {
return json5_1.default.parse(fs_1.default.readFileSync(filePath, { encoding: "utf8" }));
}
catch (err) {
// if no file, return empty object
if (err?.code === "ENOENT")
return {};
throw err;
}
};
exports.readJsonIfExists = readJsonIfExists;
2024-01-28 01:42:43 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvdXRpbHMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7O0FBQUEsa0RBQTBCO0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