MCPcopy
hub / github.com/foambubble/foam / NodeFileDataStore

Class NodeFileDataStore

packages/foam-cli/src/support/filesystem.ts:36–92  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34};
35
36export class NodeFileDataStore implements IDataStore {
37 constructor(
38 private readonly rootDir: string,
39 private readonly excludedPaths: string[],
40 private readonly matcher: IMatcher
41 ) {}
42
43 async list(pattern?: string) {
44 const files: string[] = [];
45 await collectFiles(this.rootDir, files, this.excludedPaths);
46 let uris = files.map(file => URI.file(file));
47 if (pattern) {
48 const absoluteGlob = path.posix.join(this.rootDir, pattern);
49 const matched = micromatch(uris.map(u => u.toFsPath()), [absoluteGlob]);
50 const matchedSet = new Set(matched);
51 uris = uris.filter(u => matchedSet.has(u.toFsPath()));
52 }
53 return uris.filter(uri => this.matcher.isMatch(uri));
54 }
55
56 async read(uri: URI) {
57 try {
58 return await fs.readFile(uri.toFsPath(), 'utf8');
59 } catch {
60 return null;
61 }
62 }
63
64 async write(uri: URI, content: string): Promise<void> {
65 const fsPath = uri.toFsPath();
66 await fs.mkdir(path.dirname(fsPath), { recursive: true });
67 await fs.writeFile(fsPath, content, 'utf8');
68 }
69
70 async delete(uri: URI): Promise<void> {
71 try {
72 await fs.unlink(uri.toFsPath());
73 } catch (err: any) {
74 if (err.code !== 'ENOENT') throw err;
75 }
76 }
77
78 async move(from: URI, to: URI): Promise<void> {
79 const toFs = to.toFsPath();
80 await fs.mkdir(path.dirname(toFs), { recursive: true });
81 await fs.rename(from.toFsPath(), toFs);
82 }
83
84 async exists(uri: URI): Promise<boolean> {
85 try {
86 await fs.access(uri.toFsPath());
87 return true;
88 } catch {
89 return false;
90 }
91 }
92}
93

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected