67 lines
8.1 KiB
JavaScript
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvdXRpbHMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7O0FBQUEsa0RBQTBCO0FBQzFCLDRDQUFvQjtBQUNwQixrREFBMEI7QUFNbkIsTUFBTSw0QkFBNEIsR0FBRyxDQUFDLEdBQVcsRUFBRSxFQUFFLENBQzNELEdBQUcsQ0FBQyxPQUFPLENBQUMsUUFBUSxFQUFFLENBQUMsTUFBTSxFQUFFLEVBQUUsQ0FBQyxJQUFJLE1BQU0sRUFBRSxDQUFDLENBQUMsV0FBVyxFQUFFLENBQUM7QUFEbEQsUUFBQSw0QkFBNEIsZ0NBQ3NCO0FBRS9EOztHQUVHO0FBQ0ksTUFBTSxVQUFVLEdBQUcsR0FBRyxFQUFFO0lBQzlCLGdCQUFnQjtJQUNoQixNQUFNLEVBQUUsS0FBSyxFQUFFLEdBQUcsSUFBSSxLQUFLLEVBQUUsQ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