87 lines
1.8 KiB
JavaScript
Raw Normal View History

2025-03-07 13:14:32 +01:00
import path from "path";
import fs from "fs";
import { filesystemSync } from "fs-filesystem";
export class FsService {
getAvailableRoots = () => {
const devices = filesystemSync();
var mountPoints = [];
Object.keys(devices).forEach(function (key) {
var val = devices[key];
val.volumes.forEach(function (volume) {
const mount = volume.mountPoint;
if (mount != null && mount != undefined && mount.length > 0) {
2025-04-21 11:22:44 +02:00
try {
if (!fs.lstatSync(mount).isFile()) {
mountPoints.push(mount);
}
} catch {
}
}
2025-03-07 13:14:32 +01:00
});
});
if (mountPoints.length < 1) {
// In certain containerized environments, the devices don't reveal any
// useful mounts. We'll proceed under the assumption that '/' is valid here.
return ['/'];
2025-03-07 13:14:32 +01:00
}
return mountPoints;
};
pathJoin = (parts) => {
return path.join(...parts);
};
isDir = (dir) => {
try {
return fs.lstatSync(dir).isDirectory();
} catch {
return false;
}
};
isFile = (path) => {
try {
return fs.lstatSync(path).isFile();
} catch {
return false;
}
};
2025-03-07 13:14:32 +01:00
readDir = (dir) => {
return fs.readdirSync(dir);
};
makeDir = (dir) => {
fs.mkdirSync(dir);
};
2025-04-07 16:01:49 +02:00
moveDir = (oldPath, newPath) => {
fs.moveSync(oldPath, newPath);
};
2025-04-09 10:27:00 +02:00
deleteDir = (dir) => {
fs.rmSync(dir, { recursive: true, force: true });
};
readJsonFile = (filePath) => {
return JSON.parse(fs.readFileSync(filePath));
};
writeJsonFile = (filePath, jsonObject) => {
fs.writeFileSync(filePath, JSON.stringify(jsonObject));
};
writeFile = (filePath, content) => {
fs.writeFileSync(filePath, content);
};
2025-04-16 09:42:11 +02:00
2025-04-21 10:41:44 +02:00
ensureDirExists = (dir) => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
return dir;
2025-04-16 09:42:11 +02:00
};
2025-03-07 13:14:32 +01:00
}