-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
} |