From 3bf27d1bcb1a48f1e623a6665ca584003f6fdbef Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Fri, 10 Jun 2022 10:28:38 +0300 Subject: [PATCH 01/29] feat: add package.json --- package.json | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 package.json diff --git a/package.json b/package.json new file mode 100644 index 0000000..c97c8a7 --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "nodejs-filemanager", + "version": "1.0.0", + "description": "", + "main": "index.js", + "type": "module", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Shuvalovrus/node-nodejs-FileManager.git" + }, + "author": "Shuvalov Konstantin", + "license": "ISC", + "bugs": { + "url": "https://github.com/Shuvalovrus/node-nodejs-FileManager/issues" + }, + "homepage": "https://github.com/Shuvalovrus/node-nodejs-FileManager#readme" +} From a63f0c41e914d06c186fda72daeea2945677a342 Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sat, 11 Jun 2022 15:46:23 +0300 Subject: [PATCH 02/29] feat: add run script command --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c97c8a7..f8b0444 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "type": "module", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "start": "node index.js" }, "repository": { "type": "git", From 9cf1334ce687ba2a3f314509e4e332523cadbde8 Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sat, 11 Jun 2022 15:47:00 +0300 Subject: [PATCH 03/29] feat: add get name func --- src/cli/getUserName.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/cli/getUserName.js diff --git a/src/cli/getUserName.js b/src/cli/getUserName.js new file mode 100644 index 0000000..320eba8 --- /dev/null +++ b/src/cli/getUserName.js @@ -0,0 +1,14 @@ +export const getUserName = () => { + + let result = 'Stranger'; + + process.argv.forEach((item) => { + + const name = item.split('=').pop(); + + if (item.startsWith('--username')) result = name === '' ? result : name; + + }) + + return result; +} \ No newline at end of file From 395d49e7e001c3757fb6e7520dcbd8ebdde9f1df Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sat, 11 Jun 2022 15:47:21 +0300 Subject: [PATCH 04/29] feat: add copy func --- src/files/copyFile.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/files/copyFile.js diff --git a/src/files/copyFile.js b/src/files/copyFile.js new file mode 100644 index 0000000..e020059 --- /dev/null +++ b/src/files/copyFile.js @@ -0,0 +1,19 @@ +import { createWriteStream, createReadStream } from "fs"; +import { pipeline } from 'stream'; +import { join, parse } from "path"; + +export const copyFile = async (toReadPath,toWritePath) => { + const readPath = join( process.cwd(), toReadPath ); + const writePath = join( toWritePath, parse(toReadPath).base ); + + const readStream = createReadStream(readPath); + const writeStream = createWriteStream(writePath); + + await pipeline ( + readStream, + writeStream, + (err) => { + if (err) console.error('Operation failed'); + } + ) +} \ No newline at end of file From 4b184721901f14a3d8b1499925c64abcd8d5b039 Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sat, 11 Jun 2022 15:47:41 +0300 Subject: [PATCH 05/29] feat: add create-file func --- src/files/createFile.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/files/createFile.js diff --git a/src/files/createFile.js b/src/files/createFile.js new file mode 100644 index 0000000..d809b86 --- /dev/null +++ b/src/files/createFile.js @@ -0,0 +1,15 @@ +import { createWriteStream } from 'fs'; +import { join } from "path"; + +export const createFile = async (file) => { + try { + const path = join( process.cwd(), file ); + + await createWriteStream(path).end(); + + } catch (err) { + console.error('Operation failed'); + } + + +} \ No newline at end of file From 41d52b6cfdcafda95aa3ef3c6e980a8649ad3d50 Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sat, 11 Jun 2022 15:48:01 +0300 Subject: [PATCH 06/29] feat: add delete file func --- src/files/deleteFile.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/files/deleteFile.js diff --git a/src/files/deleteFile.js b/src/files/deleteFile.js new file mode 100644 index 0000000..72cf796 --- /dev/null +++ b/src/files/deleteFile.js @@ -0,0 +1,11 @@ +import { unlink } from "fs/promises"; + +export const deleteFile = async (path) => { + + try { + await unlink(path) + + } catch (err) { + console.error('Operation failed'); + } +} \ No newline at end of file From 0f1e570148d9b65243bc4a3fb474b47aabe79891 Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sat, 11 Jun 2022 15:48:20 +0300 Subject: [PATCH 07/29] feat: add move-file func --- src/files/moveFile.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/files/moveFile.js diff --git a/src/files/moveFile.js b/src/files/moveFile.js new file mode 100644 index 0000000..f105147 --- /dev/null +++ b/src/files/moveFile.js @@ -0,0 +1,12 @@ +import { copyFile } from "./copyFile.js"; +import { deleteFile } from "./deleteFile.js"; + +export const moveFile = async (toCopyPath, toDestinationPath) => { + try { + await copyFile(toCopyPath, toDestinationPath); + await deleteFile(toCopyPath); + + } catch (err) { + console.error('Operation failed'); + } +} \ No newline at end of file From dd2a3bf1c607e7459fcfe0ad7a78fd945140f55e Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sat, 11 Jun 2022 15:48:34 +0300 Subject: [PATCH 08/29] feat: add read-file func --- src/files/readFile.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/files/readFile.js diff --git a/src/files/readFile.js b/src/files/readFile.js new file mode 100644 index 0000000..c0b1ea2 --- /dev/null +++ b/src/files/readFile.js @@ -0,0 +1,18 @@ +import { createReadStream } from 'fs'; +import { finished } from 'stream/promises' +import { join } from "path"; + +export const readFile = async (file) => { + try { + const streamPath = join( process.cwd(), file ); + + const readStream = createReadStream(streamPath); + + readStream.on('data', (chunk) => process.stdout.write(chunk + '\n')); + + await finished(readStream); + + } catch (err) { + console.error('Operation failed'); + } +} \ No newline at end of file From 5fa08d26cd87e4300b82f40aaa371ddc5be0a167 Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sat, 11 Jun 2022 15:48:53 +0300 Subject: [PATCH 09/29] feat: add rename-file func --- src/files/renameFile.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/files/renameFile.js diff --git a/src/files/renameFile.js b/src/files/renameFile.js new file mode 100644 index 0000000..3698478 --- /dev/null +++ b/src/files/renameFile.js @@ -0,0 +1,12 @@ +import { rename } from "fs/promises" + +export const renameFile = async (path, fileName) => { + + try { + + await rename(path, fileName); + + } catch (err) { + console.error('Operation failed'); + } +} \ No newline at end of file From 37f55ff62e5155772baedd6f349d614d0e3e2184 Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sat, 11 Jun 2022 15:49:17 +0300 Subject: [PATCH 10/29] feat: add calculate hash module --- src/hash/calcHash.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/hash/calcHash.js diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js new file mode 100644 index 0000000..5bc7e2f --- /dev/null +++ b/src/hash/calcHash.js @@ -0,0 +1,28 @@ +import { createReadStream } from "fs"; +import { createHash } from 'crypto' +import { join } from "path"; +import { finished } from "stream/promises"; + +export const calcHash = async (path) => { + + try { + const readPath = join( process.cwd(), path ); + const readStream = createReadStream(readPath); + + readStream.on('data', (chunk) => { + + let hash = createHash('sha256'); + let updateHash = hash.update(chunk); + let hexHash = updateHash.digest('hex'); + + process.stdout.write(hexHash + '\n'); + + }) + + await finished(readStream); + + } catch (err) { + + console.error('Operation failed'); + } +} From 07d88fcd957573a977a1fee88361e82f5ec8b9a4 Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sat, 11 Jun 2022 15:49:41 +0300 Subject: [PATCH 11/29] feat: add navigation module --- src/nav/navigation.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/nav/navigation.js diff --git a/src/nav/navigation.js b/src/nav/navigation.js new file mode 100644 index 0000000..1b25354 --- /dev/null +++ b/src/nav/navigation.js @@ -0,0 +1,32 @@ +import { join } from 'path'; +import { readdir } from 'fs/promises'; + +export const goUpDirectory = async () => { + + try { + await process.chdir(join(process.cwd(), '../')); + } catch (err) { + console.error('Operation failed'); + } + +} + +export const goToDirectory = async (path) => { + try { + await process.chdir(join(process.cwd(), path)) + } catch (err) { + console.error('Operation failed'); + } + +} + +export const getListDirectory = async () => { + + try { + const result = await readdir(process.cwd()); + console.log(result); + } catch (err) { + console.error('Operation failed'); + } + +} \ No newline at end of file From 472ce9cd6720ef4283dc2e4cbe84432e25810546 Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sat, 11 Jun 2022 15:49:55 +0300 Subject: [PATCH 12/29] feat: add os module --- src/os/os.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/os/os.js diff --git a/src/os/os.js b/src/os/os.js new file mode 100644 index 0000000..ef785f2 --- /dev/null +++ b/src/os/os.js @@ -0,0 +1,35 @@ +import * as os from "os"; + + +export const osCommandHandler = async (arg) => { + + switch (arg) { + case '--EOL' : + process.stdout.write(JSON.stringify(os.EOL) + '\n'); + break; + + case '--cpus' : + process.stdout.write(`Total: ${ os.cpus().length + '\n' }`); + + os.cpus().forEach((item) => process.stdout.write(`Model: ${ item.model } Speed: ${ item.speed / 1000 } GHz\n`)); + break; + + case '--homedir' : + process.stdout.write(os.homedir() + '\n'); + break; + + case '--username' : + process.stdout.write(os.userInfo().username + '\n'); + break; + + case '--architecture' : + process.stdout.write(os.arch() + '\n'); + break; + + default: + console.error('Operation Failed'); + } +} + + + From 2da132edd7018bd46339ad038bbc1b785b19e5c0 Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sat, 11 Jun 2022 15:50:10 +0300 Subject: [PATCH 13/29] feat: add compress func --- src/zip/compress.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/zip/compress.js diff --git a/src/zip/compress.js b/src/zip/compress.js new file mode 100644 index 0000000..28b45a3 --- /dev/null +++ b/src/zip/compress.js @@ -0,0 +1,28 @@ +import { createReadStream, createWriteStream } from "fs"; +import { createBrotliCompress } from 'zlib' +import { join, parse, dirname } from "path"; +import { pipeline } from "stream"; + +export const compressFile = async (toReadPath, toWritePath) => { + + const readPath = join( process.cwd(), toReadPath ); + + const writePath = toWritePath === toReadPath ? + + join( process.cwd(), dirname(toWritePath) , parse(toReadPath).base + '.gz') : + join( process.cwd(), toWritePath , parse(toReadPath).base + '.gz'); + + + const readStream = createReadStream( readPath ); + const writeStream = createWriteStream( writePath ); + + + await pipeline( + readStream, + createBrotliCompress(), + writeStream, + (err) => { + if (err) console.error('Operation Failed'); + } + ) +} From 7dbbd0d6190f18eb90784938d930c02343e5d200 Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sat, 11 Jun 2022 15:50:20 +0300 Subject: [PATCH 14/29] feat: add decompress func --- src/zip/decompress.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/zip/decompress.js diff --git a/src/zip/decompress.js b/src/zip/decompress.js new file mode 100644 index 0000000..bb54118 --- /dev/null +++ b/src/zip/decompress.js @@ -0,0 +1,33 @@ +import { createReadStream, createWriteStream } from "fs"; +import { createBrotliDecompress } from 'zlib' +import {join, dirname, parse} from "path"; +import { pipeline } from "stream"; + +export const decompressFile = async (toReadPath, toWritePath) => { + + const readPath = join( process.cwd(), toReadPath ); + + const unzipFileNameArr = parse(toReadPath).base.split('.') + unzipFileNameArr.length = unzipFileNameArr.length -1; + + const unzipFileName = unzipFileNameArr.join('.'); + + const writePath = toWritePath === toReadPath ? + + join( process.cwd(), dirname(toWritePath), unzipFileName) : + join( process.cwd(), toWritePath, unzipFileName); + + + const readStream = createReadStream( readPath ); + const writeStream = createWriteStream( writePath ); + + + await pipeline( + readStream, + createBrotliDecompress(), + writeStream, + (err) => { + if (err) console.error('Operation Failed'); + } + ) +} From e1f8b08c3e56e5f9929d71b447f0de8bc8c5abe1 Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sat, 11 Jun 2022 15:50:51 +0300 Subject: [PATCH 15/29] feat: add main script file --- index.js | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..f47503c --- /dev/null +++ b/index.js @@ -0,0 +1,94 @@ +import * as readline from 'node:readline'; +import { getUserName } from './src/cli/getUserName.js'; +import { goUpDirectory,goToDirectory, getListDirectory } from "./src/nav/navigation.js"; +import { readFile } from "./src/files/readFile.js"; +import { createFile } from "./src/files/createFile.js"; +import { renameFile } from "./src/files/renameFile.js"; +import { copyFile } from "./src/files/copyFile.js"; +import { moveFile } from "./src/files/moveFile.js"; +import { osCommandHandler } from "./src/os/os.js"; +import { calcHash } from "./src/hash/calcHash.js"; +import { compressFile } from "./src/zip/compress.js"; +import { decompressFile } from "./src/zip/decompress.js"; + +const { stdin, stdout, cwd } = process; +const homeDirectory = process.env["HOME"]; +const userName = getUserName(); +const readLineInterface = readline.createInterface({ input: stdin, output: stdout }); + +const printCurrentDir = () => process.stdout.write(`You are currently in ${cwd()}\n`); + +(function sayHi () { + process.chdir(homeDirectory); + stdout.write(`Welcome to the File Manager, ${userName}!\n`); + stdout.write(`You are currently in ${cwd()}\n`); +})(); + + +readLineInterface.on('line', async (line) => { + + const path = line.split(' ').slice(1); + const pathFile = path[0] || false; + const fileName = path[1] || false; + const pathDestination = path[1] || path[0]; + + const command = line.split(' ')[0]; + + switch (command) { + case 'up' : + await goUpDirectory(); + printCurrentDir() + break; + case 'cd' : + await goToDirectory(pathFile); + printCurrentDir() + break; + case 'ls' : + await getListDirectory(); + printCurrentDir() + break; + case 'cat' : + await readFile(pathFile); + printCurrentDir() + break; + case 'add' : + await createFile(pathFile); + printCurrentDir() + break; + case 'rn' : + await renameFile(pathFile, fileName); + printCurrentDir() + break; + case 'cp' : + await copyFile(pathFile, pathDestination); + printCurrentDir() + break; + case 'mv' : + await moveFile(pathFile, pathDestination); + printCurrentDir() + break; + case 'os' : + await osCommandHandler(pathFile); + printCurrentDir() + break; + case 'hash' : + await calcHash(pathFile); + printCurrentDir() + break; + case 'compress' : + await compressFile(pathFile, pathDestination); + printCurrentDir() + break; + case 'decompress' : + await decompressFile(pathFile, pathDestination); + printCurrentDir() + break; + default: + process.stdout.write('Invalid input\n'); + printCurrentDir() + } + +}) + +process.on('exit', () => process.stdout.write(`Thank you for using File Manager, ${userName}!`)); +process.on('SIGINT', () => process.exit()); From 59ba5413e18ba9ecba0a771d3110a6143c64f550 Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sat, 11 Jun 2022 15:56:40 +0300 Subject: [PATCH 16/29] feat: (.exit) case --- index.js | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index f47503c..0019f45 100644 --- a/index.js +++ b/index.js @@ -37,55 +37,58 @@ readLineInterface.on('line', async (line) => { switch (command) { case 'up' : await goUpDirectory(); - printCurrentDir() + printCurrentDir(); break; case 'cd' : await goToDirectory(pathFile); - printCurrentDir() + printCurrentDir(); break; case 'ls' : await getListDirectory(); - printCurrentDir() + printCurrentDir(); break; case 'cat' : await readFile(pathFile); - printCurrentDir() + printCurrentDir(); break; case 'add' : await createFile(pathFile); - printCurrentDir() + printCurrentDir(); break; case 'rn' : await renameFile(pathFile, fileName); - printCurrentDir() + printCurrentDir(); break; case 'cp' : await copyFile(pathFile, pathDestination); - printCurrentDir() + printCurrentDir(); break; case 'mv' : await moveFile(pathFile, pathDestination); - printCurrentDir() + printCurrentDir(); break; case 'os' : await osCommandHandler(pathFile); - printCurrentDir() + printCurrentDir(); break; case 'hash' : await calcHash(pathFile); - printCurrentDir() + printCurrentDir(); break; case 'compress' : await compressFile(pathFile, pathDestination); - printCurrentDir() + printCurrentDir(); break; case 'decompress' : await decompressFile(pathFile, pathDestination); - printCurrentDir() + printCurrentDir(); + break; + case '.exit' : + process.exit(); break; default: process.stdout.write('Invalid input\n'); - printCurrentDir() + printCurrentDir(); } }) From e86173dea3b51dcbf2611e0875a87d56ac41f41e Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sat, 11 Jun 2022 16:08:07 +0300 Subject: [PATCH 17/29] feat: add "delete file" case --- index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/index.js b/index.js index 0019f45..18e7bd0 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,7 @@ import { osCommandHandler } from "./src/os/os.js"; import { calcHash } from "./src/hash/calcHash.js"; import { compressFile } from "./src/zip/compress.js"; import { decompressFile } from "./src/zip/decompress.js"; +import { deleteFile } from "./src/files/deleteFile.js"; const { stdin, stdout, cwd } = process; const homeDirectory = process.env["HOME"]; @@ -67,6 +68,10 @@ readLineInterface.on('line', async (line) => { await moveFile(pathFile, pathDestination); printCurrentDir(); break; + case 'rm' : + await deleteFile(pathFile); + printCurrentDir(); + break; case 'os' : await osCommandHandler(pathFile); printCurrentDir(); From 91dab87de5b82a13d331576532eff5c880d0d1dd Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sat, 11 Jun 2022 16:24:35 +0300 Subject: [PATCH 18/29] docs: update readme --- README.md | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8d3f288..3963518 100644 --- a/README.md +++ b/README.md @@ -1 +1,90 @@ -# node-nodejs-FileManager \ No newline at end of file +# node-nodejs: File Manager + +### Description +The file manager can do the following: +- Operate using the CLI +- Perform basic file operations (copy, move, delete, rename, etc.) +- Use Streams API +- Get information about the operating system of the host machine +- Perform hash calculations +- Compress and decompress files +###How to rum +```bash +npm run start -- --username=your_username +``` +###List of operations and their syntax: +- Navigation & working directory (nwd) + - Go upper from current directory + ```bash + up + ``` + - Go to dedicated folder from current directory (`path_to_directory` can be relative or absolute) + ```bash + cd path_to_directory + ``` + - List all files and folder in current directory and print it to console + ```bash + ls + ``` +- Basic operations with files + - Read file and print it's content in console: + ```bash + cat path_to_file + ``` + - Create empty file in current working directory: + ```bash + add new_file_name + ``` + - Rename file: + ```bash + rn path_to_file new_filename + ``` + - Copy file: + ```bash + cp path_to_file path_to_new_directory + ``` + - Move file (same as copy but initial file is deleted): + ```bash + mv path_to_file path_to_new_directory + ``` + - Delete file: + ```bash + rm path_to_file + ``` +- Operating system info (prints following information in console) + - Get EOL (default system End-Of-Line) + ```bash + os --EOL + ``` + - Get host machine CPUs info (overall amount of CPUS plus model and clock rate (in GHz) for each of them) + ```bash + os --cpus + ``` + - Get home directory: + ```bash + os --homedir + ``` + - Get current *system user name* + ```bash + os --username + ``` + - Get CPU architecture for which Node.js binary has compiled + ```bash + os --architecture + ``` +- Hash calculation + - Calculate hash for file and print it into console + ```bash + hash path_to_file + ``` +- Compress and decompress operations + - Compress file (using Brotli algorithm) + ```bash + compress path_to_file path_to_destination + ``` + - Decompress file (using Brotli algorithm) + ```bash + decompress path_to_file path_to_destination + ``` + _If the second `path_to_destination` parameter is not specified, the operation will be performed in the current directory_ + \ No newline at end of file From 1fd0dcb2f138e42b89a7a9c382df87dac5de8371 Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sat, 11 Jun 2022 16:27:15 +0300 Subject: [PATCH 19/29] fix : fix typo in readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3963518..42ab5c1 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ The file manager can do the following: ```bash npm run start -- --username=your_username ``` -###List of operations and their syntax: +### List of operations and their syntax: - Navigation & working directory (nwd) - Go upper from current directory ```bash @@ -87,4 +87,4 @@ npm run start -- --username=your_username decompress path_to_file path_to_destination ``` _If the second `path_to_destination` parameter is not specified, the operation will be performed in the current directory_ - \ No newline at end of file + From 60d13655592545ddf8eb85c31aa76a1ed2456d10 Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sat, 11 Jun 2022 16:37:08 +0300 Subject: [PATCH 20/29] fix: add check absolute path for 'cd' --- src/nav/navigation.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/nav/navigation.js b/src/nav/navigation.js index 1b25354..8367e08 100644 --- a/src/nav/navigation.js +++ b/src/nav/navigation.js @@ -1,5 +1,6 @@ import { join } from 'path'; import { readdir } from 'fs/promises'; +import { isAbsolute } from 'path'; export const goUpDirectory = async () => { @@ -13,7 +14,8 @@ export const goUpDirectory = async () => { export const goToDirectory = async (path) => { try { - await process.chdir(join(process.cwd(), path)) + if (!isAbsolute(path)) path = join(process.cwd(), path); + await process.chdir(path) } catch (err) { console.error('Operation failed'); } From a37d81df36a6a7e63cfae7f543064fc24e2b322f Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sat, 11 Jun 2022 16:52:11 +0300 Subject: [PATCH 21/29] feat: add color for text --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 18e7bd0..63862c8 100644 --- a/index.js +++ b/index.js @@ -17,12 +17,12 @@ const homeDirectory = process.env["HOME"]; const userName = getUserName(); const readLineInterface = readline.createInterface({ input: stdin, output: stdout }); -const printCurrentDir = () => process.stdout.write(`You are currently in ${cwd()}\n`); +const printCurrentDir = () => process.stdout.write(`You are currently in \x1b[33m${cwd()}\n\x1b[0m`); (function sayHi () { process.chdir(homeDirectory); - stdout.write(`Welcome to the File Manager, ${userName}!\n`); - stdout.write(`You are currently in ${cwd()}\n`); + stdout.write(`Welcome to the File Manager, \x1b[35m${userName}!\n\x1b[0m`); + printCurrentDir(); })(); From 807877f13ea89fc02f9346c5c283636d6a7bba57 Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sun, 12 Jun 2022 11:05:36 +0300 Subject: [PATCH 22/29] fix: change behavior with absolute path case for cp --- src/files/copyFile.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/files/copyFile.js b/src/files/copyFile.js index e020059..8bc16fc 100644 --- a/src/files/copyFile.js +++ b/src/files/copyFile.js @@ -1,13 +1,15 @@ import { createWriteStream, createReadStream } from "fs"; import { pipeline } from 'stream'; -import { join, parse } from "path"; +import { isAbsolute, join, parse } from "path"; export const copyFile = async (toReadPath,toWritePath) => { - const readPath = join( process.cwd(), toReadPath ); - const writePath = join( toWritePath, parse(toReadPath).base ); - const readStream = createReadStream(readPath); - const writeStream = createWriteStream(writePath); + if (!isAbsolute(toReadPath)) toReadPath = join(process.cwd(), toReadPath); + toWritePath = join( toWritePath, parse(toReadPath).base) ; + + + const readStream = createReadStream(toReadPath); + const writeStream = createWriteStream(toWritePath); await pipeline ( readStream, From f0038b8101964803c98c163c34357c4ffda81ded Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sun, 12 Jun 2022 11:20:18 +0300 Subject: [PATCH 23/29] fix: change behavior with absolute path for mv case --- src/files/deleteFile.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/files/deleteFile.js b/src/files/deleteFile.js index 72cf796..e4eb738 100644 --- a/src/files/deleteFile.js +++ b/src/files/deleteFile.js @@ -1,6 +1,8 @@ import { unlink } from "fs/promises"; +import { isAbsolute, join } from "path"; export const deleteFile = async (path) => { + if (!isAbsolute(path)) path = join(process.cwd(), path); try { await unlink(path) From a7eaae4da4682922d4752e1172d74b5930526bea Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sun, 12 Jun 2022 11:31:14 +0300 Subject: [PATCH 24/29] fix: change behavior with absolute path for compress case --- src/zip/compress.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/zip/compress.js b/src/zip/compress.js index 28b45a3..ee1038e 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,11 +1,11 @@ import { createReadStream, createWriteStream } from "fs"; import { createBrotliCompress } from 'zlib' -import { join, parse, dirname } from "path"; +import {join, parse, dirname, isAbsolute} from "path"; import { pipeline } from "stream"; export const compressFile = async (toReadPath, toWritePath) => { - const readPath = join( process.cwd(), toReadPath ); + const readPath = !isAbsolute( toReadPath ) ? join(process.cwd(), toReadPath) : toReadPath const writePath = toWritePath === toReadPath ? @@ -13,6 +13,10 @@ export const compressFile = async (toReadPath, toWritePath) => { join( process.cwd(), toWritePath , parse(toReadPath).base + '.gz'); + + console.log(readPath) + console.log(writePath) + const readStream = createReadStream( readPath ); const writeStream = createWriteStream( writePath ); From e689dae1044ae5ed3b1941dc698d16a64124ac60 Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sun, 12 Jun 2022 11:47:08 +0300 Subject: [PATCH 25/29] fix: change behavior with absolute path for compress case --- src/zip/compress.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/zip/compress.js b/src/zip/compress.js index ee1038e..a317741 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,21 +1,21 @@ import { createReadStream, createWriteStream } from "fs"; import { createBrotliCompress } from 'zlib' -import {join, parse, dirname, isAbsolute} from "path"; +import { join, parse, dirname, isAbsolute } from "path"; import { pipeline } from "stream"; export const compressFile = async (toReadPath, toWritePath) => { + const readPath = !isAbsolute( toReadPath ) ? join(process.cwd(), toReadPath) : toReadPath - const writePath = toWritePath === toReadPath ? + let writePath = !isAbsolute( toWritePath ) ? join(process.cwd(), toWritePath) : toWritePath - join( process.cwd(), dirname(toWritePath) , parse(toReadPath).base + '.gz') : - join( process.cwd(), toWritePath , parse(toReadPath).base + '.gz'); + writePath = readPath === writePath ? + join( dirname(toWritePath) , parse(toReadPath).base + '.gz') : + join( toWritePath , parse(toReadPath).base + '.gz'); - console.log(readPath) - console.log(writePath) const readStream = createReadStream( readPath ); const writeStream = createWriteStream( writePath ); From d0c9e149b26a954df2cdfdcd38fe987de8bc6f01 Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sun, 12 Jun 2022 11:55:45 +0300 Subject: [PATCH 26/29] fix: change behavior with absolute path for decompress case --- src/zip/decompress.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/zip/decompress.js b/src/zip/decompress.js index bb54118..2195544 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,23 +1,27 @@ import { createReadStream, createWriteStream } from "fs"; import { createBrotliDecompress } from 'zlib' -import {join, dirname, parse} from "path"; +import {join, dirname, parse, isAbsolute} from "path"; import { pipeline } from "stream"; export const decompressFile = async (toReadPath, toWritePath) => { - const readPath = join( process.cwd(), toReadPath ); + const readPath = !isAbsolute( toReadPath ) ? join(process.cwd(), toReadPath) : toReadPath + + let writePath = !isAbsolute( toWritePath ) ? join(process.cwd(), toWritePath) : toWritePath + const unzipFileNameArr = parse(toReadPath).base.split('.') unzipFileNameArr.length = unzipFileNameArr.length -1; const unzipFileName = unzipFileNameArr.join('.'); - const writePath = toWritePath === toReadPath ? - join( process.cwd(), dirname(toWritePath), unzipFileName) : - join( process.cwd(), toWritePath, unzipFileName); + writePath = writePath === readPath ? + join( dirname(toWritePath), unzipFileName) : + join( toWritePath, unzipFileName); + const readStream = createReadStream( readPath ); const writeStream = createWriteStream( writePath ); From 218f20a801e912f89b3b90f3b3b4bcb8336e9160 Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sun, 1 Jan 2023 06:59:05 +0300 Subject: [PATCH 27/29] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 42ab5c1..462e9d4 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ The file manager can do the following: - Get information about the operating system of the host machine - Perform hash calculations - Compress and decompress files -###How to rum +### How to rum ```bash npm run start -- --username=your_username ``` From a06a5bfc8a112b35563219156a785265f95fa197 Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Sun, 1 Jan 2023 06:59:20 +0300 Subject: [PATCH 28/29] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 462e9d4..cc249b3 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ The file manager can do the following: - Get information about the operating system of the host machine - Perform hash calculations - Compress and decompress files -### How to rum +### How to run ```bash npm run start -- --username=your_username ``` From d40ce42d1a0332cdec812cbf4c589920ccfa44ae Mon Sep 17 00:00:00 2001 From: Konstantin Shuvalov Date: Wed, 31 Jan 2024 08:43:03 +0300 Subject: [PATCH 29/29] Update getUserName.js --- src/cli/getUserName.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cli/getUserName.js b/src/cli/getUserName.js index 320eba8..e9e67e2 100644 --- a/src/cli/getUserName.js +++ b/src/cli/getUserName.js @@ -6,9 +6,9 @@ export const getUserName = () => { const name = item.split('=').pop(); - if (item.startsWith('--username')) result = name === '' ? result : name; + if (item.startsWith('--username')) result = name ? result : name; }) return result; -} \ No newline at end of file +}