@timothy-spaceman/multitrack-vcs
Version:
Version Control System for musicians
77 lines • 2.56 kB
JavaScript
import { File } from "../file.js";
import fs from "fs";
import path from "node:path";
import { glob } from "glob";
export class FsStorageProvider {
constructor() {
}
async exists(path) {
try {
await fs.promises.access(path);
return true;
}
catch {
return false;
}
}
async isFile(path) {
try {
return (await fs.promises.stat(path)).isFile();
}
catch {
return false;
}
}
async readFile(filePath) {
await fs.promises.access(filePath, fs.constants.R_OK);
return new File(path.resolve(filePath));
}
async createFile(filePath, content) {
const resolvedPath = path.resolve(filePath);
const dir = path.dirname(resolvedPath);
await fs.promises.mkdir(dir, { recursive: true });
await fs.promises.writeFile(resolvedPath, content);
return new File(resolvedPath);
}
async moveFile(sourcePath, targetPath) {
const from = path.resolve(sourcePath);
const to = path.resolve(targetPath);
const targetDir = path.dirname(to);
await fs.promises.mkdir(targetDir, { recursive: true });
await fs.promises.rename(from, to);
return new File(to);
}
async copyFile(sourcePath, targetPath) {
const from = path.resolve(sourcePath);
const to = path.resolve(targetPath);
const targetDir = path.dirname(to);
await fs.promises.mkdir(targetDir, { recursive: true });
await fs.promises.copyFile(from, to);
return new File(to);
}
async isDir(path) {
try {
return (await fs.promises.stat(path)).isDirectory();
}
catch {
return false;
}
}
async readDir(dirPath, ignore = []) {
const results = await glob.glob(["*"], { cwd: dirPath, ignore, noext: false, absolute: false });
return results.filter(p => p != ".");
}
async readDirDeep(dirPath, ignore = []) {
const results = await glob.glob(["**/*", ".**", ".**/*"], { cwd: dirPath, ignore, noext: false, absolute: false });
return results.filter(p => p != ".");
}
async createDir(dirPath) {
const resolvedPath = path.resolve(dirPath);
await fs.promises.mkdir(resolvedPath, { recursive: true });
return resolvedPath;
}
async deleteFileOrDir(path) {
await fs.promises.rm(path, { recursive: true, force: true });
}
}
//# sourceMappingURL=fs-storage-provider.js.map