@sunamo/sunodejs
Version:
Node.js utilities for file system operations, process management, and Electron apps. Includes TypeScript support with functions for file operations, directory management, and cross-platform compatibility.
80 lines (79 loc) • 2.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.readDirectory = readDirectory;
exports.fileExistsWithData = fileExistsWithData;
exports.fileExists = fileExists;
exports.checkDirectoryExists = checkDirectoryExists;
exports.createUpfoldersPsysicallyUnlessThere = createUpfoldersPsysicallyUnlessThere;
exports.createFoldersPsysicallyUnlessThere = createFoldersPsysicallyUnlessThere;
exports.withEndSlash = withEndSlash;
const tslib_1 = require("tslib");
const promises_1 = tslib_1.__importStar(require("node:fs/promises"));
const path_1 = require("path");
async function readDirectory(directoryPath) {
try {
const files = await promises_1.default.readdir(directoryPath);
console.log(`Soubory v adresáři ${directoryPath}:`);
return files;
}
catch (err) {
console.error(`Chyba při čtení adresáře ${directoryPath}:`, err);
}
}
async function fileExistsWithData(path) {
try {
await promises_1.default.stat(path);
return true;
}
catch (err) {
return false;
}
}
async function fileExists(path) {
try {
await promises_1.default.access(path);
return true;
}
catch (err) {
return false;
}
}
async function checkDirectoryExists(log, path) {
const { error } = log;
try {
const stats = await (0, promises_1.stat)(path);
if (stats.isDirectory()) {
return true;
}
else {
return null;
}
}
catch (err) {
if (err.code === "ENOENT") {
return false;
}
else {
error(`An error occurred while checking the path. "${path}":`, err);
}
}
}
async function createUpfoldersPsysicallyUnlessThere(log, path) {
const resolvedPath = (0, path_1.resolve)(path);
return createFoldersPsysicallyUnlessThere(log, (0, path_1.dirname)(resolvedPath));
}
async function createFoldersPsysicallyUnlessThere(log, path) {
const { error } = log;
const resolvedPath = (0, path_1.resolve)(path);
try {
await promises_1.default.mkdir(resolvedPath, { recursive: true });
return true;
}
catch (err) {
error(`Error creating directory structure ${resolvedPath}:`, err);
return false;
}
}
function withEndSlash(path) {
return path.endsWith("\\") ? path : path + "\\";
}