diff --git a/bin/file-manager.js b/bin/file-manager.js new file mode 100644 index 0000000000..3b23596d17 --- /dev/null +++ b/bin/file-manager.js @@ -0,0 +1,27 @@ +import process from 'node:process' +import { printWelcome } from '../src/utils/printWelcome.js'; +import { getUserName } from '../src/utils/getUserName.js'; +import { commandDispatch, printCommandsPromts } from '../src/utils/commandDispatch.js'; +import { homedir } from 'node:os'; + +const fileManager = async () => { + const [shell, filename, cliUserName] = process.argv + const userName = getUserName(cliUserName) + + printWelcome(userName) + + let currentWorkingDir = { path: homedir() } + printCommandsPromts(currentWorkingDir.path); + process.stdin.on("data", async (input) => { + const [command, ...args] = input.toString().trim().split(' ') + printCommandsPromts(currentWorkingDir.path); + await commandDispatch(command, args, currentWorkingDir, userName); + }); + + process.on('SIGINT', () => { + commandDispatch('SIGINT', '', userName) + process.exit(1); + }) +} + +await fileManager(); \ No newline at end of file diff --git a/package.json b/package.json index 61220743f3..c1f83bfa24 100644 --- a/package.json +++ b/package.json @@ -2,25 +2,12 @@ "name": "node-nodejs-basics", "version": "1.0.0", "description": "This repository is the part of nodejs-assignments https://github.com/AlreadyBored/nodejs-assignments", + "engines": { + "node": ">=20.0.0" + }, "type": "module", "scripts": { - "cli:args": "node src/cli/args.js --some-arg value1 --other 1337 --arg2 42", - "cli:env": "npx cross-env SOME=any RSS_foo=bar RSS_bar=baz node src/cli/env.js", - "cp": "node src/cp/cp.js", - "fs:copy": "node src/fs/copy.js", - "fs:create": "node src/fs/create.js", - "fs:delete": "node src/fs/delete.js", - "fs:list": "node src/fs/list.js", - "fs:read": "node src/fs/read.js", - "fs:rename": "node src/fs/rename.js", - "hash": "node src/hash/calcHash.js", - "modules": "node src/modules/esm.mjs", - "streams:read": "node src/streams/read.js", - "streams:transform": "node src/streams/transform.js", - "streams:write": "node src/streams/write.js", - "wt": "node src/wt/main.js", - "zip:compress": "node src/zip/compress.js", - "zip:decompress": "node src/zip/decompress.js" + "start": "node ./bin/file-manager.js" }, "repository": { "type": "git", diff --git a/src/cli/args.js b/src/cli/args.js deleted file mode 100644 index 8283f7f7aa..0000000000 --- a/src/cli/args.js +++ /dev/null @@ -1,5 +0,0 @@ -const parseArgs = () => { - // Write your code here -}; - -parseArgs(); \ No newline at end of file diff --git a/src/cli/env.js b/src/cli/env.js deleted file mode 100644 index fe4aa4a8df..0000000000 --- a/src/cli/env.js +++ /dev/null @@ -1,5 +0,0 @@ -const parseEnv = () => { - // Write your code here -}; - -parseEnv(); \ No newline at end of file diff --git a/src/cp/cp.js b/src/cp/cp.js deleted file mode 100644 index ac9116a73f..0000000000 --- a/src/cp/cp.js +++ /dev/null @@ -1,5 +0,0 @@ -const spawnChildProcess = async (args) => { - // Write your code here -}; - -spawnChildProcess(); \ No newline at end of file diff --git a/src/cp/files/script.js b/src/cp/files/script.js deleted file mode 100644 index 44a0622fbc..0000000000 --- a/src/cp/files/script.js +++ /dev/null @@ -1,12 +0,0 @@ -const args = process.argv.slice(2); - -console.log(`Total number of arguments is ${args.length}`); -console.log(`Arguments: ${JSON.stringify(args)}`); - -const echoInput = (chunk) => { - const chunkStringified = chunk.toString(); - if (chunkStringified.includes('CLOSE')) process.exit(0); - process.stdout.write(`Received from master process: ${chunk.toString()}\n`) -}; - -process.stdin.on('data', echoInput); diff --git a/src/fs/copy.js b/src/fs/copy.js deleted file mode 100644 index 95ed6701bb..0000000000 --- a/src/fs/copy.js +++ /dev/null @@ -1,5 +0,0 @@ -const copy = async () => { - // Write your code here -}; - -copy(); \ No newline at end of file diff --git a/src/fs/create.js b/src/fs/create.js deleted file mode 100644 index 8d18cf9fc2..0000000000 --- a/src/fs/create.js +++ /dev/null @@ -1,5 +0,0 @@ -const create = async () => { - // Write your code here -}; - -await create(); \ No newline at end of file diff --git a/src/fs/delete.js b/src/fs/delete.js deleted file mode 100644 index 4718dbc4c5..0000000000 --- a/src/fs/delete.js +++ /dev/null @@ -1,5 +0,0 @@ -const remove = async () => { - // Write your code here -}; - -await remove(); \ No newline at end of file diff --git a/src/fs/files/dontLookAtMe.txt b/src/fs/files/dontLookAtMe.txt deleted file mode 100644 index 8979bab743..0000000000 --- a/src/fs/files/dontLookAtMe.txt +++ /dev/null @@ -1 +0,0 @@ -What are you looking at?! \ No newline at end of file diff --git a/src/fs/files/fileToRead.txt b/src/fs/files/fileToRead.txt deleted file mode 100644 index 5d66c332d6..0000000000 --- a/src/fs/files/fileToRead.txt +++ /dev/null @@ -1,7 +0,0 @@ -My content -should -be -printed -into -console -! \ No newline at end of file diff --git a/src/fs/files/fileToRemove.txt b/src/fs/files/fileToRemove.txt deleted file mode 100644 index 43e64cd45c..0000000000 --- a/src/fs/files/fileToRemove.txt +++ /dev/null @@ -1 +0,0 @@ -How dare you! \ No newline at end of file diff --git a/src/fs/files/hello.txt b/src/fs/files/hello.txt deleted file mode 100644 index 4e65f7775f..0000000000 --- a/src/fs/files/hello.txt +++ /dev/null @@ -1 +0,0 @@ -Hello Node.js \ No newline at end of file diff --git a/src/fs/files/wrongFilename.txt b/src/fs/files/wrongFilename.txt deleted file mode 100644 index 38cca5db19..0000000000 --- a/src/fs/files/wrongFilename.txt +++ /dev/null @@ -1,3 +0,0 @@ -# This is a file with a wrong filename - -Hello from **markdown**! \ No newline at end of file diff --git a/src/fs/list.js b/src/fs/list.js deleted file mode 100644 index c0a83dea15..0000000000 --- a/src/fs/list.js +++ /dev/null @@ -1,5 +0,0 @@ -const list = async () => { - // Write your code here -}; - -await list(); \ No newline at end of file diff --git a/src/fs/read.js b/src/fs/read.js deleted file mode 100644 index 52c78cc6ee..0000000000 --- a/src/fs/read.js +++ /dev/null @@ -1,5 +0,0 @@ -const read = async () => { - // Write your code here -}; - -await read(); \ No newline at end of file diff --git a/src/fs/rename.js b/src/fs/rename.js deleted file mode 100644 index 2bb99ecdb5..0000000000 --- a/src/fs/rename.js +++ /dev/null @@ -1,5 +0,0 @@ -const rename = async () => { - // Write your code here -}; - -await rename(); \ No newline at end of file diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js deleted file mode 100644 index 450f8f72e2..0000000000 --- a/src/hash/calcHash.js +++ /dev/null @@ -1,5 +0,0 @@ -const calculateHash = async () => { - // Write your code here -}; - -await calculateHash(); \ No newline at end of file diff --git a/src/hash/files/fileToCalculateHashFor.txt b/src/hash/files/fileToCalculateHashFor.txt deleted file mode 100644 index 08f56564f8..0000000000 --- a/src/hash/files/fileToCalculateHashFor.txt +++ /dev/null @@ -1 +0,0 @@ -Calculate hash for me! \ No newline at end of file diff --git a/src/modules/cjsToEsm.cjs b/src/modules/cjsToEsm.cjs deleted file mode 100644 index 8b7be2a48b..0000000000 --- a/src/modules/cjsToEsm.cjs +++ /dev/null @@ -1,40 +0,0 @@ -const path = require('path'); -const { release, version } = require('os'); -const { createServer: createServerHttp } = require('http'); -require('./files/c'); - -const random = Math.random(); - -let unknownObject; - -if (random > 0.5) { - unknownObject = require('./files/a.json'); -} else { - unknownObject = require('./files/b.json'); -} - -console.log(`Release ${release()}`); -console.log(`Version ${version()}`); -console.log(`Path segment separator is "${path.sep}"`); - -console.log(`Path to current file is ${__filename}`); -console.log(`Path to current directory is ${__dirname}`); - -const myServer = createServerHttp((_, res) => { - res.end('Request accepted'); -}); - -const PORT = 3000; - -console.log(unknownObject); - -myServer.listen(PORT, () => { - console.log(`Server is listening on port ${PORT}`); - console.log('To terminate it, use Ctrl+C combination'); -}); - -module.exports = { - unknownObject, - myServer, -}; - diff --git a/src/modules/files/a.json b/src/modules/files/a.json deleted file mode 100644 index 7878a209f6..0000000000 --- a/src/modules/files/a.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "a": 1, - "b": 2, - "c": 3 -} \ No newline at end of file diff --git a/src/modules/files/b.json b/src/modules/files/b.json deleted file mode 100644 index 5214b8c93d..0000000000 --- a/src/modules/files/b.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "a": 11, - "b": 22, - "c": 33 -} \ No newline at end of file diff --git a/src/modules/files/c.js b/src/modules/files/c.js deleted file mode 100644 index ac09fab173..0000000000 --- a/src/modules/files/c.js +++ /dev/null @@ -1 +0,0 @@ -console.log('Hello from c.js!'); \ No newline at end of file diff --git a/src/streams/files/fileToRead.txt b/src/streams/files/fileToRead.txt deleted file mode 100644 index c7e8d132a1..0000000000 --- a/src/streams/files/fileToRead.txt +++ /dev/null @@ -1 +0,0 @@ -This file should be read using Streams API \ No newline at end of file diff --git a/src/streams/files/fileToWrite.txt b/src/streams/files/fileToWrite.txt deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/src/streams/read.js b/src/streams/read.js deleted file mode 100644 index 52c78cc6ee..0000000000 --- a/src/streams/read.js +++ /dev/null @@ -1,5 +0,0 @@ -const read = async () => { - // Write your code here -}; - -await read(); \ No newline at end of file diff --git a/src/streams/transform.js b/src/streams/transform.js deleted file mode 100644 index 315fc6597f..0000000000 --- a/src/streams/transform.js +++ /dev/null @@ -1,5 +0,0 @@ -const transform = async () => { - // Write your code here -}; - -await transform(); \ No newline at end of file diff --git a/src/streams/write.js b/src/streams/write.js deleted file mode 100644 index fc917160a2..0000000000 --- a/src/streams/write.js +++ /dev/null @@ -1,5 +0,0 @@ -const write = async () => { - // Write your code here -}; - -await write(); \ No newline at end of file diff --git a/src/utils/commandDispatch.js b/src/utils/commandDispatch.js new file mode 100644 index 0000000000..3cca7716cd --- /dev/null +++ b/src/utils/commandDispatch.js @@ -0,0 +1,94 @@ +import { compressFile } from "./compressFile.js"; +import { createNewFile } from "./createNewFile.js"; +import { decompressFile } from "./decompressFile.js"; +import { deleteFile } from "./deleteFile.js"; +import { getUpperDir } from "./getUpperDir.js"; +import { goToDir } from "./goToDir.js"; +import { moveFile } from "./moveFile.js"; +import { printBye } from "./printBye.js"; +import { printFileContent } from "./printFileContent.js"; +import { printHash } from "./printHash.js"; +import { printListOfFiles } from "./printListOfFiles.js"; +import { printOS } from "./printOS.js"; +import { renameFile } from "./renameFile.js"; + +const commandList = { + ".exit": (_, args, [userName]) => { + printBye(userName) + process.exit(1); + }, + "SIGINT": (_, args, [userName]) => { + printBye(userName) + process.exit(1); + }, + "up": async (currentPath) => { + currentPath.path = await getUpperDir(currentPath.path); + + }, + "cd": async (currentPath, [destination]) => { + currentPath.path = await goToDisr(currentPath.path, destination); + }, + "ls": async ({ path }) => { + await printListOfFiles(path); + + }, + "cat": async ({ path }, [filepath]) => { + await printFileContent(path, filepath); + }, + "add": async ({ path }, [filepath]) => { + await createNewFile(path, filepath); + }, + "rn": async ({ path }, [oldpath, newpath]) => { + await renameFile(path, oldpath, newpath) + }, + "mv": async ({ path }, [oldpath, newpath]) => { + await moveFile(path, oldpath, newpath) + }, + "rm": async ({ path }, [filepath]) => { + await deleteFile(path, filepath) + }, + "os": ({ path }, [optionType]) => { + printOS(path, optionType) + }, + "hash": async ({ path }, [filepath]) => { + await printHash(path, filepath); + }, + "compress": async ({ path }, [oldpath, newpath]) => { + await compressFile(path, oldpath, newpath) + }, + "decompress": async ({ path }, [archivepath, decompresspath]) => { + await decompressFile(path, archivepath, decompresspath) + } +} + +export const commandPrompts = { + ".exit": "Exit to File Manager", + "up": "Go to parent DIR", + "cd": "Go to dirpath ", + "ls": "Print list of files, from current DIR", + "cat": "Print file content by path ", + "add": "Create new empty file , from current DIR", + "rn": "Rename file ", + "mv": "Move file ", + "rm": "Delete file by ", + "os": "Print OS information. By setting --EOL, --cpus, --homedir, --username, --architecture", + "hash": "Print hash of file by filepath ", + "compress": "Compress file ", + "decompress": "Decompress file " +} + +export const printCommandsPromts = (currentWorkingDir) => { + const prompts = Object.entries(commandPrompts).map(([command, description]) => `${command} - ${description}`).join('\n') + console.log(`\nYou are currently in ${currentWorkingDir}`) + + console.log('\nAvailable commands list\n'); + console.log(prompts) +} + +export const commandDispatch = async (command, args, currentWorkingDir = '', usename = '') => { + if (command in commandList) { + await commandList[command](currentWorkingDir, args, usename); + printCommandsPromts(currentWorkingDir.path) + } + else console.error('Invalid input') +} \ No newline at end of file diff --git a/src/utils/compressFile.js b/src/utils/compressFile.js new file mode 100644 index 0000000000..468fd1ddc1 --- /dev/null +++ b/src/utils/compressFile.js @@ -0,0 +1,45 @@ +import path from 'node:path' +import { createReadStream, createWriteStream } from 'node:fs'; +import { pipeline } from "node:stream/promises"; +import { access, stat } from 'fs/promises' +import { createBrotliCompress } from 'node:zlib'; + +export const compressFile = async (currentPath, oldname, newpath) => { + if (!oldname || !newpath) { + console.error('Invalid input') + return; + } + + const absoldpath = path.resolve(currentPath, oldname) + const absnewpath = path.resolve(currentPath, newpath) + + const isCreated = await access(absoldpath).then(() => true).catch(() => false); + + if (!isCreated) { + console.error("File doesn't exists") + return; + } + + const stats = await stat(absoldpath) + + if (stats.isDirectory()) { + console.error('It is directory') + return + } + + const isDestinationUse = await access(absnewpath).then(() => true).catch(() => false); + + if (isDestinationUse) { + console.error('New destination already use') + return; + } + + const brotli = createBrotliCompress(); + const source = createReadStream(absoldpath); + const destination = createWriteStream(absnewpath); + try { + await pipeline(source, brotli, destination); + } catch(_) { + console.error('Cannot compress file') + } +} \ No newline at end of file diff --git a/src/utils/createNewFile.js b/src/utils/createNewFile.js new file mode 100644 index 0000000000..684531e51a --- /dev/null +++ b/src/utils/createNewFile.js @@ -0,0 +1,22 @@ +import { access, writeFile } from 'fs/promises'; +import path from 'node:path' + +export const createNewFile = async (currentPath, filepath) => { + if (!filepath) { + console.error('Invalid input') + return; + } + + const newpath = path.resolve(currentPath, filepath) + + const isFileCreated = await access(newpath).then(() => true).catch(() => false); + + if (isFileCreated) { + console.error('File already created'); + } + try { + writeFile(newpath, '') + } catch(_) { + console.error('Cannot create file') + } +} \ No newline at end of file diff --git a/src/utils/decompressFile.js b/src/utils/decompressFile.js new file mode 100644 index 0000000000..b471aeb5a7 --- /dev/null +++ b/src/utils/decompressFile.js @@ -0,0 +1,45 @@ +import path from 'node:path' +import { createReadStream, createWriteStream } from 'node:fs'; +import { pipeline } from "node:stream/promises"; +import { access, stat } from 'fs/promises' +import { createBrotliDecompress } from 'node:zlib'; + +export const decompressFile = async (currentPath, oldname, newpath) => { + if (!oldname || !newpath) { + console.error('Invalid input') + return; + } + + const arhivePath = path.resolve(currentPath, oldname) + const decompressPath = path.resolve(currentPath, newpath) + + const isCreated = await access(arhivePath).then(() => true).catch(() => false); + + if (!isCreated) { + console.error("File doesn't exists") + return; + } + + const stats = await stat(arhivePath) + + if (stats.isDirectory()) { + console.error('It is directory') + return + } + + const isDestinationUse = await access(decompressPath).then(() => true).catch(() => false); + + if (isDestinationUse) { + console.error('New destination already use') + return; + } + + const brotli = createBrotliDecompress(); + const source = createReadStream(arhivePath); + const destination = createWriteStream(decompressPath); + try { + await pipeline(source, brotli, destination); + } catch(_) { + console.error('Cannot compress file') + } +} \ No newline at end of file diff --git a/src/utils/deleteFile.js b/src/utils/deleteFile.js new file mode 100644 index 0000000000..36782ba80d --- /dev/null +++ b/src/utils/deleteFile.js @@ -0,0 +1,32 @@ +import path from 'node:path' +import { access, stat, rm } from 'fs/promises' + +export const deleteFile = async (currentPath, filepath) => { + if (!filepath) { + console.error('Invalid input') + return; + } + + const absfilepath = path.resolve(currentPath, filepath); + + const isCreated = await access(absfilepath).then(() => true).catch(() => false); + + if (!isCreated) { + console.error("File doesn't exists") + return; + } + + const stats = await stat(absfilepath) + + if (stats.isDirectory()) { + console.error('It is directory') + return + } + + + try { + await rm(absfilepath) + } catch(_) { + console.error('Cannot delete file') + } +} \ No newline at end of file diff --git a/src/utils/getUpperDir.js b/src/utils/getUpperDir.js new file mode 100644 index 0000000000..80325c44db --- /dev/null +++ b/src/utils/getUpperDir.js @@ -0,0 +1,10 @@ +import path from 'node:path' +import { homedir } from 'node:os'; + + +export const getUpperDir = (currentPath) => { + const upperDir = path.resolve('..', currentPath); + if (upperDir === homedir()) return currentPath; + + return upperDir +} \ No newline at end of file diff --git a/src/utils/getUserName.js b/src/utils/getUserName.js new file mode 100644 index 0000000000..6d3ba2b8ef --- /dev/null +++ b/src/utils/getUserName.js @@ -0,0 +1 @@ +export const getUserName = (cliUserName) => cliUserName.split('=')[1]; \ No newline at end of file diff --git a/src/utils/goToDir.js b/src/utils/goToDir.js new file mode 100644 index 0000000000..5ed8edd63a --- /dev/null +++ b/src/utils/goToDir.js @@ -0,0 +1,34 @@ +import path from 'node:path'; +import { stat, access } from 'fs/promises'; +import { homedir } from 'node:os'; + +export const goToDir = async (currentPath, destinationPath) => { + if (!destinationPath) { + console.error('Invalid input') + return; + } + + let newPath = path.resolve(currentPath, destinationPath) + const relative = path.relative(homedir(), newPath); + + if (relative && relative.startsWith('..')) return currentPath; + try { + const stats = await stat(newPath) + if (stats.isDirectory()) { + newPath = path.resolve(currentPath, destinationPath); + try { + await access(newPath); + return newPath + } catch (e) { + console.error("This directory isn't created") + return currentPath + } + } else { + console.error("\nIt's not directory") + return currentPath + } + } catch(e) { + console.error(e.message) + return currentPath + } +} \ No newline at end of file diff --git a/src/utils/moveFile.js b/src/utils/moveFile.js new file mode 100644 index 0000000000..d167a7b79c --- /dev/null +++ b/src/utils/moveFile.js @@ -0,0 +1,44 @@ +import path from 'node:path' +import { createReadStream, createWriteStream } from 'node:fs'; +import { pipeline } from "node:stream/promises"; +import { access, stat, rm } from 'fs/promises' + +export const moveFile = async (currentPath, oldpath, newpath) => { + if (!oldpath || !newpath) { + console.error('Invalid input') + return; + } + + const absoldpath = path.resolve(currentPath, oldpath) + const absnewpath = path.resolve(currentPath, newpath) + + const isCreated = await access(absoldpath).then(() => true).catch(() => false); + + if (!isCreated) { + console.error("File doesn't exists") + return; + } + + const stats = await stat(absoldpath) + + if (stats.isDirectory()) { + console.error('It is directory') + return + } + + const isDestinationUse = await access(absnewpath).then(() => true).catch(() => false); + + if (isDestinationUse) { + console.error('New destination already use') + return; + } + + const sourceReadStream = createReadStream(absoldpath) + const destinationWritableStream = createWriteStream(absnewpath) + try { + await pipeline(sourceReadStream, destinationWritableStream) + await rm(absoldpath); + } catch(_) { + console.error("Cannot move file") + } +} \ No newline at end of file diff --git a/src/utils/printBye.js b/src/utils/printBye.js new file mode 100644 index 0000000000..0daad64ae5 --- /dev/null +++ b/src/utils/printBye.js @@ -0,0 +1,3 @@ +export const printBye = (username) => { + console.log(`Thank you for using File Manager, ${username}, goodbye!`) +} \ No newline at end of file diff --git a/src/utils/printFileContent.js b/src/utils/printFileContent.js new file mode 100644 index 0000000000..adb6370de3 --- /dev/null +++ b/src/utils/printFileContent.js @@ -0,0 +1,31 @@ +import { createReadStream } from 'node:fs'; +import { access } from 'fs/promises' +import path from 'node:path' + +export const printFileContent = async (currentPath, filepath) => { + if (!filepath) { + console.error('Invalid input') + return; + } + + try { + const absfilepath = path.resolve(currentPath, filepath); + await access(absfilepath) + const filecontent = createReadStream(absfilepath) + const promise = new Promise((resolve) => { + filecontent.on('data', (chunk) => { + console.log(chunk.toString()) + }) + filecontent.on('close', () => { + resolve(); + }) + }) + + await promise; + + filecontent.destroy(); + + } catch(e) { + console.error(e) + } +} \ No newline at end of file diff --git a/src/utils/printHash.js b/src/utils/printHash.js new file mode 100644 index 0000000000..f78c4fbfcd --- /dev/null +++ b/src/utils/printHash.js @@ -0,0 +1,49 @@ +import path from 'node:path' +import { access,stat } from 'fs/promises' +import { createHash } from 'node:crypto'; +import { createReadStream } from 'node:fs'; + +export const printHash = async (currentPath, filepath) => { + if (!filepath) { + console.error('Invalid input') + return; + } + + const absfilepath = path.resolve(currentPath, filepath); + + const isCreated = await access(absfilepath).then(() => true).catch(() => false); + + if (!isCreated) { + console.error("File doesn't exists") + return; + } + + const stats = await stat(absfilepath) + + if (stats.isDirectory()) { + console.error('It is directory') + return + } + + const promise = new Promise((resolve, reject) => { + const hash = createHash('sha256'); + const filecontent = createReadStream(absfilepath) + filecontent.on('error', reject); + filecontent.on('data', chunk => hash.update(chunk)); + filecontent.on('end', () => { + resolve(hash.digest('hex')) + filecontent.destroy(); + }) + }) + + try { + const hash = await promise; + console.log(`Hash is - ${hash}`) + } catch(_) { + console.error('Cannot calculate hash') + } + + // filecontent.destroy(); + + // console.log(hash.setEncoding('hex')) +} \ No newline at end of file diff --git a/src/utils/printListOfFiles.js b/src/utils/printListOfFiles.js new file mode 100644 index 0000000000..b900c59c8f --- /dev/null +++ b/src/utils/printListOfFiles.js @@ -0,0 +1,26 @@ +import { readdir, stat } from 'fs/promises' +import path from 'node:path' + +function Named(Name, Type) { + this.Name = Name; + this.Type = Type; +} + +export const printListOfFiles = async (dirPath) => { + const files = await readdir(dirPath) + + const formattedData = files.map(async (recordPath) => { + const absoluteRecordPath = path.resolve(dirPath, recordPath) + const stats = await stat(absoluteRecordPath) + if (stats.isFile()) { + return [recordPath, 'file'] + } else { + return [recordPath, 'directory'] + } + }) + + const listOfDirectory = await Promise.all(formattedData) + + console.table(listOfDirectory.map(record => new Named(...record))); + +} \ No newline at end of file diff --git a/src/utils/printOS.js b/src/utils/printOS.js new file mode 100644 index 0000000000..474cacdf62 --- /dev/null +++ b/src/utils/printOS.js @@ -0,0 +1,29 @@ +import os from 'node:os' + +export const printOS = (_, type) => { + if (!type) { + console.error('Invalid input') + return; + } + + switch (type) { + case "--EOL": { + console.log(`EOL is - ${JSON.stringify(os.EOL)}`) + break; + } + case "--cpus": + console.log(`CPUS - ${JSON.stringify(os.cpus())}`) + break; + case "--homedir": + console.log(`Homedir is - ${JSON.stringify(os.homedir())}`) + break; + case "--username": + console.log(`User - ${JSON.stringify(os.userInfo().username)}`) + break; + case "--architecture": + console.log(`Architecture is - ${JSON.stringify(os.arch())}`) + break; + default: + console.error('Invalid input') + } +} \ No newline at end of file diff --git a/src/utils/printWelcome.js b/src/utils/printWelcome.js new file mode 100644 index 0000000000..e10d8747fe --- /dev/null +++ b/src/utils/printWelcome.js @@ -0,0 +1,3 @@ +export const printWelcome = (usename) => { + console.log(`Welcome to the File Manager, ${usename}!`) +} \ No newline at end of file diff --git a/src/utils/renameFile.js b/src/utils/renameFile.js new file mode 100644 index 0000000000..d80e6a7a34 --- /dev/null +++ b/src/utils/renameFile.js @@ -0,0 +1,32 @@ +import { stat, rename, access } from 'fs/promises' +import path from 'node:path' + +export const renameFile = async (currentPath, oldPath, newPath) => { + if (!oldPath || !newPath) { + console.error('Invalid input') + return; + } + + const absoldpath = path.resolve(currentPath, oldPath) + const absnewpath = path.resolve(currentPath, newPath) + + const isCreated = await access(absoldpath).then(() => true).catch(() => false); + + if (!isCreated) { + console.error("File doesn't exists") + return; + } + + const stats = await stat(absoldpath) + + if (stats.isDirectory()) { + console.error('It is directory') + return + } + + try { + await rename(absoldpath, absnewpath) + } catch(_) { + console.error('Cannot rename file') + } +} \ No newline at end of file diff --git a/src/wt/main.js b/src/wt/main.js deleted file mode 100644 index 37d80484ec..0000000000 --- a/src/wt/main.js +++ /dev/null @@ -1,5 +0,0 @@ -const performCalculations = async () => { - // Write your code here -}; - -await performCalculations(); \ No newline at end of file diff --git a/src/wt/worker.js b/src/wt/worker.js deleted file mode 100644 index 441b2154f8..0000000000 --- a/src/wt/worker.js +++ /dev/null @@ -1,8 +0,0 @@ -// n should be received from main thread -const nthFibonacci = (n) => n < 2 ? n : nthFibonacci(n - 1) + nthFibonacci(n - 2); - -const sendResult = () => { - // This function sends result of nthFibonacci computations to main thread -}; - -sendResult(); \ No newline at end of file diff --git a/src/zip/compress.js b/src/zip/compress.js deleted file mode 100644 index bb328f43c6..0000000000 --- a/src/zip/compress.js +++ /dev/null @@ -1,5 +0,0 @@ -const compress = async () => { - // Write your code here -}; - -await compress(); \ No newline at end of file diff --git a/src/zip/decompress.js b/src/zip/decompress.js deleted file mode 100644 index 69f6c345f8..0000000000 --- a/src/zip/decompress.js +++ /dev/null @@ -1,5 +0,0 @@ -const decompress = async () => { - // Write your code here -}; - -await decompress(); \ No newline at end of file diff --git a/src/zip/files/fileToCompress.txt b/src/zip/files/fileToCompress.txt deleted file mode 100644 index 4d4efc82fe..0000000000 --- a/src/zip/files/fileToCompress.txt +++ /dev/null @@ -1 +0,0 @@ -Compress me! \ No newline at end of file