Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
74 changes: 74 additions & 0 deletions src/commandService/commandService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {navigationService} from "../navigationService/navigationService.js";
import {OperatingSystemService} from "../operatingSystemService/operatingSystemService.js";
import {ERRORS} from "../constants/errors.js";
import {HashService} from "../hashService/hashService.js";
import {FileService} from "../fileService/fileService.js";
import {ZipService} from "../zipService/zipService.js";

export class CommandService {
static parseCommand(commandString) {
const items = commandString.trim().split(" ");
return {command: items[0], args: items.slice(1)}
}

static async executeCommand(commandString) {
const {command, args} = commandString;
let filePath, copyFilePath, compressDest;
switch (command) {
case 'up':
await navigationService.setDir('..');
break;
case 'ls':
await navigationService.getList();
break;
case 'cd':
await navigationService.setDir(args[0]);
break;
case 'os':
OperatingSystemService.getInfo(args[0]);
break;
case 'hash':
filePath = navigationService.getFilepath(args[0]);
await HashService.getHash(filePath);
break;
case 'cat':
filePath = navigationService.getFilepath(args[0]);
await FileService.readFile(filePath);
break;
case 'add':
filePath = navigationService.getFilepath(args[0]);
await FileService.addNewFile(filePath);
break;
case 'rn':
filePath = navigationService.getFilepath(args[0]);
await FileService.renameFile(filePath, args[1]);
break;
case 'rm':
filePath = navigationService.getFilepath(args[0]);
await FileService.removeFile(filePath);
break;
case 'cp':
filePath = navigationService.getFilepath(args[0]);
copyFilePath = navigationService.getFilepath(args[1]);
await FileService.copyFile(filePath, copyFilePath);
break;
case 'mv':
filePath = navigationService.getFilepath(args[0]);
copyFilePath = navigationService.getFilepath(args[1]);
await FileService.moveFile(filePath, copyFilePath);
break;
case 'compress':
filePath = navigationService.getFilepath(args[0]);
compressDest = navigationService.getFilepath(args[1]);
await ZipService.compressFile(filePath, compressDest);
break;
case 'decompress':
filePath = navigationService.getFilepath(args[0]);
compressDest = navigationService.getFilepath(args[1]);
await ZipService.decompressFile(filePath, compressDest);
break;
default:
throw new Error(ERRORS.INVALID_INPUT);
}
}
}
4 changes: 4 additions & 0 deletions src/constants/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const ERRORS = {
INVALID_INPUT: 'Invalid input',
OPERATION_FAILED: 'Operation failed',
}
90 changes: 90 additions & 0 deletions src/fileService/fileService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import fs from "fs";
import {writeFile, rename, access, rm} from "fs/promises"
import {ERRORS} from "../constants/errors.js";
import path from 'path';

export class FileService {
static async readFile(filePath) {
try {
await new Promise((resolve, reject) => {
const reader = fs.createReadStream(filePath);
reader.on('data', function (chunk) {
console.log(chunk.toString());
resolve();
});
reader.on('error', () => {
reject();
})
})
} catch {
throw new Error(ERRORS.OPERATION_FAILED);
}
}

static async addNewFile(filePath) {
try {
await writeFile(filePath, '', {flag: 'wx'});
console.log('File Created!')
} catch (e) {
throw new Error(ERRORS.OPERATION_FAILED);
}
}

static async renameFile(oldFilePath, newFileName) {
try {
const dir = path.dirname(oldFilePath);
const newFilePath = path.join(dir, newFileName);
const fileExists = await access(newFilePath, fs.constants.F_OK).then(() => true, () => false);
if (fileExists) throw new Error();

await rename(oldFilePath, newFilePath);
console.log('File renamed!');
} catch (e) {
throw new Error(ERRORS.OPERATION_FAILED);
}
}

static async removeFile(filePath) {
try {
await rm(filePath);
console.log('File removed!')
} catch {
throw new Error(ERRORS.OPERATION_FAILED);
}
}

static async moveFile(sourceFilePath, newFilePath) {
try {
await this.copyFile(sourceFilePath, newFilePath);
await this.removeFile(sourceFilePath);
} catch {
throw new Error(ERRORS.OPERATION_FAILED);
}
}

static async copyFile(sourceFilePath, newFilePath) {
try {
const fileName = path.basename(sourceFilePath);
const newFile = path.join(newFilePath, fileName);
await new Promise((resolve, reject) => {
const reader = fs.createReadStream(sourceFilePath);
const writer = fs.createWriteStream(newFile);

reader.on('error', (err) => {
reject();
});
writer.on('error', (err) => {
reject();
});
writer.on('close', () => {
console.log('File copied!');
resolve();
})

reader.pipe(writer);
})
} catch {
throw new Error(ERRORS.OPERATION_FAILED);
}
}
}
26 changes: 26 additions & 0 deletions src/hashService/hashService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import fs from "fs";
import crypto from "crypto";
import {ERRORS} from "../constants/errors.js";

export class HashService {
static async getHash(filePath) {
try {
await new Promise((resolve, reject) => {
const fh = fs.createReadStream(filePath);
const hash = crypto.createHash('sha256');
hash.setEncoding('hex');


fh.on('end', () => {
hash.end();
console.log(`Hash of the file - ${hash.read()}`);
resolve();
});
fh.on('error', reject);
fh.pipe(hash);
});
} catch (e) {
throw new Error(ERRORS.OPERATION_FAILED);
}
}
}
10 changes: 10 additions & 0 deletions src/helpers/get-username.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function getUsername() {
const usernameArg = process.argv.slice(2).find((arg, i, arr) => {
if (arg.startsWith("--username")) {
return true;
}
return false;
});

return usernameArg ? usernameArg.split("=")[1] : 'Unknown User';
}
19 changes: 19 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {initPromptService} from "./promptService/prompt-service.js";
import {getUsername} from "./helpers/get-username.js";
import {navigationService} from "./navigationService/navigationService.js";


function initApp() {
const username = getUsername();
process.stdout.write(`Welcome to the File Manager, ${username}!\n`);
process.stdout.write(`You're currently in ${navigationService.dir}\n`);

function onClose() {
process.stdout.write(`Thank you for using File Manager, ${username}, goodbye!`);
}

initPromptService(onClose);
}

initApp();

64 changes: 64 additions & 0 deletions src/navigationService/navigationService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import os from 'os';
import path from "path";
import fs from "fs/promises";
import {ERRORS} from "../constants/errors.js";

export class NavigationService {
constructor() {
this.currentDir = os.homedir();
}

get dir() {
return this.currentDir;
}

async setDir(pathCommand) {
try {
const newPath = path.resolve(this.currentDir, pathCommand);
const status = await fs.stat(newPath);
if (!status.isDirectory()) {
throw new Error();
}
this.currentDir = newPath;
} catch (e) {
throw new Error(ERRORS.INVALID_INPUT);
}
}

async getList() {
try {
let files = [];
let folders = [];
const items = await fs.readdir(this.currentDir, {withFileTypes: true});

items.forEach((item) => {
if (item.isDirectory()) {
folders.push(item.name);
} else {
files.push(item.name);
}
});

files.sort();
folders.sort();

files = files.map((item) => ({Name: item, Type: 'file'}))
folders = folders.map((item) => ({Name: item, Type: 'directory'}));

console.table([...folders, ...files]);

} catch (err) {
throw new Error(ERRORS.OPERATION_FAILED);
}
}

getFilepath(filePath) {
try {
return path.resolve(this.currentDir, filePath);
} catch {
throw new Error(ERRORS.INVALID_INPUT);
}
}
}

export const navigationService = new NavigationService();
50 changes: 50 additions & 0 deletions src/operatingSystemService/operatingSystemService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import os from 'os';
import {ERRORS} from "../constants/errors.js";

export class OperatingSystemService {
static getInfo(command) {
switch (command) {
case '--EOL':
this.getEOL();
break;
case '--cpus':
this.getCpus();
break;
case '--homedir':
this.getHomedir();
break;
case '--username':
this.getUsername();
break;
case '--architecture':
this.getArchitecture();
break;
default:
throw new Error(ERRORS.INVALID_INPUT)
}
}

static getCpus() {
const cpuInfo = os.cpus();
console.log(`Overall amount of CPUS - ${cpuInfo.length}`);
for (let cpu of cpuInfo) {
console.log(`${cpu.model} - Speed ${cpu.speed/1000} GHz`);
}
}

static getEOL() {
console.log(`End of line marker - ${JSON.stringify(os.EOL)}`);
}

static getHomedir() {
console.log(`Your node directory - ${os.homedir()}`);
}

static getUsername() {
console.log(`Your system username - ${os.userInfo().username}`);
}

static getArchitecture() {
console.log(`CPU architecture ${process.arch}`);
}
}
24 changes: 24 additions & 0 deletions src/promptService/prompt-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {createInterface} from 'readline/promises';
import {navigationService} from "../navigationService/navigationService.js";
import {CommandService} from "../commandService/commandService.js";

let rl;
export const initPromptService = (onClose) => {
rl = createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on('line', async (line) => {
try {
const command = CommandService.parseCommand(line);
if (command.command === '.exit') {
rl.close(); return;
}
await CommandService.executeCommand(command);
} catch (e) {
console.log(e.message);
}
process.stdout.write(`You're currently in ${navigationService.dir}\n`);
});
rl.on('close', onClose);
}
Loading