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
49 changes: 49 additions & 0 deletions compress/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as path from 'path';
import { getCurrentDir } from '../data.mjs';
import { access } from 'node:fs/promises';
import * as fs from 'node:fs';
import * as zlib from 'zlib';
import { exec } from '../executor.mjs';
import { pipeline } from 'node:stream/promises';

export const compress = async (fileToPath, pathToDestination) => {

await exec(async () => {

const currentDir = getCurrentDir();
const pathResolved = path.resolve(currentDir, fileToPath);
await access(pathResolved);

const readableStream = fs.createReadStream(pathResolved);
const writeStream = fs.createWriteStream(path.resolve(currentDir, pathToDestination));
const brotli = zlib.createBrotliCompress();

await pipeline(
readableStream,
brotli,
writeStream
);

})
}

export const decompress = async (fileToPath, pathToDestination) => {

await exec(async () => {

const currentDir = getCurrentDir();
const pathResolved = path.resolve(currentDir, fileToPath);
await access(pathResolved);

const readableStream = fs.createReadStream(pathResolved);
const writeStream = fs.createWriteStream(path.resolve(currentDir, pathToDestination));
const brotli = zlib.createBrotliDecompress();

await pipeline(
readableStream,
brotli,
writeStream
);

})
}
10 changes: 10 additions & 0 deletions data.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as os from 'os';


let currentDir = os.homedir();

export const getCurrentDir = () => currentDir;

export const setCurrentDir = (newDir) => {
currentDir = newDir;
};
10 changes: 10 additions & 0 deletions executor.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { messageEmitter } from './messageEmmiter.mjs';

export const exec = async ( cb) => {
try {
await cb();
messageEmitter.emit('currentDir');
} catch (error) {
messageEmitter.emit('operationFailed');
}
}
9 changes: 9 additions & 0 deletions exit/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as path from 'path';
import { access, readdir, stat } from 'node:fs/promises';
import { messageEmitter } from '../messageEmmiter.mjs';
import { getCurrentDir, setCurrentDir } from '../data.mjs';
import { exec } from '../executor.mjs';

export function exit() {
process.exit();
}
14 changes: 14 additions & 0 deletions files/add.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { writeFile } from 'node:fs/promises';
import { exec } from '../executor.mjs';
import * as path from 'path';
import { getCurrentDir } from '../data.mjs';

export const add = async (newFileName) =>{
await exec(
async ()=> {
const currentDir = getCurrentDir();
const pathResolved = path.resolve(currentDir, newFileName);
await writeFile(pathResolved, '');
}
)
}
31 changes: 31 additions & 0 deletions files/cat.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as path from 'path';
import { getCurrentDir } from '../data.mjs';
import { access } from 'node:fs/promises';
import * as fs from 'node:fs';
import { messageEmitter } from '../messageEmmiter.mjs';

export const cat = async (fileToPath) => {
try {
const currentDir = getCurrentDir();
const pathResolved = path.resolve(currentDir, fileToPath);
await access(pathResolved);

const readableStream = fs.createReadStream(pathResolved).setEncoding('utf8');

let data='';
readableStream.on('data', (chunk) => {
data+=chunk;
});

readableStream.once('end', () => {
messageEmitter.emit('message', ()=> console.log(data));
});

readableStream.on('error', () => {
messageEmitter.emit('operationFailed');
})

} catch (error) {
messageEmitter.emit('operationFailed');
}
}
22 changes: 22 additions & 0 deletions files/cp.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as path from 'path';
import { getCurrentDir } from '../data.mjs';
import { access } from 'node:fs/promises';
import * as fs from 'node:fs';
import { pipeline } from 'node:stream/promises';
import { exec } from '../executor.mjs';

export const cp = async (pathToFile, pathToDirectory) => {


await exec(async () => {
const currentDir = getCurrentDir();
const pathResolved = path.resolve(currentDir, pathToFile);
await access(pathResolved);
const {base}= path.parse(pathResolved);
const pathToCopy = path.resolve(currentDir, pathToDirectory, base);

const readableStream = fs.createReadStream(pathResolved);
const writtableStream = fs.createWriteStream(pathToCopy);
await pipeline(readableStream, writtableStream);
});
}
6 changes: 6 additions & 0 deletions files/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from './add.mjs';
export * from './cat.mjs';
export * from './rn.mjs';
export * from './rm.mjs';
export * from './cp.mjs';
export * from './mv.mjs';
25 changes: 25 additions & 0 deletions files/mv.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as path from 'path';
import { getCurrentDir } from '../data.mjs';
import { access } from 'node:fs/promises';
import * as fs from 'node:fs';
import { rm } from 'node:fs/promises';
import { pipeline } from 'node:stream/promises';
import { exec } from '../executor.mjs';

export const mv = async (pathToFile, pathToNewDir) => {

await exec(async () => {
const currentDir = getCurrentDir();
const pathResolved = path.resolve(currentDir, pathToFile);
await access(pathResolved);
const {base}= path.parse(pathResolved);
const pathToCopy = path.resolve(currentDir, pathToNewDir, base);

const readableStream = fs.createReadStream(pathResolved);
const writtableStream = fs.createWriteStream(pathToCopy);

await pipeline(readableStream, writtableStream);
await rm(pathResolved);

});
}
15 changes: 15 additions & 0 deletions files/rm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as path from 'path';
import { getCurrentDir } from '../data.mjs';
import { access } from 'node:fs/promises';
import { rm as remove } from 'node:fs/promises';
import { exec } from '../executor.mjs';

export const rm = async (pathToFile) => {

await exec(async () => {
const currentDir = getCurrentDir();
const pathResolved = path.resolve(currentDir, pathToFile);
await access(pathResolved);
await remove(pathResolved);
})
}
16 changes: 16 additions & 0 deletions files/rn.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as path from 'path';
import { getCurrentDir } from '../data.mjs';
import { access } from 'node:fs/promises';
import { rename as fsRename } from 'node:fs/promises';

import { exec } from '../executor.mjs';

export const rn = async (pathToFile, newFileName) => {
await exec(async () => {
const currentDir = getCurrentDir();
const pathResolved = path.resolve(currentDir, pathToFile);
await access(pathResolved);
const oldFileName = path.parse(pathResolved).base
await fsRename(pathResolved, pathResolved.replace(oldFileName, newFileName));
})
}
20 changes: 20 additions & 0 deletions hash/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as path from 'path';
import { getCurrentDir } from '../data.mjs';
import { access } from 'node:fs/promises';
import { exec } from '../executor.mjs';
import { createHash } from 'node:crypto';
import { readFile } from 'node:fs/promises';
import { messageEmitter } from '../messageEmmiter.mjs';

export const calculateHash = async (fileToPath) => {

await exec(async ()=> {
const currentDir = getCurrentDir();
const pathResolved = path.resolve(currentDir, fileToPath);
await access(pathResolved);
const content = await readFile(pathResolved);
const hash = createHash('sha256');
const res = hash.update(content).digest('hex');
messageEmitter.emit('text', res);
});
}
26 changes: 26 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { rl } from './readline.mjs';
import { messageEmitter } from './messageEmmiter.mjs';

let userName='';

function greeting() {
try{
const userNameArg = process.argv.find(arg => arg.startsWith('--username='));
userName=userNameArg.split('=')[1];
}catch(er){
console.log(process.argv[2]);
console.error('"--username" argument is not found. Please use correct argument');
process.exit(1);
}

process.on('exit', ()=> {
messageEmitter.emit('goodbye',userName );
});

messageEmitter.emit('greeting',userName );
rl.prompt(true);
}


greeting();

42 changes: 42 additions & 0 deletions messageEmmiter.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {EventEmitter} from 'node:events';
import { getCurrentDir, setCurrentDir } from './data.mjs';

class MessageEmitter extends EventEmitter {}

export const messageEmitter = new MessageEmitter();
messageEmitter.on('greeting', (user) => {
console.log(`Welcome to the File Manager, ${user}!`);
messageEmitter.emit('currentDir');
});

messageEmitter.on('goodbye', (user) => {
console.log(`Thank you for using File Manager, ${user}, goodbye!`)});

messageEmitter.on('currentDir', () => {
console.log(`You are currently in ${getCurrentDir()}`);

});

messageEmitter.on('invalidInput', () => {
console.log(`Invalid input`);
messageEmitter.emit('currentDir');
});

messageEmitter.on('operationFailed', () => {
console.log(`Operation failed`);
messageEmitter.emit('currentDir');
});


messageEmitter.on('message', (cb) => {
cb();
messageEmitter.emit('currentDir');
});

messageEmitter.on('text', (text) => {
console.log(text);
});




46 changes: 46 additions & 0 deletions nwd/up.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as path from 'path';
import { access, readdir, stat } from 'node:fs/promises';
import { messageEmitter } from '../messageEmmiter.mjs';
import { getCurrentDir, setCurrentDir } from '../data.mjs';
import { exec } from '../executor.mjs';

export function up() {
exec(() => {
const currentDir = path.resolve(getCurrentDir(), '..');
setCurrentDir(currentDir);
});
}


export const cd = async (dest) => {

await exec(async () => {
const newPath = path.resolve(getCurrentDir(), dest);
await access(newPath);
setCurrentDir(newPath);
});
}

export const ls = async () => {

await exec(async () => {
const currentDir = getCurrentDir();
const items = await readdir(currentDir);
const res = items.map(async (item) => {
const itemPath = path.resolve(currentDir, `./${item}`);
const statItem = await stat(itemPath).then(s => s.isFile());

return {
['Name'] : item,
['Type'] : statItem ? 'file' : 'directory',
}

});

await Promise.all(res).then(r=> r.sort((a, b) => a.Type.localeCompare(b.Type))).then(
console.table


);
});
}
Loading