Skip to content

Commit d478ea3

Browse files
authored
Add function to get full path from filesystem adapter (#35)
* add function to get the full path * check for same adapter by default * add documentaton * update version
1 parent 611abfe commit d478ea3

File tree

13 files changed

+95
-16
lines changed

13 files changed

+95
-16
lines changed

adapters/in-memory/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@loom-io/in-memory-adapter",
3-
"version": "0.12.1",
3+
"version": "0.12.2",
44
"main": "dist/exports/lib.js",
55
"types": "dist/exports/lib.d.ts",
66
"author": "Cotton Coding",

adapters/minio/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@loom-io/minio-s3-adapter",
3-
"version": "0.12.1",
3+
"version": "0.12.2",
44
"main": "dist/exports/lib.js",
55
"types": "dist/exports/lib.d.ts",
66
"author": "Cotton Coding",

adapters/node-fs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@loom-io/node-filesystem-adapter",
3-
"version": "0.12.1",
3+
"version": "0.12.2",
44
"main": "dist/exports/lib.js",
55
"types": "dist/exports/lib.d.ts",
66
"author": "Cotton Coding",
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { describe, test, expect } from "vitest";
2+
import { FilesystemAdapter } from "./lib";
3+
4+
describe("FilesystemAdapter", () => {
5+
test("should be able to create a new instance", () => {
6+
const adapter = new FilesystemAdapter();
7+
expect(adapter).toBeDefined();
8+
});
9+
10+
test("fullPath should return the full path of a file", () => {
11+
const adapter = new FilesystemAdapter("/root/dir");
12+
const file = adapter.file("file.txt");
13+
expect(adapter.getFullPath(file)).toBe("/root/dir/file.txt");
14+
});
15+
16+
test("fullPath should return the full path of a directory", () => {
17+
const adapter = new FilesystemAdapter("/etc/loom");
18+
const dir = adapter.dir("dir");
19+
expect(adapter.getFullPath(dir)).toBe("/etc/loom/dir");
20+
});
21+
22+
test("fullPath should fail to other adapters", () => {
23+
const adapter = new FilesystemAdapter("/etc/loom");
24+
const adapter2 = new FilesystemAdapter("/etc/loom");
25+
const dir2 = adapter2.dir("dir");
26+
expect(() => adapter.getFullPath(dir2)).toThrowError();
27+
});
28+
29+
test("fullPath should return the full path also from other adapter if flag is set", () => {
30+
const adapter = new FilesystemAdapter("/etc/loom");
31+
const adapter2 = new FilesystemAdapter("/etc/loom");
32+
const dir2 = adapter2.dir("dir");
33+
expect(adapter.getFullPath(dir2, true)).toBe("/etc/loom/dir");
34+
});
35+
36+
test("fullPath should return the full path of a file (cwd)", () => {
37+
const adapter = new FilesystemAdapter();
38+
const file = adapter.file("file.txt");
39+
expect(adapter.getFullPath(file)).toBe(process.cwd() + "/file.txt");
40+
});
41+
42+
test("fullPath should return the full path of a directory (cwd)", () => {
43+
const adapter = new FilesystemAdapter();
44+
const dir = adapter.dir("dir");
45+
expect(adapter.getFullPath(dir)).toBe(process.cwd() + "/dir");
46+
});
47+
48+
test("file should return a new LoomFile instance", () => {
49+
const adapter = new FilesystemAdapter();
50+
const file = adapter.file("file.txt");
51+
expect(file).toBeDefined();
52+
});
53+
54+
test("dir should return a new Directory instance", () => {
55+
const adapter = new FilesystemAdapter();
56+
const dir = adapter.dir("dir");
57+
expect(dir).toBeDefined();
58+
});
59+
});
Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import { PathLike } from 'node:fs';
2-
import { type LoomSourceAdapter } from '@loom-io/core';
3-
import { Adapter } from '../core/adapter.js';
4-
import { dirname, basename } from 'node:path';
5-
import { Directory, LoomFile } from '@loom-io/core/internal';
1+
import { PathLike } from "node:fs";
2+
import { type LoomSourceAdapter } from "@loom-io/core";
3+
import { Adapter } from "../core/adapter.js";
4+
import { dirname, basename } from "node:path";
5+
import { Directory, LoomFile } from "@loom-io/core/internal";
6+
7+
import { resolve } from "node:path";
68

79
export class FilesystemAdapter implements LoomSourceAdapter {
810
protected adapter: Adapter;
11+
protected rootdir: PathLike;
912
constructor(rootdir: PathLike = process.cwd()) {
13+
this.rootdir = rootdir;
1014
this.adapter = new Adapter(rootdir);
1115
}
1216
file(path: string): LoomFile {
@@ -16,4 +20,11 @@ export class FilesystemAdapter implements LoomSourceAdapter {
1620
dir(path: string): Directory {
1721
return new Directory(this.adapter, path);
1822
}
19-
}
23+
24+
getFullPath(dirOrFile: Directory | LoomFile, ignoreAdapter = false) {
25+
if (!ignoreAdapter && this.adapter !== dirOrFile.adapter) {
26+
throw new Error("The directory or file does not belong to this adapter");
27+
}
28+
return resolve(this.rootdir.toString(), dirOrFile.path);
29+
}
30+
}

documentation/adapter/node-filesystem-adapter.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,12 @@ const project = new FilesystemAdapter();
3333
const rootDir = root.dir("/");
3434
const projectDir = project("/");
3535
```
36+
37+
38+
## Additional functionality
39+
40+
### getFullPath
41+
42+
By default, the root of each file is set to the current working directory or the specified installation path, and the path returned by the directory of the file object is a relative path to that directory. If the full path is needed, it can be generated by calling `adapter.getFullPath(dirOrFileObject)`. By default this function checks if it is from the same adapter and throws an error if not. Set the second param of the function to `true` to not check the adapter and ignore the path if it probably does not exist.
43+
44+

packages/combined-converter/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@loom-io/converter",
3-
"version": "0.12.1",
3+
"version": "0.12.2",
44
"main": "dist/exports/lib.js",
55
"types": "dist/exports/lib.d.ts",
66
"author": "Cotton Coding",

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@loom-io/core",
3-
"version": "0.12.1",
3+
"version": "0.12.2",
44
"main": "dist/exports/lib.js",
55
"types": "dist/exports/lib.d.ts",
66
"author": "Cotton Coding",

plugins/front-matter-converter/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@loom-io/front-matter-converter",
3-
"version": "0.12.1",
3+
"version": "0.12.2",
44
"description": "Convert front matter yaml and json into json with loom-io",
55
"main": "dist/exports/lib.js",
66
"type": "module",

plugins/json-converter/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@loom-io/json-converter",
3-
"version": "0.12.1",
3+
"version": "0.12.2",
44
"description": "A plugin for @loom-io to convert json files to JSON",
55
"main": "dist/json-converter.js",
66
"module": "dist/json-converter.js",

0 commit comments

Comments
 (0)