Skip to content
14 changes: 14 additions & 0 deletions add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { open } from 'fs';


export function add(newFileName){

try { //можно ли здесь избавиться от колбэка?
open(newFileName, 'w', () => {
console.log('File '+ newFileName + ' was created.'+ '\n' + 'You are currently in ' + process.cwd());
});
} catch(err) {
console.log('Operation failed.'+ '\n' + 'You are currently in ' + process.cwd());
}

}
93 changes: 93 additions & 0 deletions arch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { createBrotliCompress, createBrotliDecompress } from 'zlib';
import { createReadStream, createWriteStream } from 'fs';
import { pipeline } from 'stream';




export async function compress(args){

const handledArgs = await handleDoubleArgs(args);

if(handledArgs === 'err') {
console.log('Operation failed. ' + '\n' + 'You are currently in ' + process.cwd());
return;
}

const { sourceFile, destinationFile } = handledArgs;

const readStream = createReadStream(sourceFile);
readStream.on('error', err => {
console.log('Operation failed. ' + '\n' + 'You are currently in ' + process.cwd());
return;
});

const writeStream = createWriteStream(destinationFile);
writeStream.on('error', err => {
console.log('Operation failed. ' + '\n' + 'You are currently in ' + process.cwd())
});

const compressFile = createBrotliCompress();
pipeline(readStream, compressFile, writeStream, ()=>{});
console.log('File compressed.'+'\n'+'You are currently in ' + process.cwd());

}


export async function decompress(args){

const handledArgs = await handleDoubleArgs(args);

if(handledArgs === 'err') {
console.log('Operation failed. ' + '\n' + 'You are currently in ' + process.cwd());
}

const { sourceFile, destinationFile } = handledArgs;

console.log(sourceFile);

try{
const readStream = createReadStream(sourceFile);
readStream.on('error', err => console.log('Operation failed. ' + '\n' + 'You are currently in ' + process.cwd()));

const writeStream = createWriteStream(destinationFile);
writeStream.on('error', err => console.log('Operation failed. ' + '\n' + 'You are currently in ' + process.cwd()));

const compressFile = createBrotliDecompress();

pipeline(readStream, compressFile, writeStream, ()=>{});

writeStream.on('finish', () => console.log('File decompressed.'+'\n'+'You are currently in ' + process.cwd()));

} catch(err) {
console.log('Operation failed. ' + '\n' + 'You are currently in ' + process.cwd())
}
}



export async function handleDoubleArgs(args){

if(!args.includes('" "')){
return ('err');
}

const splittedArgs = args.split(' "');

if (splittedArgs.length !== 2){
return ('err');
}

const argsWithoutQuotes = splittedArgs.map(item => {
if (item.startsWith('"')) item = item.slice(1);
if (item.endsWith('"')) item = item.slice(0, item.length - 1);
return item;
});

let resultObj = {};

resultObj.sourceFile = argsWithoutQuotes[0];
resultObj.destinationFile = argsWithoutQuotes[1];

return resultObj;
}
17 changes: 17 additions & 0 deletions cat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Read file and print it's content in console

import { createReadStream } from 'fs';
import { join } from 'path';

export function cat(pathToFile){

const readStream = createReadStream(pathToFile);
console.log('Reading '+pathToFile);
readStream.on('data', chunk => console.log(chunk.toString()));
readStream.on('error', () => { console.log('Operation failed');
console.log('You are currently in ' + process.cwd())
});
readStream.on('end', () => console.log('You are currently in ' + process.cwd()));


}
38 changes: 38 additions & 0 deletions cp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { createReadStream, createWriteStream} from 'fs';
import { readdir } from 'fs/promises';
import { handleDoubleArgs } from './arch.js'

export async function copy(args){

const handledArgs = await handleDoubleArgs(args);

if(handledArgs === 'err') {
console.log('Operation failed. ' + '\n' + 'You are currently in ' + process.cwd());
}

const { sourceFile, destinationFile } = handledArgs;

try{
const readStream = createReadStream(sourceFile);

readStream.on('error', err => {
console.log('Operation failed. ' + '\n' + 'You are currently in ' + process.cwd());
return;
});

readStream.on('open', () => {
const writeStream = createWriteStream(destinationFile);
readStream.pipe(writeStream);
});

readStream.on('end', () => {
console.log('File was successfully copied');
console.log('You are currently in ' + process.cwd());
});

} catch(err) {
console.log('Operation failed.'+ '\n' + 'You are currently in ' + process.cwd());
return;
}

}
13 changes: 13 additions & 0 deletions del.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { rm } from 'fs/promises';

export async function del(pathToFile){

try{
await rm(pathToFile);
console.log('File was deleted.' + '\n' + 'You are currently in ' + process.cwd());
} catch(err) {
console.log(err+'Operation failed ' + '\n' + 'You are currently in ' + process.cwd());
return;
}

}
18 changes: 18 additions & 0 deletions hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createHash } from 'crypto';
import { join, resolve } from 'path';
import { createReadStream } from 'fs';

export function hash(arg){

const pathToFile = resolve(arg); // нужно ли делать так, чтобы если слеши в переданном арг не те, resolve заменил бы? или лишнее?

const readStream = createReadStream(pathToFile);
readStream.on('error', err => console.log('Operation failed. ' + '\n' + 'You are currently in ' + process.cwd()));
readStream.on('data', chunk => {
const hashObj = createHash('sha256');
hashObj.update(chunk);
const hex = hashObj.digest('hex');
console.log(hex);
});
readStream.on('end', () => console.log('Hash delivered.'+'\n'+'You are currently in ' + process.cwd()));
}
42 changes: 42 additions & 0 deletions help.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export function help(){
console.log(`

***************************************************************************************************

!!! Use "" double quotes in arguments with _two paths_ to prevent spaces-related errors!

command example

add - create file add trturh.txt
rn - rename file rn "C:\\Users\\User\\test test\\ttt.txt" qqq1.js
cp - copy file cp "C:\\Users\\User\\test test\\qqq1.js" "C:\\Users\\User\\test\\qqq2.js"
mv - copy and delete mv "C:\\Users\\User\\test test\\qqq1.js" "C:\\Users\\User\\test\\qqq2.js"
rm - delete rm C:\\Users\\User\\test test\\qqq1.js
cat - read and print cat C:\\Users\\User\\test\\qqq2.js

compress - Brotli compress "C:\\Users\\User\\test\\qqq2.js" "C:\\Users\\User\\test\\qqq2"
decompress - Brotli decompress "C:\\Users\\User\\test\\qqq2" "C:\\Users\\User\\test\\qqq2.js"

hash - Calculate hash hash C:\\Users\\User\\test\\qqq2.js

cd - go from current directory cd MyFoldername
ls - list files ls
up - go up from directory up


os commands:

os --EOL - Get EOL (default system End-Of-Line)
os --cpus - Get host machine CPUs info (overall amount of CPUS plus model and clock rate (in GHz) for each of them)
os --homedir - Get home directory
os --username - Get current system user name
os --architecture - Get CPU architecture for which Node.js binary has compiled

Good luck!

(=^・^=)

***************************************************************************************************

`);
}
14 changes: 14 additions & 0 deletions ls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { readdir } from 'fs/promises';

export async function ls(){
const pathToFolder = process.cwd();
try {
const items = await readdir(pathToFolder);
items.forEach(item => console.log(item));

console.log('You are currently in ' + process.cwd());

} catch (err) {
console.log(err+'Operation failed. ' + '\n' + 'You are currently in ' + process.cwd());
}
}
78 changes: 78 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// npm run start -- --username=MrUser

import { createInterface } from 'readline';
import { parse } from 'path';
const { stdout, stdin, argv, cwd } = process;

import { help } from './help.js';

import { setNewWorkingDirectory, cd, up } from './navigate.js';
import { ls } from './ls.js';

import { cat } from './cat.js';
import { add } from './add.js';
import { rn } from './rn.js';
import { copy } from './cp.js';
import { mv } from './mv.js';
import { del } from './del.js';

import { os } from './os.js';

import { hash } from './hash.js';
import { compress, decompress } from './arch.js';



const pathToWorkingDirectory = setNewWorkingDirectory();

const args = argv.slice(2); // если не передаст???


let username;

if(!args[0]){
username = 'User';
} else {
username = args[0].split('=')[1];
}

stdout.write(`Welcome to the File Manager, ${username}!
You are currently in ${process.cwd()}.
To get instructions use 'help'
`);

const rl = createInterface(stdin, stdout);

rl.on('line', (line) => { route(line) });
process.on('SIGINT', () => { process.exit(); } );
process.on('exit', () => stdout.write(`Thank you for using File Manager, ${username}!`) );




function route(command){

if(command === 'exit') process.exit();

else if(command === 'help') help();

else if(command === 'up') up();
else if(command === 'ls') ls();
else if(command.startsWith('cd ')) cd(command.slice(3), pathToWorkingDirectory);

else if(command.startsWith('cat ')) cat(command.slice(4)); // Read file and print it's content in console
else if(command.startsWith('add ')) add(command.slice(4)); // Create empty file in current working directory
else if(command.startsWith('rn ')) rn(command.slice(3)); // Rename file: rn path_to_file new_filename (must be in working directory)
else if(command.startsWith('cp ')) copy(command.slice(3)); // Copy file: cp file.txt directory\file.txt (must be in working directory)
else if(command.startsWith('mv ')) mv(command.slice(3)); // Move file mv path_to_file path_to_new_directory (must be in working directory)
else if(command.startsWith('rm ')) del(command.slice(3)); // Delete file: rm path_to_file (must be in working directory)

else if(command.startsWith('os ')) os(command.split('--')[1]);

else if(command.startsWith('hash ')) hash(command.slice(5)); // Calculate hash for file and print it into console hash path_to_file
else if(command.startsWith('compress ')) compress(command.slice(9)); // compress path_to_file path_to_destination
else if(command.startsWith('decompress ')) decompress(command.slice(11)); // decompress path_to_file path_to_destination

else stdout.write('Invalid input\n');

}
15 changes: 15 additions & 0 deletions mv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Move file mv path_to_file path_to_new_directory

import { copy } from './cp.js';
import { del } from './del.js';

export async function mv(args){

await copy(args);

// if has been successfully copied - no need to check args again
const splittedArgs = args.split('" "');
const oldFilePath = splittedArgs[0].slice(1);
del(oldFilePath);

}
32 changes: 32 additions & 0 deletions navigate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { join } from 'path';
import { homedir } from 'os';


export function setNewWorkingDirectory(path){
const pathToWorkingDirectory = path || homedir();
try {
process.chdir(pathToWorkingDirectory);
console.log('You are currently in ' + process.cwd());
} catch (err) {
console.log('Operation failed. ' + '\n' + 'You are currently in ' + process.cwd());
}
}



export async function cd(pathToFolder){
const pathArr = pathToFolder.split('\\');
const folder = pathArr.pop();
const pathToWorkingDirectory = process.cwd();
const newPath = join(pathToWorkingDirectory, folder);
setNewWorkingDirectory(newPath);
}


export async function up(){
const pathToWorkingDirectory = process.cwd();
const pathArr = pathToWorkingDirectory.split('\\');
pathArr.pop();
const newPath = join(pathArr.join('\\'), '\\');
setNewWorkingDirectory(newPath);
}
Loading