From 8cf742297a76e36d1e80a20a1f39e608aa499ca5 Mon Sep 17 00:00:00 2001 From: Alex Smirnov Date: Tue, 15 Oct 2024 02:28:59 +0300 Subject: [PATCH 1/4] finish filemanager --- Readme copy.md | 3 + bin/file-manager.js | 27 +++++++++ package copy.json | 27 +++++++++ src copy/utils/commandDispatch.js | 94 ++++++++++++++++++++++++++++++ src copy/utils/compressFile.js | 40 +++++++++++++ src copy/utils/createNewFile.js | 17 ++++++ src copy/utils/decompressFile.js | 40 +++++++++++++ src copy/utils/deleteFile.js | 27 +++++++++ src copy/utils/getUpperDir.js | 10 ++++ src copy/utils/getUserName.js | 1 + src copy/utils/goToDir.js | 29 +++++++++ src copy/utils/moveFile.js | 40 +++++++++++++ src copy/utils/printBye.js | 3 + src copy/utils/printFileContent.js | 26 +++++++++ src copy/utils/printHash.js | 44 ++++++++++++++ src copy/utils/printListOfFiles.js | 41 +++++++++++++ src copy/utils/printOS.js | 24 ++++++++ src copy/utils/printWelcome.js | 3 + src copy/utils/renameFile.js | 27 +++++++++ 19 files changed, 523 insertions(+) create mode 100644 Readme copy.md create mode 100644 bin/file-manager.js create mode 100644 package copy.json create mode 100644 src copy/utils/commandDispatch.js create mode 100644 src copy/utils/compressFile.js create mode 100644 src copy/utils/createNewFile.js create mode 100644 src copy/utils/decompressFile.js create mode 100644 src copy/utils/deleteFile.js create mode 100644 src copy/utils/getUpperDir.js create mode 100644 src copy/utils/getUserName.js create mode 100644 src copy/utils/goToDir.js create mode 100644 src copy/utils/moveFile.js create mode 100644 src copy/utils/printBye.js create mode 100644 src copy/utils/printFileContent.js create mode 100644 src copy/utils/printHash.js create mode 100644 src copy/utils/printListOfFiles.js create mode 100644 src copy/utils/printOS.js create mode 100644 src copy/utils/printWelcome.js create mode 100644 src copy/utils/renameFile.js diff --git a/Readme copy.md b/Readme copy.md new file mode 100644 index 0000000000..611a505f49 --- /dev/null +++ b/Readme copy.md @@ -0,0 +1,3 @@ +# Node.js basics + +## !!! Please don't submit Pull Requests to this repository !!! 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 copy.json b/package copy.json new file mode 100644 index 0000000000..c1f83bfa24 --- /dev/null +++ b/package copy.json @@ -0,0 +1,27 @@ +{ + "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": { + "start": "node ./bin/file-manager.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/AlreadyBored/node-nodejs-basics.git" + }, + "keywords": [ + "nodejs", + "assignments", + "alreadybored" + ], + "author": "alreadybored", + "license": "ISC", + "bugs": { + "url": "https://github.com/AlreadyBored/node-nodejs-basics/issues" + }, + "homepage": "https://github.com/AlreadyBored/node-nodejs-basics#readme" +} diff --git a/src copy/utils/commandDispatch.js b/src copy/utils/commandDispatch.js new file mode 100644 index 0000000000..5ae55a3ac0 --- /dev/null +++ b/src copy/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 goToDir(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 copy/utils/compressFile.js b/src copy/utils/compressFile.js new file mode 100644 index 0000000000..f2b0b89368 --- /dev/null +++ b/src copy/utils/compressFile.js @@ -0,0 +1,40 @@ +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) => { + 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 copy/utils/createNewFile.js b/src copy/utils/createNewFile.js new file mode 100644 index 0000000000..20eb2cf155 --- /dev/null +++ b/src copy/utils/createNewFile.js @@ -0,0 +1,17 @@ +import { access, writeFile } from 'fs/promises'; +import path from 'node:path' + +export const createNewFile = async (currentPath, filepath) => { + 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 copy/utils/decompressFile.js b/src copy/utils/decompressFile.js new file mode 100644 index 0000000000..59e4faeefb --- /dev/null +++ b/src copy/utils/decompressFile.js @@ -0,0 +1,40 @@ +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) => { + 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 copy/utils/deleteFile.js b/src copy/utils/deleteFile.js new file mode 100644 index 0000000000..20e31e03f8 --- /dev/null +++ b/src copy/utils/deleteFile.js @@ -0,0 +1,27 @@ +import path from 'node:path' +import { access, stat, rm } from 'fs/promises' + +export const deleteFile = async (currentPath, filepath) => { + 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 copy/utils/getUpperDir.js b/src copy/utils/getUpperDir.js new file mode 100644 index 0000000000..80325c44db --- /dev/null +++ b/src copy/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 copy/utils/getUserName.js b/src copy/utils/getUserName.js new file mode 100644 index 0000000000..6d3ba2b8ef --- /dev/null +++ b/src copy/utils/getUserName.js @@ -0,0 +1 @@ +export const getUserName = (cliUserName) => cliUserName.split('=')[1]; \ No newline at end of file diff --git a/src copy/utils/goToDir.js b/src copy/utils/goToDir.js new file mode 100644 index 0000000000..56b054d2e2 --- /dev/null +++ b/src copy/utils/goToDir.js @@ -0,0 +1,29 @@ +import path from 'node:path'; +import { stat, access } from 'fs/promises'; +import { homedir } from 'node:os'; + +export const goToDir = async (currentPath, destinationPath = '') => { + 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 copy/utils/moveFile.js b/src copy/utils/moveFile.js new file mode 100644 index 0000000000..f462e39249 --- /dev/null +++ b/src copy/utils/moveFile.js @@ -0,0 +1,40 @@ +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) => { + 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) + console.log(absoldpath, 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 copy/utils/printBye.js b/src copy/utils/printBye.js new file mode 100644 index 0000000000..0daad64ae5 --- /dev/null +++ b/src copy/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 copy/utils/printFileContent.js b/src copy/utils/printFileContent.js new file mode 100644 index 0000000000..64278c9454 --- /dev/null +++ b/src copy/utils/printFileContent.js @@ -0,0 +1,26 @@ +import { createReadStream } from 'node:fs'; +import { access } from 'fs/promises' +import path from 'node:path' + +export const printFileContent = async (currentPath, filepath) => { + 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 copy/utils/printHash.js b/src copy/utils/printHash.js new file mode 100644 index 0000000000..b18b3c7ed0 --- /dev/null +++ b/src copy/utils/printHash.js @@ -0,0 +1,44 @@ +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) => { + 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 copy/utils/printListOfFiles.js b/src copy/utils/printListOfFiles.js new file mode 100644 index 0000000000..4d490ead51 --- /dev/null +++ b/src copy/utils/printListOfFiles.js @@ -0,0 +1,41 @@ +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))); + // const filteredList = listOfDirectory.filter(record => record) + // const resultList = filteredList.map(async (currentPath) => { + // try { + // console.log(currentPath) + // const pathStat = await stat(currentPath); + // console.log(pa) + // if (pathStat.isDirectory()) return 'directory' + // if (pathStat.isFile()) return 'file' + // return pathStat.isDirectory() ? 'directory' : 'file'; + // } catch(_) { + // return null + // } + + // }); + // const result = await Promise.all(resultList) + // console.log(result) +} \ No newline at end of file diff --git a/src copy/utils/printOS.js b/src copy/utils/printOS.js new file mode 100644 index 0000000000..530b817f91 --- /dev/null +++ b/src copy/utils/printOS.js @@ -0,0 +1,24 @@ +import os from 'node:os' + +export const printOS = (_, type) => { + 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 copy/utils/printWelcome.js b/src copy/utils/printWelcome.js new file mode 100644 index 0000000000..e10d8747fe --- /dev/null +++ b/src copy/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 copy/utils/renameFile.js b/src copy/utils/renameFile.js new file mode 100644 index 0000000000..72774ce038 --- /dev/null +++ b/src copy/utils/renameFile.js @@ -0,0 +1,27 @@ +import { stat, rename, access } from 'fs/promises' +import path from 'node:path' + +export const renameFile = async (currentPath, oldPath, newPath) => { + 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 From f9a4611cc84e551b5a7f29322a25f08053e8eae0 Mon Sep 17 00:00:00 2001 From: Alex Smirnov Date: Tue, 15 Oct 2024 02:44:48 +0300 Subject: [PATCH 2/4] add errors --- Readme copy.md | 3 -- package copy.json | 27 -------------- package.json | 21 +++-------- src/cli/args.js | 5 --- src/cli/env.js | 5 --- src/cp/cp.js | 5 --- src/cp/files/script.js | 12 ------- src/fs/copy.js | 5 --- src/fs/create.js | 5 --- src/fs/delete.js | 5 --- src/fs/files/dontLookAtMe.txt | 1 - src/fs/files/fileToRead.txt | 7 ---- src/fs/files/fileToRemove.txt | 1 - src/fs/files/hello.txt | 1 - src/fs/files/wrongFilename.txt | 3 -- src/fs/list.js | 5 --- src/fs/read.js | 5 --- src/fs/rename.js | 5 --- src/hash/calcHash.js | 5 --- src/hash/files/fileToCalculateHashFor.txt | 1 - src/modules/cjsToEsm.cjs | 40 --------------------- src/modules/files/a.json | 5 --- src/modules/files/b.json | 5 --- src/modules/files/c.js | 1 - src/streams/files/fileToRead.txt | 1 - src/streams/files/fileToWrite.txt | 0 src/streams/read.js | 5 --- src/streams/transform.js | 5 --- src/streams/write.js | 5 --- {src copy => src}/utils/commandDispatch.js | 0 {src copy => src}/utils/compressFile.js | 5 +++ {src copy => src}/utils/createNewFile.js | 5 +++ {src copy => src}/utils/decompressFile.js | 5 +++ {src copy => src}/utils/deleteFile.js | 5 +++ {src copy => src}/utils/getUpperDir.js | 0 {src copy => src}/utils/getUserName.js | 0 {src copy => src}/utils/goToDir.js | 7 +++- {src copy => src}/utils/moveFile.js | 5 +++ {src copy => src}/utils/printBye.js | 0 {src copy => src}/utils/printFileContent.js | 5 +++ {src copy => src}/utils/printHash.js | 5 +++ {src copy => src}/utils/printListOfFiles.js | 0 {src copy => src}/utils/printOS.js | 5 +++ {src copy => src}/utils/printWelcome.js | 0 {src copy => src}/utils/renameFile.js | 5 +++ src/wt/main.js | 5 --- src/wt/worker.js | 8 ----- src/zip/compress.js | 5 --- src/zip/decompress.js | 5 --- src/zip/files/fileToCompress.txt | 1 - 50 files changed, 55 insertions(+), 215 deletions(-) delete mode 100644 Readme copy.md delete mode 100644 package copy.json delete mode 100644 src/cli/args.js delete mode 100644 src/cli/env.js delete mode 100644 src/cp/cp.js delete mode 100644 src/cp/files/script.js delete mode 100644 src/fs/copy.js delete mode 100644 src/fs/create.js delete mode 100644 src/fs/delete.js delete mode 100644 src/fs/files/dontLookAtMe.txt delete mode 100644 src/fs/files/fileToRead.txt delete mode 100644 src/fs/files/fileToRemove.txt delete mode 100644 src/fs/files/hello.txt delete mode 100644 src/fs/files/wrongFilename.txt delete mode 100644 src/fs/list.js delete mode 100644 src/fs/read.js delete mode 100644 src/fs/rename.js delete mode 100644 src/hash/calcHash.js delete mode 100644 src/hash/files/fileToCalculateHashFor.txt delete mode 100644 src/modules/cjsToEsm.cjs delete mode 100644 src/modules/files/a.json delete mode 100644 src/modules/files/b.json delete mode 100644 src/modules/files/c.js delete mode 100644 src/streams/files/fileToRead.txt delete mode 100644 src/streams/files/fileToWrite.txt delete mode 100644 src/streams/read.js delete mode 100644 src/streams/transform.js delete mode 100644 src/streams/write.js rename {src copy => src}/utils/commandDispatch.js (100%) rename {src copy => src}/utils/compressFile.js (92%) rename {src copy => src}/utils/createNewFile.js (85%) rename {src copy => src}/utils/decompressFile.js (92%) rename {src copy => src}/utils/deleteFile.js (88%) rename {src copy => src}/utils/getUpperDir.js (100%) rename {src copy => src}/utils/getUserName.js (100%) rename {src copy => src}/utils/goToDir.js (84%) rename {src copy => src}/utils/moveFile.js (92%) rename {src copy => src}/utils/printBye.js (100%) rename {src copy => src}/utils/printFileContent.js (89%) rename {src copy => src}/utils/printHash.js (93%) rename {src copy => src}/utils/printListOfFiles.js (100%) rename {src copy => src}/utils/printOS.js (90%) rename {src copy => src}/utils/printWelcome.js (100%) rename {src copy => src}/utils/renameFile.js (88%) delete mode 100644 src/wt/main.js delete mode 100644 src/wt/worker.js delete mode 100644 src/zip/compress.js delete mode 100644 src/zip/decompress.js delete mode 100644 src/zip/files/fileToCompress.txt diff --git a/Readme copy.md b/Readme copy.md deleted file mode 100644 index 611a505f49..0000000000 --- a/Readme copy.md +++ /dev/null @@ -1,3 +0,0 @@ -# Node.js basics - -## !!! Please don't submit Pull Requests to this repository !!! diff --git a/package copy.json b/package copy.json deleted file mode 100644 index c1f83bfa24..0000000000 --- a/package copy.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "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": { - "start": "node ./bin/file-manager.js" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/AlreadyBored/node-nodejs-basics.git" - }, - "keywords": [ - "nodejs", - "assignments", - "alreadybored" - ], - "author": "alreadybored", - "license": "ISC", - "bugs": { - "url": "https://github.com/AlreadyBored/node-nodejs-basics/issues" - }, - "homepage": "https://github.com/AlreadyBored/node-nodejs-basics#readme" -} 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 copy/utils/commandDispatch.js b/src/utils/commandDispatch.js similarity index 100% rename from src copy/utils/commandDispatch.js rename to src/utils/commandDispatch.js diff --git a/src copy/utils/compressFile.js b/src/utils/compressFile.js similarity index 92% rename from src copy/utils/compressFile.js rename to src/utils/compressFile.js index f2b0b89368..468fd1ddc1 100644 --- a/src copy/utils/compressFile.js +++ b/src/utils/compressFile.js @@ -5,6 +5,11 @@ 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) diff --git a/src copy/utils/createNewFile.js b/src/utils/createNewFile.js similarity index 85% rename from src copy/utils/createNewFile.js rename to src/utils/createNewFile.js index 20eb2cf155..684531e51a 100644 --- a/src copy/utils/createNewFile.js +++ b/src/utils/createNewFile.js @@ -2,6 +2,11 @@ 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); diff --git a/src copy/utils/decompressFile.js b/src/utils/decompressFile.js similarity index 92% rename from src copy/utils/decompressFile.js rename to src/utils/decompressFile.js index 59e4faeefb..b471aeb5a7 100644 --- a/src copy/utils/decompressFile.js +++ b/src/utils/decompressFile.js @@ -5,6 +5,11 @@ 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) diff --git a/src copy/utils/deleteFile.js b/src/utils/deleteFile.js similarity index 88% rename from src copy/utils/deleteFile.js rename to src/utils/deleteFile.js index 20e31e03f8..36782ba80d 100644 --- a/src copy/utils/deleteFile.js +++ b/src/utils/deleteFile.js @@ -2,6 +2,11 @@ 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); diff --git a/src copy/utils/getUpperDir.js b/src/utils/getUpperDir.js similarity index 100% rename from src copy/utils/getUpperDir.js rename to src/utils/getUpperDir.js diff --git a/src copy/utils/getUserName.js b/src/utils/getUserName.js similarity index 100% rename from src copy/utils/getUserName.js rename to src/utils/getUserName.js diff --git a/src copy/utils/goToDir.js b/src/utils/goToDir.js similarity index 84% rename from src copy/utils/goToDir.js rename to src/utils/goToDir.js index 56b054d2e2..5ed8edd63a 100644 --- a/src copy/utils/goToDir.js +++ b/src/utils/goToDir.js @@ -2,7 +2,12 @@ import path from 'node:path'; import { stat, access } from 'fs/promises'; import { homedir } from 'node:os'; -export const goToDir = async (currentPath, destinationPath = '') => { +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); diff --git a/src copy/utils/moveFile.js b/src/utils/moveFile.js similarity index 92% rename from src copy/utils/moveFile.js rename to src/utils/moveFile.js index f462e39249..b09b1ba63a 100644 --- a/src copy/utils/moveFile.js +++ b/src/utils/moveFile.js @@ -4,6 +4,11 @@ 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) diff --git a/src copy/utils/printBye.js b/src/utils/printBye.js similarity index 100% rename from src copy/utils/printBye.js rename to src/utils/printBye.js diff --git a/src copy/utils/printFileContent.js b/src/utils/printFileContent.js similarity index 89% rename from src copy/utils/printFileContent.js rename to src/utils/printFileContent.js index 64278c9454..adb6370de3 100644 --- a/src copy/utils/printFileContent.js +++ b/src/utils/printFileContent.js @@ -3,6 +3,11 @@ 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) diff --git a/src copy/utils/printHash.js b/src/utils/printHash.js similarity index 93% rename from src copy/utils/printHash.js rename to src/utils/printHash.js index b18b3c7ed0..f78c4fbfcd 100644 --- a/src copy/utils/printHash.js +++ b/src/utils/printHash.js @@ -4,6 +4,11 @@ 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); diff --git a/src copy/utils/printListOfFiles.js b/src/utils/printListOfFiles.js similarity index 100% rename from src copy/utils/printListOfFiles.js rename to src/utils/printListOfFiles.js diff --git a/src copy/utils/printOS.js b/src/utils/printOS.js similarity index 90% rename from src copy/utils/printOS.js rename to src/utils/printOS.js index 530b817f91..474cacdf62 100644 --- a/src copy/utils/printOS.js +++ b/src/utils/printOS.js @@ -1,6 +1,11 @@ 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)}`) diff --git a/src copy/utils/printWelcome.js b/src/utils/printWelcome.js similarity index 100% rename from src copy/utils/printWelcome.js rename to src/utils/printWelcome.js diff --git a/src copy/utils/renameFile.js b/src/utils/renameFile.js similarity index 88% rename from src copy/utils/renameFile.js rename to src/utils/renameFile.js index 72774ce038..d80e6a7a34 100644 --- a/src copy/utils/renameFile.js +++ b/src/utils/renameFile.js @@ -2,6 +2,11 @@ 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) 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 From 14b31888de6b30b4c3b1168657ff47690ebc8ff7 Mon Sep 17 00:00:00 2001 From: Alex Smirnov Date: Tue, 15 Oct 2024 03:01:21 +0300 Subject: [PATCH 3/4] delete console.log --- src/utils/moveFile.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils/moveFile.js b/src/utils/moveFile.js index b09b1ba63a..d167a7b79c 100644 --- a/src/utils/moveFile.js +++ b/src/utils/moveFile.js @@ -35,7 +35,6 @@ export const moveFile = async (currentPath, oldpath, newpath) => { const sourceReadStream = createReadStream(absoldpath) const destinationWritableStream = createWriteStream(absnewpath) - console.log(absoldpath, absnewpath) try { await pipeline(sourceReadStream, destinationWritableStream) await rm(absoldpath); From e825f0b2ec1f3a387aa34033b86e718066e91ea1 Mon Sep 17 00:00:00 2001 From: Alex Smirnov Date: Tue, 15 Oct 2024 03:07:44 +0300 Subject: [PATCH 4/4] delete console.log --- src/utils/commandDispatch.js | 2 +- src/utils/printListOfFiles.js | 17 +---------------- 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/src/utils/commandDispatch.js b/src/utils/commandDispatch.js index 5ae55a3ac0..3cca7716cd 100644 --- a/src/utils/commandDispatch.js +++ b/src/utils/commandDispatch.js @@ -26,7 +26,7 @@ const commandList = { }, "cd": async (currentPath, [destination]) => { - currentPath.path = await goToDir(currentPath.path, destination); + currentPath.path = await goToDisr(currentPath.path, destination); }, "ls": async ({ path }) => { await printListOfFiles(path); diff --git a/src/utils/printListOfFiles.js b/src/utils/printListOfFiles.js index 4d490ead51..b900c59c8f 100644 --- a/src/utils/printListOfFiles.js +++ b/src/utils/printListOfFiles.js @@ -22,20 +22,5 @@ export const printListOfFiles = async (dirPath) => { const listOfDirectory = await Promise.all(formattedData) console.table(listOfDirectory.map(record => new Named(...record))); - // const filteredList = listOfDirectory.filter(record => record) - // const resultList = filteredList.map(async (currentPath) => { - // try { - // console.log(currentPath) - // const pathStat = await stat(currentPath); - // console.log(pa) - // if (pathStat.isDirectory()) return 'directory' - // if (pathStat.isFile()) return 'file' - // return pathStat.isDirectory() ? 'directory' : 'file'; - // } catch(_) { - // return null - // } - - // }); - // const result = await Promise.all(resultList) - // console.log(result) + } \ No newline at end of file