Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3bf27d1
feat: add package.json
mementovici Jun 10, 2022
a63f0c4
feat: add run script command
mementovici Jun 11, 2022
9cf1334
feat: add get name func
mementovici Jun 11, 2022
395d49e
feat: add copy func
mementovici Jun 11, 2022
4b18472
feat: add create-file func
mementovici Jun 11, 2022
41d52b6
feat: add delete file func
mementovici Jun 11, 2022
0f1e570
feat: add move-file func
mementovici Jun 11, 2022
dd2a3bf
feat: add read-file func
mementovici Jun 11, 2022
5fa08d2
feat: add rename-file func
mementovici Jun 11, 2022
37f55ff
feat: add calculate hash module
mementovici Jun 11, 2022
07d88fc
feat: add navigation module
mementovici Jun 11, 2022
472ce9c
feat: add os module
mementovici Jun 11, 2022
2da132e
feat: add compress func
mementovici Jun 11, 2022
7dbbd0d
feat: add decompress func
mementovici Jun 11, 2022
e1f8b08
feat: add main script file
mementovici Jun 11, 2022
59ba541
feat: (.exit) case
mementovici Jun 11, 2022
e86173d
feat: add "delete file" case
mementovici Jun 11, 2022
91dab87
docs: update readme
mementovici Jun 11, 2022
1fd0dcb
fix : fix typo in readme
mementovici Jun 11, 2022
60d1365
fix: add check absolute path for 'cd'
mementovici Jun 11, 2022
0439e97
Merge branch 'develop' of https://github.com/Shuvalovrus/node-nodejs-…
mementovici Jun 11, 2022
a37d81d
feat: add color for text
mementovici Jun 11, 2022
807877f
fix: change behavior with absolute path case for cp
mementovici Jun 12, 2022
f0038b8
fix: change behavior with absolute path for mv case
mementovici Jun 12, 2022
a7eaae4
fix: change behavior with absolute path for compress case
mementovici Jun 12, 2022
e689dae
fix: change behavior with absolute path for compress case
mementovici Jun 12, 2022
d0c9e14
fix: change behavior with absolute path for decompress case
mementovici Jun 12, 2022
218f20a
Update README.md
mementovici Jan 1, 2023
a06a5bf
Update README.md
mementovici Jan 1, 2023
d40ce42
Update getUserName.js
mementovici Jan 31, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 90 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,90 @@
# node-nodejs-FileManager
# 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 run
```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_

102 changes: 102 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
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";
import { deleteFile } from "./src/files/deleteFile.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 \x1b[33m${cwd()}\n\x1b[0m`);

(function sayHi () {
process.chdir(homeDirectory);
stdout.write(`Welcome to the File Manager, \x1b[35m${userName}!\n\x1b[0m`);
printCurrentDir();
})();


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 'rm' :
await deleteFile(pathFile);
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;
case '.exit' :
process.exit();
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());
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "nodejs-filemanager",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"start": "node index.js"
},
"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"
}
14 changes: 14 additions & 0 deletions src/cli/getUserName.js
Original file line number Diff line number Diff line change
@@ -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;
}
21 changes: 21 additions & 0 deletions src/files/copyFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createWriteStream, createReadStream } from "fs";
import { pipeline } from 'stream';
import { isAbsolute, join, parse } from "path";

export const copyFile = async (toReadPath,toWritePath) => {

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,
writeStream,
(err) => {
if (err) console.error('Operation failed');
}
)
}
15 changes: 15 additions & 0 deletions src/files/createFile.js
Original file line number Diff line number Diff line change
@@ -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');
}


}
13 changes: 13 additions & 0 deletions src/files/deleteFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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)

} catch (err) {
console.error('Operation failed');
}
}
12 changes: 12 additions & 0 deletions src/files/moveFile.js
Original file line number Diff line number Diff line change
@@ -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');
}
}
18 changes: 18 additions & 0 deletions src/files/readFile.js
Original file line number Diff line number Diff line change
@@ -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');
}
}
12 changes: 12 additions & 0 deletions src/files/renameFile.js
Original file line number Diff line number Diff line change
@@ -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');
}
}
28 changes: 28 additions & 0 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -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');
}
}
34 changes: 34 additions & 0 deletions src/nav/navigation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { join } from 'path';
import { readdir } from 'fs/promises';
import { isAbsolute } from 'path';

export const goUpDirectory = async () => {

try {
await process.chdir(join(process.cwd(), '../'));
} catch (err) {
console.error('Operation failed');
}

}

export const goToDirectory = async (path) => {
try {
if (!isAbsolute(path)) path = join(process.cwd(), path);
await process.chdir(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');
}

}
Loading