diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js new file mode 100644 index 000000000..0d014d57e --- /dev/null +++ b/implement-shell-tools/cat/cat.js @@ -0,0 +1,38 @@ +import {program} from "commander"; +import {promises as fs} from "node:fs"; +import process from "node:process"; + +program +.name ("cat") +.description ("A JS clone for unix command cat") +.argument("","One or more files to reach and print") +.option("-n", "number all output lines") +.action(async (files,options)=> { + for (const filePath of files){ + try { + const content = await fs.readFile(filePath,"utf8"); + if (options.n) + { + const lines = content.trimEnd().split("\n"); + lines.forEach((line,index) => { + console.log(`${index+1} \t ${line}`); + }); + } + else + { + process.stdout.write(content); + } + + + } + catch(error) + { + console.error("No such file or directory"); + process.exit(1); + } + + } + +}); + +program.parse(process.argv); \ No newline at end of file diff --git a/implement-shell-tools/cat/package-lock.json b/implement-shell-tools/cat/package-lock.json new file mode 100644 index 000000000..5cc805d7d --- /dev/null +++ b/implement-shell-tools/cat/package-lock.json @@ -0,0 +1,21 @@ +{ + "name": "cat", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "commander": "^15.0.0" + } + }, + "node_modules/commander": { + "version": "15.0.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-15.0.0.tgz", + "integrity": "sha512-z67u4ZhzCL/Tydu1lJARtEZYWbWaN7oYLHbsuzocr6y4N6WZAagG3RQ4FW61V1/0+jImpj293XfrcYnd1qxtPg==", + "license": "MIT", + "engines": { + "node": ">=22.12.0" + } + } + } +} diff --git a/implement-shell-tools/cat/package.json b/implement-shell-tools/cat/package.json new file mode 100644 index 000000000..0a3e3ebed --- /dev/null +++ b/implement-shell-tools/cat/package.json @@ -0,0 +1,6 @@ +{ + "type":"module" , + "dependencies": { + "commander": "^15.0.0" + } +} diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js new file mode 100644 index 000000000..a9b8c9f26 --- /dev/null +++ b/implement-shell-tools/ls/ls.js @@ -0,0 +1,27 @@ +import {program} from "commander"; +import process from "node:process"; +import {promises as fs } from "node:fs"; + + +program +.name ("list command clone") +.description("List all files & folders within current folder") +.argument("[directory]","Directory to list",".") +.option("-1","list file line by line") +.action(async (directory,options) => +{ + try + { + let files = await fs.readdir(directory); + console.log(files.join(" ")); + } + + catch (error) + { + console.error('No such file or directory'); + process.exit(1); + } + +}); + +program.parse(process.argv); \ No newline at end of file diff --git a/implement-shell-tools/ls/package-lock.json b/implement-shell-tools/ls/package-lock.json new file mode 100644 index 000000000..09674c718 --- /dev/null +++ b/implement-shell-tools/ls/package-lock.json @@ -0,0 +1,21 @@ +{ + "name": "ls", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "commander": "^15.0.0" + } + }, + "node_modules/commander": { + "version": "15.0.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-15.0.0.tgz", + "integrity": "sha512-z67u4ZhzCL/Tydu1lJARtEZYWbWaN7oYLHbsuzocr6y4N6WZAagG3RQ4FW61V1/0+jImpj293XfrcYnd1qxtPg==", + "license": "MIT", + "engines": { + "node": ">=22.12.0" + } + } + } +} diff --git a/implement-shell-tools/ls/package.json b/implement-shell-tools/ls/package.json new file mode 100644 index 000000000..cddd041aa --- /dev/null +++ b/implement-shell-tools/ls/package.json @@ -0,0 +1,6 @@ +{ + "type": "module", + "dependencies": { + "commander": "^15.0.0" + } +} diff --git a/implement-shell-tools/wc/package-lock.json b/implement-shell-tools/wc/package-lock.json new file mode 100644 index 000000000..974337ed8 --- /dev/null +++ b/implement-shell-tools/wc/package-lock.json @@ -0,0 +1,21 @@ +{ + "name": "wc", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "commander": "^15.0.0" + } + }, + "node_modules/commander": { + "version": "15.0.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-15.0.0.tgz", + "integrity": "sha512-z67u4ZhzCL/Tydu1lJARtEZYWbWaN7oYLHbsuzocr6y4N6WZAagG3RQ4FW61V1/0+jImpj293XfrcYnd1qxtPg==", + "license": "MIT", + "engines": { + "node": ">=22.12.0" + } + } + } +} diff --git a/implement-shell-tools/wc/package.json b/implement-shell-tools/wc/package.json new file mode 100644 index 000000000..2de0b91e2 --- /dev/null +++ b/implement-shell-tools/wc/package.json @@ -0,0 +1,7 @@ +{ + "type": "module", + "dependencies": { + "commander": "^15.0.0" + } + } + \ No newline at end of file diff --git a/implement-shell-tools/wc/wc.js b/implement-shell-tools/wc/wc.js new file mode 100644 index 000000000..a3e05ab21 --- /dev/null +++ b/implement-shell-tools/wc/wc.js @@ -0,0 +1,60 @@ +import process from "node:process"; +import {program} from "commander"; +import {promises as fs} from "node:fs"; + +program +.name("WC clone") +.description("Counting each words in a given file") +.argument("","one or more files to count the words") +.action(async (files)=>{ + let totalLines = 0; + let totalWords = 0; + let totalBytes = 0; + + for (const file of files){ + try { + const content = await fs.readFile(file,"utf-8"); + const size = await fs.readFile(file); + + const lineCount = content.split("\n").length-1; + + const wordCount = content.trim() === "" ? 0 : content.trim().split(/\s+/).length; + + const byteCount = size.length; + + totalLines += lineCount; + totalWords += wordCount; + totalBytes += byteCount; + + printCounts({lineCount,wordCount,byteCount,label:file,}); + } + +catch (err){ + console.error("No such file or directory"); + process.exit(1); +} + } + + if (files.length>1) + { + printCounts({lineCount:totalLines, + wordCount:totalWords, + byteCount:totalBytes, + label:"Total", + }) + } + +}); + +function printCounts({lineCount,wordCount,byteCount,label}) +{ + let output = ""; + + output += lineCount.toString().padStart(7, " "); + output += wordCount.toString().padStart(7," "); + output += byteCount.toString().padStart(7," "); + + console.log(`${output} ${label}`); +} + +program.parse(process.argv); \ No newline at end of file