diff options
Diffstat (limited to 'Components/ListDirectory')
-rw-r--r-- | Components/ListDirectory/index.js | 85 | ||||
-rw-r--r-- | Components/ListDirectory/metadata.yml | 40 |
2 files changed, 125 insertions, 0 deletions
diff --git a/Components/ListDirectory/index.js b/Components/ListDirectory/index.js new file mode 100644 index 0000000..a7fa45b --- /dev/null +++ b/Components/ListDirectory/index.js @@ -0,0 +1,85 @@ +let arguments = JSON.parse(process.argv[2]); +const chalk = require(__dirname + '/../../MistyCore/node_modules/chalk'); +const fs = require("fs"); +const path = require('path'); + +let list = []; + +function processList(list, dir, arguments) { + if (!Object.keys(arguments).includes("all") && dir === "/") { + list = list.filter(i => i !== "dev" && i !== "etc" && i !== "bin" && i !== "lib" && i !== "lib64" && i !== "lost+found" && i !== "proc" && i !== "run" && i !== "sys" && i !== "MistyOSPrivate"); + } else if (!Object.keys(arguments).includes("All") &&dir === "/User") { + list = list.filter(i => i !== "Library"); + } + + if (Object.keys(arguments).includes("all")) list.unshift(".", ".."); + if (Object.keys(arguments).includes("search")) list = list.filter(i => i.includes(arguments["search"])); + + if (!Object.keys(arguments).includes("all")) list = list.filter(i => !i.startsWith(".")); + return list.map((i) => { + if (fs.existsSync(dir + "/" + i)) { + try { + fs.accessSync(dir + "/" + i, fs.constants.R_OK); + + if (fs.lstatSync(dir + "/" + i).isDirectory()) { + return i + chalk.gray("/"); + } else if (fs.lstatSync(dir + "/" + i).isSymbolicLink()) { + return i + chalk.gray(">"); + } else { + return i; + } + } catch (e) { + return i + chalk.gray("?"); + } + } else { + return i + chalk.gray("x"); + } + }); +} + +if (arguments._finals.length > 0) { + if (arguments._finals.length === 1) { + if (fs.existsSync(arguments._finals[0])) { + try { + fs.accessSync(arguments._finals[0], fs.constants.R_OK); + + if (!fs.lstatSync(arguments._finals[0]).isDirectory()) { + console.log(chalk.bgYellow.white("<!>") + " " + chalk.yellow("Not a directory: ") + arguments._finals[0]); + } else { + list = require('fs').readdirSync(arguments._finals[0]); + list = processList(list, path.resolve(arguments['_finals'][0]), arguments); + console.log(list.join(chalk.gray(", "))); + } + } catch (e) { + console.log(chalk.bgYellow.white("<!>") + " " + chalk.yellow("Permission denied: ") + arguments._finals[0]); + } + } else { + console.log(chalk.bgYellow.white("<!>") + " " + chalk.yellow("No such file or directory: ") + arguments._finals[0]); + } + } else { + for (let final of arguments._finals) { + console.log(final + ":"); + if (fs.existsSync(final)) { + try { + fs.accessSync(final, fs.constants.R_OK); + + if (!fs.lstatSync(final).isDirectory()) { + console.log(" " + chalk.bgYellow.white("<!>") + " " + chalk.yellow("Not a directory: ") + final); + } else { + list = require('fs').readdirSync(final); + list = processList(list, path.resolve(final), arguments); + console.log(" " + list.join(chalk.gray(", "))); + } + } catch (e) { + console.log(" " + chalk.bgYellow.white("<!>") + " " + chalk.yellow("Permission denied: ") + final); + } + } else { + console.log(" " + chalk.bgYellow.white("<!>") + " " + chalk.yellow("No such file or directory: ") + final); + } + } + } +} else { + list = require('fs').readdirSync("."); + list = processList(list, path.resolve("."), arguments); + console.log(list.join(chalk.gray(", "))); +}
\ No newline at end of file diff --git a/Components/ListDirectory/metadata.yml b/Components/ListDirectory/metadata.yml new file mode 100644 index 0000000..431c999 --- /dev/null +++ b/Components/ListDirectory/metadata.yml @@ -0,0 +1,40 @@ +description: Lists files in the current or a select directory +internal: false + +aliases: + - ls + - dir + +manual: + summary: | + This command lists all files that exist in the specific directory, or the current working directory if not specified. + + parameters: + - name: All + description: Show hidden files (starting with . or system files) + required: false + + - name: Search + value: Query + description: Search for a specific file containing the query + required: false + + final: + name: Dir + description: The directory or directories to list files of + required: false + multiple: true + command: false + path: true + daemon: false + + examples: + - command: ListDirectory -All + description: List all the files in the current directory, including hidden files + + - command: ListDirectory / + description: List all the files in the root directory + + compatibility: + mistyos: '>=1.0.0' + kernel: '>=5.10.0'
\ No newline at end of file |