Skip to content

Commit

Permalink
Changed the filesystem plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
bgelatti committed Oct 10, 2024
1 parent cd2ffd0 commit 4ba4481
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 0 deletions.
9 changes: 9 additions & 0 deletions plugins/filesystem-local/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# About

This is a plugin that allows users to select files and folders in the server's filesystem.

---

## License

MIT
Binary file added plugins/filesystem-local/assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions plugins/filesystem-local/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@tago-io/tcore-plugin-filesystem-local",
"version": "0.0.1",
"private": true,
"main": "./build/index.js",
"files": [
"assets/**/*",
"build/**/*"
],
"tcore": {
"name": "Local Disk Filesystem",
"types": ["filesystem"],
"icon": "./assets/icon.png",
"full_description": "./README.md",
"hidden": true,
"cluster": true,
"permissions": []
},
"scripts": {
"build:clean": "rm -rf ./build",
"build": "npm run build:clean; esbuild --bundle ./src/index.ts --platform=node --target=node16 --minify --outfile=./build/index.js",
"pack": "tcore-plugin pack -f -o c311dfe85cf1a29fb1d86a1a2c5afc1c.tcore",
"plugin:add": "cp c311dfe85cf1a29fb1d86a1a2c5afc1c.tcore ../../plugins",
"tsc:watch": "npm run build -- --watch"
},
"dependencies": {
"systeminformation": "5.9.7",
"@tago-io/tcore-sdk": "*"
}
}
14 changes: 14 additions & 0 deletions plugins/filesystem-local/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { FileSystemModule } from "@tago-io/tcore-sdk";
import { resolveFile } from "./resolveFile";
import { resolveFolder } from "./resolveFolder";

/**
* Filesystem module.
*/
const filesystem = new FileSystemModule({
id: "local-filesystem",
name: "Local Disk Filesystem",
});

filesystem.resolveFile = resolveFile;
filesystem.resolveFolder = resolveFolder;
8 changes: 8 additions & 0 deletions plugins/filesystem-local/src/resolveFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Returns the local path of the file since this is in the local disk anyway.
*/
async function resolveFile(filePath: string) {
return filePath;
};

export { resolveFile };
99 changes: 99 additions & 0 deletions plugins/filesystem-local/src/resolveFolder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import path from "path";
import fs from "fs";
import os from "os";
import si from "systeminformation";
import { IPluginFilesystemItem } from "@tago-io/tcore-sdk/types";

/**
* Checks if a file path is directory or not.
*/
async function isDirectory(file: string): Promise<boolean> {
try {
const stat = await fs.promises.lstat(file);
const isDir = stat.isDirectory();
return isDir;
} catch (ex) {
return false;
}
}

/**
* Receives an array of paths and adds some characteristics to each file in
* the path, such as name, full path, and if the path is a folder or not.
*/
async function transformFiles(root: string, paths: string[]): Promise<IPluginFilesystemItem[]> {
const transformed: any[] = [];

for (const item of paths) {
transformed.push({
name: item,
path: root ? path.join(root, item) : item,
is_folder: await isDirectory(root ? path.join(root, item) : item),
children: [],
});
}

return transformed;
}

/**
* List all mounts of the filesystem.
*/
async function getMounts() {
if (os.platform() === "win32") {
// windows
const mounts = await si.fsSize();
return mounts.map((x) => x.mount);
} else {
// unix-like
return ["/"];
}
}

/**
* Resolves a folder structure up until the folder path.
*/
async function resolveFolder(folderPath: string) {
const mounts = await getMounts();
const roots = await transformFiles("", mounts);

const filterArray = folderPath.split(/\/|\\/g).filter((x) => x);
if (os.platform() !== "win32") {
// add / for unix-like systems
filterArray.unshift("/");
}

const isExistingFile = folderPath && fs.existsSync(folderPath) && !(await isDirectory(folderPath));
if (isExistingFile) {
// if the folder path is pointing to a file, we must show the parent folder of the file
filterArray.pop();
}

const accumulated: string[] = [];
let last: IPluginFilesystemItem[] = roots;

for (const folder of filterArray) {
accumulated.push(folder);

let accumulatedString = accumulated.join(path.sep).replace(/\/\//g, "/");
if (accumulatedString.endsWith(":")) {
accumulatedString += path.sep;
}

const subFolders = await fs.promises.readdir(accumulatedString).catch(() => []);

const temp = last?.find((x) => x.name === folder);
if (temp) {
temp.children = await transformFiles(accumulatedString, subFolders).catch(() => []);
last = temp.children;
}
}

if (roots.length === 1) {
return roots[0].children;
}

return roots;
};

export { resolveFolder };
30 changes: 30 additions & 0 deletions plugins/filesystem-local/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"lib": ["ES2020"],
"allowJs": true,
"sourceMap": false,
"outDir": "./build",
"strict": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"noImplicitAny": false,
"rootDir": "./src",
"baseUrl": ".",
"declaration": false,
"skipLibCheck": true
},
"exclude": [
"node_module",
"jest.config.js",
".eslintrc.js"
],
"include": ["src/**/*"],
"ts-node": {
"transpileOnly": true
},
}

0 comments on commit 4ba4481

Please sign in to comment.