|
| 1 | +const fs = require("fs"); |
| 2 | +const { allowedNodeEnvironmentFlags } = require("process"); |
| 3 | + |
| 4 | +const args = process.argv; |
| 5 | +const userArgs = args.slice(2); |
| 6 | + |
| 7 | +const index = args[1].lastIndexOf("/"); |
| 8 | +const currentWorkingDirectory = args[1].slice(0, index); |
| 9 | + |
| 10 | +const flags = userArgs |
| 11 | + .filter((arg) => arg.startsWith("-")) |
| 12 | + .map((arg) => arg.slice(1)) |
| 13 | + .join(""); |
| 14 | + |
| 15 | +const flagHandlers = { |
| 16 | + 1: Flag1, |
| 17 | + a: Flaga, |
| 18 | +}; |
| 19 | + |
| 20 | +let printInList = false; |
| 21 | +let showAll = false; |
| 22 | + |
| 23 | +const fsItems = userArgs.filter((arg) => !arg.startsWith("-")); |
| 24 | +let dirArgs; |
| 25 | +let fileArgs; |
| 26 | +let outputString = ""; |
| 27 | + |
| 28 | +const rawData = fs.readdirSync(currentWorkingDirectory); |
| 29 | + |
| 30 | +checkArgsLength(); |
| 31 | +executeFlags(); |
| 32 | +filterFilesAndDirs(); |
| 33 | +populateOutput(); |
| 34 | +print(); |
| 35 | + |
| 36 | +function executeFlags() { |
| 37 | + for (const flag of flags) { |
| 38 | + if (flagHandlers[flag]) { |
| 39 | + allFilesContents = flagHandlers[flag](); |
| 40 | + } else { |
| 41 | + console.error( |
| 42 | + `ls: invalid option -- ${flag}\nusage: ls [-@ABCFGHILOPRSTUWXabcdefghiklmnopqrstuvwxy1%,] [--color=when] [-D format] [file ...]`, |
| 43 | + ); |
| 44 | + process.exit(1); |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +function Flag1() { |
| 50 | + if (printInList) { |
| 51 | + outputString = outputString.replaceAll("\t", "\n"); |
| 52 | + outputString = outputString.replaceAll("\n\n", "\n"); |
| 53 | + } else { |
| 54 | + printInList = true; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +function Flaga() { |
| 59 | + showAll = true; |
| 60 | +} |
| 61 | + |
| 62 | +function checkArgsLength() { |
| 63 | + if (fsItems.length == 0) { |
| 64 | + fsItems.push("."); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +function filterFilesAndDirs() { |
| 69 | + dirArgs = fsItems.filter((p) => fs.statSync(currentWorkingDirectory + "/" + p).isDirectory()); |
| 70 | + fileArgs = fsItems.filter((p) => !fs.statSync(currentWorkingDirectory + "/" + p).isDirectory()); |
| 71 | + if (showAll == false) { |
| 72 | + removeDotFiles(fileArgs); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +function populateOutput() { |
| 77 | + if (fsItems.length == 1) { |
| 78 | + fileArgs.forEach((file) => { |
| 79 | + outputString += file + " "; |
| 80 | + }); |
| 81 | + dirArgs.forEach((dir) => { |
| 82 | + outputString += dirOutput(dir); |
| 83 | + }); |
| 84 | + } else { |
| 85 | + fileArgs.forEach((file) => { |
| 86 | + outputString += file + "\t"; |
| 87 | + }); |
| 88 | + dirArgs.forEach((dir) => { |
| 89 | + outputString += "\n\n" + dir + ":\n" + dirOutput(dir); |
| 90 | + }); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +function dirOutput(dir) { |
| 95 | + let contents = fs.readdirSync(currentWorkingDirectory + "/" + dir); |
| 96 | + let outputStr = ""; |
| 97 | + if (showAll) { |
| 98 | + outputStr += ".\t..\t"; |
| 99 | + } else { |
| 100 | + for (let i = contents.length - 1; i >= 0; i--) { |
| 101 | + contents = removeDotFiles(contents); |
| 102 | + } |
| 103 | + } |
| 104 | + contents.forEach((item) => { |
| 105 | + outputStr += item + "\t"; |
| 106 | + }); |
| 107 | + return outputStr; |
| 108 | +} |
| 109 | + |
| 110 | +function removeDotFiles(fileList) { |
| 111 | + for (let i = fileList.length - 1; i >= 0; i--) { |
| 112 | + if (fileList[i][0] == ".") { |
| 113 | + fileList.splice(i, 1); |
| 114 | + } |
| 115 | + } |
| 116 | + return fileList; |
| 117 | +} |
| 118 | + |
| 119 | +function print() { |
| 120 | + if (printInList == true) { |
| 121 | + Flag1(); |
| 122 | + } |
| 123 | + console.log(outputString.trimEnd()); |
| 124 | +} |
0 commit comments