filefive
Version:
SFTP/FTP/Amazon S3 client and dual-panel file manager for macOS and Linux
127 lines (126 loc) • 4.08 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isWin = isWin;
exports.osify = osify;
exports.unosify = unosify;
exports.pwd = pwd;
exports.stat = stat;
exports.list = list;
exports.mkDirRecursive = mkDirRecursive;
exports.move = move;
exports.copy = copy;
exports.del = del;
exports.touch = touch;
exports.read = read;
exports.readInBuffer = readInBuffer;
exports.write = write;
exports.watch = watch;
exports.watchChanges = watchChanges;
const node_os_1 = require("node:os");
const posix_1 = require("node:path/posix");
const node_path_1 = __importDefault(require("node:path"));
const node_fs_1 = require("node:fs");
const promises_1 = require("node:fs/promises");
const os_1 = require("./utils/os");
const win_1 = require("./win");
function isWin() {
return (0, node_os_1.platform)() === 'win32';
}
function osify(path) {
return isWin() ? (0, os_1.unixToWin)(path) : path;
}
function unosify(path) {
return isWin() ? (0, os_1.winToUnix)(path) : path;
}
function pwd() {
return unosify((0, node_os_1.homedir)());
}
function stat(path) {
path = (0, posix_1.normalize)(path);
const actualPath = osify(path);
try {
let stat = (0, node_fs_1.lstatSync)(actualPath);
let target;
if (stat.isSymbolicLink()) {
target = (0, node_fs_1.readlinkSync)(actualPath);
if (!(0, posix_1.isAbsolute)(target)) {
target = node_path_1.default.normalize(node_path_1.default.join(node_path_1.default.dirname(actualPath), target));
}
stat = (0, node_fs_1.statSync)(target);
}
return {
path,
name: (0, posix_1.basename)(path),
dir: stat.isDirectory(),
size: stat.size, // in bytes
modified: stat.mtime,
inode: stat.ino,
...(target ? { target } : {})
};
}
catch (e) {
return null;
}
}
function list(dir) {
let actialDir = osify(dir);
if (actialDir == '\\') {
return (0, win_1.getDrives)().map(path => stat(unosify(path))).filter(f => f);
}
return (0, node_fs_1.readdirSync)(actialDir).map(name => stat((0, posix_1.join)(dir, name))).filter(f => f);
}
async function mkDirRecursive(path) {
return (0, promises_1.mkdir)(osify(path), { recursive: true });
}
async function move(from, to, force = false) {
await mkDirRecursive((0, posix_1.dirname)(to));
const existing = stat(to);
if (existing) {
if (force) {
await (0, promises_1.unlink)(osify(to));
}
else {
return existing;
}
}
await (0, promises_1.rename)(osify(from), osify(to));
return true;
}
async function copy(from, to, force = false) {
await mkDirRecursive((0, posix_1.dirname)(to));
const existing = stat(to);
if (existing && !force) {
return existing;
}
await (0, promises_1.cp)(osify(from), osify(to), { recursive: true });
return true;
}
async function del(path) {
return (0, promises_1.rm)(osify(path), { recursive: true, force: true });
}
async function touch(path, data) {
if (!stat(path)) {
await mkDirRecursive((0, posix_1.dirname)(path));
data !== undefined ? await (0, promises_1.writeFile)(osify(path), data) : await (await (0, promises_1.open)(osify(path), 'a')).close();
}
}
async function read(path) {
return (0, promises_1.readFile)(osify(path), { encoding: 'utf8' });
}
async function readInBuffer(path) {
return (0, promises_1.readFile)(osify(path));
}
async function write(path, data) {
return (0, promises_1.writeFile)(osify(path), data);
}
function watch(path, listener, onError) {
const watcher = (0, node_fs_1.watch)(osify(path), listener);
watcher.on('error', onError);
return () => watcher.close();
}
function watchChanges(path, ac) {
return (0, promises_1.watch)(osify(path), { signal: ac.signal });
}