filefive
Version:
SFTP/FTP/Amazon S3 client and dual-panel file manager for macOS and Linux
87 lines (86 loc) • 2.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.pwd = pwd;
exports.stat = stat;
exports.list = list;
exports.move = move;
exports.copy = copy;
exports.del = del;
exports.touch = touch;
exports.watch = watch;
exports.read = read;
const node_os_1 = require("node:os");
const node_path_1 = require("node:path");
const node_fs_1 = require("node:fs");
const promises_1 = require("node:fs/promises");
function pwd() {
return (0, node_os_1.homedir)();
}
function stat(path) {
path = (0, node_path_1.normalize)(path);
try {
let stat = (0, node_fs_1.lstatSync)(path);
let target;
if (stat.isSymbolicLink()) {
target = (0, node_fs_1.readlinkSync)(path);
if (!(0, node_path_1.isAbsolute)(target)) {
target = (0, node_path_1.normalize)((0, node_path_1.join)((0, node_path_1.dirname)(path), target));
}
stat = (0, node_fs_1.statSync)(target);
}
return {
path,
name: (0, node_path_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) {
return (0, node_fs_1.readdirSync)(dir).map(name => stat((0, node_path_1.join)(dir, name))).filter(f => f);
}
async function move(from, to, force = false) {
await (0, promises_1.mkdir)((0, node_path_1.dirname)(to), { recursive: true });
const existing = stat(to);
if (existing) {
if (force) {
await (0, promises_1.unlink)(to);
}
else {
return existing;
}
}
await (0, promises_1.rename)(from, to);
return true;
}
async function copy(from, to, force = false) {
await (0, promises_1.mkdir)((0, node_path_1.dirname)(to), { recursive: true });
const existing = stat(to);
if (existing && !force) {
return existing;
}
await (0, promises_1.cp)(from, to, { recursive: true });
return true;
}
async function del(path) {
return (0, promises_1.rm)(path, { recursive: true, force: true });
}
async function touch(path, data) {
if (!stat(path)) {
await (0, promises_1.mkdir)((0, node_path_1.dirname)(path), { recursive: true });
data !== undefined ? await (0, promises_1.writeFile)(path, data) : await (await (0, promises_1.open)(path, 'a')).close();
}
}
function watch(path, listener) {
const watcher = (0, node_fs_1.watch)(path, listener);
return () => watcher.close();
}
async function read(path) {
return await (0, promises_1.readFile)(path, { encoding: 'utf8' });
}