utils/lib/cjs/utils.js

67 lines
8.1 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.readJsonIfExists = exports.log = exports.getTime = exports.getDirname = exports.convertCamelToUpperSnakeCase = void 0;
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;
/**
* From: https://www.webmanajemen.com/2022/05/use-import-meta-cjs.html
*/
const getDirname = () => {
// get the stack
const { stack } = new Error();
// get the third line (the original invoker)
const invokeFileLine = (stack || "").split(`\n`)[2];
// match the file url from file://(.+.(ts|js)) and get the first capturing group
const __filename = (invokeFileLine.match(/file:\/\/(.+.(ts|js))/) ||
[])[1].slice(1);
// match the file URL from file://(.+)/ and get the first capturing group
// the (.+) is a greedy quantifier and will make the RegExp expand to the largest match
const __dirname = (invokeFileLine.match(/file:\/\/(.+)\//) || [])[1].slice(1);
return { __dirname, __filename };
};
exports.getDirname = getDirname;
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;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvdXRpbHMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7O0FBQUEsa0RBQTBCO0FBQzFCLDRDQUFvQjtBQUNwQixrREFBMEI7QUFNbkIsTUFBTSw0QkFBNEIsR0FBRyxDQUFDLEdBQVcsRUFBRSxFQUFFLENBQzNELEdBQUcsQ0FBQyxPQUFPLENBQUMsUUFBUSxFQUFFLENBQUMsTUFBTSxFQUFFLEVBQUUsQ0FBQyxJQUFJLE1BQU0sRUFBRSxDQUFDLENBQUMsV0FBVyxFQUFFLENBQUM7QUFEbEQsUUFBQSw0QkFBNEIsZ0NBQ3NCO0FBRS9EOztHQUVHO0FBQ0ksTUFBTSxVQUFVLEdBQUcsR0FBRyxFQUFFO0lBQzlCLGdCQUFnQjtJQUNoQixNQUFNLEVBQUUsS0FBSyxFQUFFLEdBQUcsSUFBSSxLQUFLLEVBQUUsQ0FBQztJQUM5Qiw0Q0FBNEM7SUFDNUMsTUFBTSxjQUFjLEdBQUcsQ0FBQyxLQUFLLElBQUksRUFBRSxDQUFDLENBQUMsS0FBSyxDQUFDLElBQUksQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDO0lBQ3BELGdGQUFnRjtJQUNoRixNQUFNLFVBQVUsR0FBRyxDQUFDLGNBQWMsQ0FBQyxLQUFLLENBQUMsdUJBQXVCLENBQUM7UUFDaEUsRUFBRSxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUMsS0FBSyxDQUFDLENBQUMsQ0FBQyxDQUFDO0lBQ2pCLHlFQUF5RTtJQUN6RSwyRkFBMkY7SUFDM0YsTUFBTSxTQUFTLEdBQUcsQ0FBQyxjQUFjLENBQUMsS0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