summaryrefslogtreecommitdiff
path: root/Components/ListDirectory/index.js
diff options
context:
space:
mode:
authorMinteck <contact@minteck.org>2022-11-28 17:31:34 +0100
committerMinteck <contact@minteck.org>2022-11-28 17:31:34 +0100
commit7923aa8942b55884320ef2428417e3ee4b121613 (patch)
tree7993632f2898b1998f25b11ce40a8d2eb3d44730 /Components/ListDirectory/index.js
downloadmistyos-og-7923aa8942b55884320ef2428417e3ee4b121613.tar.gz
mistyos-og-7923aa8942b55884320ef2428417e3ee4b121613.tar.bz2
mistyos-og-7923aa8942b55884320ef2428417e3ee4b121613.zip
Initial commitHEADmane
Diffstat (limited to 'Components/ListDirectory/index.js')
-rw-r--r--Components/ListDirectory/index.js85
1 files changed, 85 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