Skip to content

Commit fe4e65c

Browse files
committed
fix: update fs commands
1 parent 774ec62 commit fe4e65c

File tree

9 files changed

+51
-10
lines changed

9 files changed

+51
-10
lines changed

src/app/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class App {
2929

3030
this.printHello = function () {
3131
console.log(consoleColors.yellow, `${msg.WELCOME_MSG}${this.userName}!${EOL}${msg.HELP_MSG}`);
32+
console.log(consoleColors.gray, `${EOL}${msg.CURRENT_DIR}${this.dir}${EOL}`);
3233

3334
};
3435
this.printBye = function () {

src/app/dispatcher.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import * as COMMAND from '../commands/commandList.js'
2+
import { BYE_MSG, OPERATION_FAILED, THANK_MSG } from '../common/messages.js';
3+
import { consoleColors } from '../utils/consoleColors.js';
24
import { parseInput } from './parser.js';
35

46

57
export const handleCommands = async (line) => {
68
const { command, params } = parseInput(line);
79
// ! refactor switch-case
810
switch (command) {
11+
case '.exit':
12+
console.log(consoleColors.yellow, `${THANK_MSG}${BYE_MSG}`);
13+
process.exit(0);
914
case 'help':
1015
await COMMAND.printHelp();
1116
break;
@@ -48,5 +53,7 @@ export const handleCommands = async (line) => {
4853
case 'decompress':
4954
await COMMAND.decompress(...params);
5055
break;
56+
default:
57+
console.log(consoleColors.red, OPERATION_FAILED);
5158
}
5259
}

src/commands/fs/cat.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1+
import { createReadStream } from 'fs';
2+
import { resolve as resolvePath } from 'path';
3+
import { cwd } from 'process';
14

2-
export const cat = async (command) => {
3-
console.log('Works')
5+
export const cat = async (path) => {
6+
await new Promise((resolve, reject) => {
7+
const filePath = resolvePath(cwd(), path);
8+
const fileContent = createReadStream(filePath, { encoding: 'utf8' });
9+
10+
fileContent.on('data', (chunk) => {
11+
console.log(chunk);
12+
});
13+
fileContent.on('error', reject);
14+
fileContent.on('end', resolve);
15+
});
416
};

src/commands/fs/rm.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import { rm } from 'fs/promises';
1+
import { rm as rmFile} from 'fs/promises';
2+
import { resolve } from 'path';
3+
import { cwd } from 'process';
24

35
import { rm_success } from '../../common/messages.js';
46

5-
export const remove = async (command) => {
6-
7+
export const remove = async (path) => {
8+
const filePath = resolve(cwd(), path);
9+
await rmFile(filePath);
710
console.log(rm_success);
811
};

src/commands/fs/rn.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { rename } from 'fs/promises';
2+
import { resolve } from 'path';
3+
import { cwd } from 'process';
24

35
import { rn_success } from '../../common/messages.js';
46

5-
export const rn = async (command) => {
6-
console.log(rn_success);
7+
export const rn = async (currentName, newName) => {
8+
const filePath = resolve(cwd(), currentName);
9+
const newFile = resolve(cwd(), newName);
10+
await rename(filePath, newFile);
11+
if (rename){
12+
console.log(rn_success);
13+
}
714
};

src/commands/hash/hash.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import { createHash } from 'crypto';
22
import { readFile } from 'fs/promises';
3+
import { resolve } from 'path';
4+
import { cwd } from 'process';
5+
36
import { hash_success } from '../../common/messages.js';
7+
import { consoleColors } from '../../utils/consoleColors.js'
48

5-
export const calculateHash = async (command) => {
6-
console.log(hash_success);
9+
export const calculateHash = async (path) => {
10+
const filePath = resolve(cwd(), path);
11+
const buffer = await readFile(filePath);
12+
const hash = createHash('sha256').update(buffer).digest('hex');
13+
console.log(consoleColors.green, hash_success);
14+
console.log(`Hash: ${hash}`)
715
};

src/commands/os/os.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EOL, cpus, homedir, userInfo, arch } from "os";
2-
import { OPERATION_FAILED } from "../../common/messages";
2+
import { OPERATION_FAILED } from "../../common/messages.js";
33

44
const osEOL = '--EOL';
55
const CPUS = '--cpus';

src/utils/consoleColors.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ export const consoleColors = {
22
red: '\x1b[31m%s\x1b[0m',
33
yellow: '\x1b[33m%s\x1b[0m',
44
cyan: '\x1b[36m%s\x1b[0m',
5+
gray: '\x1b[90m%s\x1b[0m',
6+
green: '\x1b[32m%s\x1b[0m'
57
};

test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1233ww

0 commit comments

Comments
 (0)