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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,4 @@ dist

# TernJS port file
.tern-port
.DS_Store
28 changes: 28 additions & 0 deletions commands/file/copy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { createReadStream, createWriteStream } from 'node:fs';
import { exists } from '../../utils/fsUtils.mjs'
import { errMsg } from '../../utils/constants.mjs';

export const copy = async (filePath, fileCopyPath) => {
if (!exists(fileCopyPath)) {;
const readable = createReadStream(filePath);
const writable = createWriteStream(fileCopyPath);

readable.on('error', (err) => {
errMsg();
return
});

writable.on('error', (err) => {
errMsg();
readable.destroy();
writable.end();
return;
});

writable.on('finish', () => {
console.log(`${filePath} was copied successfully`)
});

readable.pipe(writable);
}
};
21 changes: 21 additions & 0 deletions commands/file/create.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createWriteStream } from 'node:fs';
import path from 'node:path';
import { cwd } from 'node:process';
import { exists } from '../../utils/fsUtils.mjs'
import { errMsg } from '../../utils/constants.mjs';

export const create = async (fileName) => {
const filePath = path.join(cwd(), fileName);

if (exists(fileName)) {
console.log(errMsg);
return;
} else {
const writable = createWriteStream(filePath);
writable.write('');
writable.end();
writable.on('finish', () => {
console.log(`${fileName} was created`);
})
}
};
18 changes: 18 additions & 0 deletions commands/file/delete.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { rm } from 'node:fs';
import { errMsg } from '../../utils/constants.mjs';
import { exists } from '../../utils/fsUtils.mjs';

export const remove = async (fileToRemove) => {
if (!exists(fileToRemove)) {
errMsg();
return;
}

rm(fileToRemove, (err) => {
if (err) {
errMsg();
} else {
console.log(`${fileToRemove} was deleted`);
}
})
};
33 changes: 33 additions & 0 deletions commands/file/fileCommandsHandler.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { read } from './read.mjs';
import { create } from './create.mjs';
import { rename } from './rename.mjs';
import { copy } from './copy.mjs'
import { move } from './move.mjs'
import { remove } from './delete.mjs'
import { invalidMsg } from '../../utils/constants.mjs'

export const handleFileCommand = (command, args) => {
switch (command) {
case 'cat':
read(args);
break;
case 'add':
create(args);
break;
case 'rn':
rename(args[0], args[1]);
break;
case 'cp':
copy(args[0], args[1]);
break;
case 'mv':
move(args[0], args[1]);
break;
case 'rm':
remove(args);
break;
default:
console.log(invalidMsg);
return;
}
}
12 changes: 12 additions & 0 deletions commands/file/list.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { readdirSync } from 'node:fs';
import { errMsg } from '../../utils/constants.mjs';

export const list = async () => {
const fileList = readdirSync('.');

try {
console.log(fileList) ;
} catch (err) {
console.log(errMsg());
}
};
32 changes: 32 additions & 0 deletions commands/file/move.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { createReadStream, createWriteStream, rm } from 'node:fs';
import { exists } from '../../utils/fsUtils.mjs'
import { errMsg } from '../../utils/constants.mjs';

export const move = async (filePath, fileCopyPath) => {
if (!exists(fileCopyPath)) errMsg();

const readable = createReadStream(filePath, {encoding: 'utf8'});
const writable = createWriteStream(fileCopyPath);

readable.on('error', () => {
errMsg();
});

writable.on('error', () => {
errMsg();
readable.destroy();
writable.end();
});

writable.on('finish', (err) => {
if (err) errMsg();

rm(filePath, (err) => {
if (err) errMsg();
});

console.log(`${filePath} was moved successfully`)
});

readable.pipe(writable);
};
23 changes: 23 additions & 0 deletions commands/file/read.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { createReadStream } from 'node:fs';
import { exists } from '../../utils/fsUtils.mjs';
import { errMsg } from '../../utils/constants.mjs';

export const read = async (filePath) => {
if (!exists(filePath)) {
console.log(errMsg());
return;
} else {
const readable = createReadStream(filePath, 'utf8');

readable
.on('data', (chunk) => {
console.log(chunk);
})
.on('end', () => {
console.log('end-of-file');
})
.on('error', () => {
console.log(errMsg());
})
}
};
15 changes: 15 additions & 0 deletions commands/file/rename.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import fs from 'node:fs';
import { exists } from '../../utils/fsUtils.mjs'
import { errMsg } from '../../utils/constants.mjs';

export const rename = async (srcFileName, destFileName) => {
if (exists(destFileName)) {
console.log(errMsg());
return;
} else {
fs.rename(srcFileName, destFileName, (err) => {
if (err) { console.log(errMsg()) }
else { console.log('File was renamed') };
});
}
};
19 changes: 19 additions & 0 deletions commands/misc/hash/calcHash.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createHash } from 'node:crypto';
import { createReadStream } from 'node:fs';

export const calcHash = async (filePath) => {
const readable = createReadStream(filePath, 'utf8');
const hash = createHash('sha256');
hash.setEncoding('hex');

readable
.on('end', () => {
hash.end();
console.log(hash.read());
})
.on('error', () => {
console.log(errMsg());
})

readable.pipe(hash);
}
25 changes: 25 additions & 0 deletions commands/misc/miscCommandsHandler.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { handleOsCommand } from './os/osCommandsHandler.mjs';
import { compress} from './zip/compress.mjs'
import { decompress} from './zip/decompress.mjs'
import { invalidMsg } from '../../utils/constants.mjs'
import { calcHash } from './hash/calcHash.mjs';

export const handleMiscCommand = (command, args) => {
switch (command) {
case 'os':
handleOsCommand(args);
break;
case 'hash':
calcHash(args);
break;
case 'compress':
compress(args[0], args[1]);
break;
case 'decompress':
decompress(args[0], args[1]);
break;
default:
invalidMsg();
return;
}
}
16 changes: 16 additions & 0 deletions commands/misc/os/osCommands.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { EOL, cpus, userInfo } from "os";
import { arch } from 'node:process';

const eolInfo = () => console.log(JSON.stringify(EOL));

const cpusInfo = () => {
console.log(`Number of CPUs: ${cpus().length}`)
console.log(cpus().map(cpu => `${cpu.model}, clock rate: ${cpu.speed}`));
}
const homedirInfo = () => console.log(userInfo().homedir);

const usernameInfo = () => console.log(userInfo().username);

const archInfo = () => console.log(arch);

export { eolInfo, cpusInfo, homedirInfo, usernameInfo, archInfo }
25 changes: 25 additions & 0 deletions commands/misc/os/osCommandsHandler.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { eolInfo, cpusInfo, homedirInfo, usernameInfo, archInfo } from './osCommands.mjs'
import { invalidMsg } from "../../../utils/constants.mjs";

export const handleOsCommand = (input) => {
switch (input.slice(2)) {
case 'EOL':
eolInfo();
break;
case 'cpus':
cpusInfo();
break;
case 'homedir':
homedirInfo();
break;
case 'username':
usernameInfo();
break;
case 'architecture':
archInfo();
break;
default:
invalidMsg();
break;
};
}
23 changes: 23 additions & 0 deletions commands/misc/zip/compress.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { createBrotliCompress } from 'node:zlib';
import { createWriteStream, createReadStream } from 'node:fs';
import { errMsg } from '../../../utils/constants.mjs';
import { exists } from '../../../utils/fsUtils.mjs'

export const compress = async (fileName, archName) => {
if (!exists(fileName)) return errMsg();

const readable = createReadStream(fileName);
const writable = createWriteStream(archName);

const brotli = createBrotliCompress();
const stream = readable.pipe(brotli).pipe(writable);

stream
.on('finish', () => {
console.log(`${archName} was successfully created`)
})
.on('error', () => {
errMsg();
});

};
24 changes: 24 additions & 0 deletions commands/misc/zip/decompress.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createBrotliDecompress } from 'node:zlib';
import { createWriteStream, createReadStream } from 'node:fs';
import { exists } from '../../../utils/fsUtils.mjs';
import { create } from '../../file/create.mjs'
import { errMsg } from '../../../utils/constants.mjs';

export const decompress = async (archName, fileName) => {
if (!exists(fileName)) create(fileName);
else if (!exists(archName)) return errMsg();

const readable = createReadStream(archName);
const writable = createWriteStream(fileName);

const brotli = createBrotliDecompress();
const stream = readable.pipe(brotli).pipe(writable);

stream
.on('finish', () => {
console.log(`${archName} was successfully decompressed`)
})
.on('error', () => {
errMsg();
});
};
21 changes: 21 additions & 0 deletions commands/nav/navCommandsHandler.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { chdir, cwd } from 'node:process';
import { list } from '../file/list.mjs';
import { sep } from 'node:path';
import { errMsg, invalidMsg } from '../../utils/constants.mjs'

export const handleNavCommand = (command, arg) => {
switch (command) {
case 'up':
chdir('..' + sep);
break;
case 'ls':
list(cwd());
break;
case 'cd':
try { chdir(cwd() + sep + arg); } catch { errMsg() };
break;
default:
invalidMsg();
return;
}
}
Loading