@techmely/utils
Version:
Collection of helpful JavaScript / TypeScript utils
175 lines (171 loc) • 5.83 kB
JavaScript
/*!
* @techmely/utils
* Copyright(c) 2021-2024 Techmely <techmely.creation@gmail.com>
* MIT Licensed
*/
;
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);
// src/file.ts
var file_exports = {};
__export(file_exports, {
MimeType: () => MimeType,
copyDir: () => copyDir,
emptyDir: () => emptyDir,
findNearestFile: () => findNearestFile,
getMimeTypeFromExtension: () => getMimeTypeFromExtension,
isFileReadable: () => isFileReadable,
mimeTable: () => mimeTable,
readFile: () => readFile,
readFileSync: () => readFileSync,
writeFile: () => writeFile
});
module.exports = __toCommonJS(file_exports);
var import_fs = __toESM(require("fs"));
var import_path = __toESM(require("path"));
var MimeType = /* @__PURE__ */ ((MimeType2) => {
MimeType2["JPG"] = "image/jpeg";
MimeType2["GIF"] = "image/gif";
MimeType2["PNG"] = "image/png";
MimeType2["MP4"] = "video/mp4";
MimeType2["QuickTime"] = "video/quicktime";
MimeType2["AVI"] = "video/x-msvideo";
MimeType2["WMV"] = "video/x-ms-wmv";
MimeType2["HEIC"] = "image/heic";
return MimeType2;
})(MimeType || {});
var mimeTable = [
{ ext: ["gif"], type: "image/gif" /* GIF */ },
{ ext: ["jpe", "jpeg", "jpg"], type: "image/jpeg" /* JPG */ },
{ ext: ["png"], type: "image/png" /* PNG */ },
{ ext: ["qt", "mov"], type: "video/quicktime" /* QuickTime */ },
{ ext: ["mp4", "mp4v", "mpg4", "m4v"], type: "video/mp4" /* MP4 */ },
{ ext: ["avi"], type: "video/x-msvideo" /* AVI */ },
{ ext: ["wmv"], type: "video/x-ms-wmv" /* WMV */ },
{ ext: ["heic"], type: "image/heic" /* HEIC */ }
];
var getMimeTypeFromExtension = (extension) => {
const result = mimeTable.find(({ ext }) => ext.includes(extension));
if (!result) {
throw new Error(`'${extension}' is not a valid extension`);
}
return result.type;
};
function writeFile(filename, content) {
const dir = import_path.default.dirname(filename);
if (!import_fs.default.existsSync(dir)) {
import_fs.default.mkdirSync(dir, { recursive: true });
}
import_fs.default.writeFileSync(filename, content);
}
function isFileReadable(filename) {
try {
import_fs.default.accessSync(filename, import_fs.default.constants.R_OK);
return true;
} catch {
return false;
}
}
function emptyDir(dir, skip) {
for (const file of import_fs.default.readdirSync(dir)) {
if (skip?.includes(file)) {
continue;
}
const abs = import_path.default.resolve(dir, file);
if (import_fs.default.lstatSync(abs).isDirectory()) {
emptyDir(abs);
import_fs.default.rmdirSync(abs);
} else {
import_fs.default.unlinkSync(abs);
}
}
}
function copyDir(srcDir, destDir) {
import_fs.default.mkdirSync(destDir, { recursive: true });
for (const file of import_fs.default.readdirSync(srcDir)) {
const srcFile = import_path.default.resolve(srcDir, file);
if (srcFile === destDir) {
continue;
}
const destFile = import_path.default.resolve(destDir, file);
const stat = import_fs.default.statSync(srcFile);
if (stat.isDirectory()) {
copyDir(srcFile, destFile);
} else {
import_fs.default.copyFileSync(srcFile, destFile);
}
}
}
function readFile(filePath) {
return new Promise((resolve, reject) => {
import_fs.default.readFile(filePath, "utf8", (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
}
function readFileSync(filePath) {
return import_fs.default.readFileSync(filePath, "utf8");
}
async function getDataPath(directoryPath, fileName) {
const packageJsonPath = import_path.default.join(directoryPath, fileName);
const pkg = await readFile(packageJsonPath);
const packageJsonData = JSON.parse(pkg);
return {
path: packageJsonPath,
data: packageJsonData
};
}
async function findNearestFile(fileName, directoryPath = import_path.default.resolve()) {
try {
const data = await getDataPath(directoryPath, fileName);
return data;
} catch (error) {
const parentDirectoryPath = import_path.default.dirname(directoryPath);
if (parentDirectoryPath === directoryPath) {
throw new Error(`No ${fileName} files found`);
}
return findNearestFile(parentDirectoryPath, fileName);
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
MimeType,
copyDir,
emptyDir,
findNearestFile,
getMimeTypeFromExtension,
isFileReadable,
mimeTable,
readFile,
readFileSync,
writeFile
});