Skip to content

Commit 48b91bb

Browse files
committed
refactor: update fs and brotli commands
1 parent fe4e65c commit 48b91bb

File tree

12 files changed

+81
-86
lines changed

12 files changed

+81
-86
lines changed

src/app/dispatcher.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import * as COMMAND from '../commands/commandList.js'
2-
import { BYE_MSG, OPERATION_FAILED, THANK_MSG } from '../common/messages.js';
2+
import { BYE_MSG, OPERATION_FAILED, THANK_MSG, compress_success, decompress_success } from '../common/messages.js';
33
import { consoleColors } from '../utils/consoleColors.js';
44
import { parseInput } from './parser.js';
55

6-
76
export const handleCommands = async (line) => {
87
const { command, params } = parseInput(line);
9-
// ! refactor switch-case
108
switch (command) {
119
case '.exit':
1210
console.log(consoleColors.yellow, `${THANK_MSG}${BYE_MSG}`);
@@ -48,10 +46,12 @@ export const handleCommands = async (line) => {
4846
await COMMAND.calculateHash(...params);
4947
break;
5048
case 'compress':
51-
await COMMAND.compress(...params);
49+
await COMMAND.brotli('compress', ...params);
50+
console.log(consoleColors.green, compress_success)
5251
break;
5352
case 'decompress':
54-
await COMMAND.decompress(...params);
53+
await COMMAND.brotli('decompress', ...params);
54+
console.log(consoleColors.green, decompress_success)
5555
break;
5656
default:
5757
console.log(consoleColors.red, OPERATION_FAILED);

src/app/parser.js

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { INVALID_PARAM } from "../common/messages.js";
2+
13
export const parseInput = (line) => {
24
const trimmedInput = line.trim();
35
const [command] = trimmedInput.split(' ');
@@ -13,36 +15,20 @@ export const parseInput = (line) => {
1315

1416
const separateParams = (paramsLine) => {
1517
let params = [];
16-
17-
const lineTrimmed = paramsLine.trim();
18-
19-
if (!lineTrimmed.length) return params;
20-
21-
const firstSymbol = lineTrimmed.slice(0, 1);
22-
23-
if (firstSymbol === "'" || firstSymbol === '"') {
24-
const paramEnd = lineTrimmed.indexOf(firstSymbol, 1);
25-
26-
if (paramEnd === -1) {
27-
throw new Error('Invalid param');
28-
}
29-
30-
const param = lineTrimmed.slice(1, paramEnd);
31-
32-
if (param.includes('"') || param.includes("'")) {
33-
throw new Error('Invalid param');
34-
}
35-
36-
const rest = lineTrimmed.slice(paramEnd + 1);
18+
const trimmedLine = paramsLine.trim();
19+
if (!trimmedLine.length) return params;
20+
const first = trimmedLine.slice(0, 1);
21+
if (first === "'" || first === '"') {
22+
const end = trimmedLine.indexOf(first, 1);
23+
if (end === -1) throw new Error(INVALID_PARAM);
24+
const param = trimmedLine.slice(1, end);
25+
if (param.includes('"') || param.includes("'")) throw new Error(INVALID_PARAM)
26+
const rest = trimmedLine.slice(end + 1);
3727
params.push(param, ...separateParams(rest));
3828
} else {
39-
const [param] = lineTrimmed.split(' ');
40-
41-
if (param.includes('"') || param.includes("'")) {
42-
throw new Error('Invalid param');
43-
}
44-
45-
params.push(param, ...separateParams(lineTrimmed.slice(param.length)));
29+
const [param] = trimmedLine.split(' ');
30+
if (param.includes('"') || param.includes("'")) throw new Error(INVALID_PARAM);
31+
params.push(param, ...separateParams(trimmedLine.slice(param.length)));
4632
}
4733

4834
return params;

src/commands/commandList.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ export { os } from './os/os.js';
1414
//HASH
1515
export { calculateHash } from './hash/hash.js';
1616
//ZIP
17-
export { compress } from './zip/compress.js';
18-
export { decompress } from './zip/decompress.js';
17+
export { brotli } from './zip/brotli.js';
1918
//HELP
2019
export { printHelp } from './help/help.js';
2120

src/commands/fs/cp.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
import { createReadStream, createWriteStream } from 'fs';
2-
import { basename, join } from 'path';
2+
import { resolve as pathResolve, basename } from 'path';
33

4-
export const cp = async (command) => {
5-
console.log('ll')
6-
};
4+
export const cp = async (path, newPath) => {
5+
const fileToCopyPath = pathResolve(process.cwd(), path);
6+
const fileName = basename(fileToCopyPath);
7+
const dirToWritePath = pathResolve(process.cwd(), newPath);
8+
const fileToWritePath = pathResolve(dirToWritePath, fileName);
9+
try {
10+
await new Promise(async (res, rej) => {
11+
const fileToCopy = createReadStream(fileToCopyPath, { encoding: 'utf8' });
12+
const fileToWrite = createWriteStream(fileToWritePath, {
13+
flags: 'wx',
14+
});
15+
16+
fileToCopy.pipe(fileToWrite);
17+
fileToCopy.on('error', rej);
18+
fileToCopy.on('end', res);
19+
});
20+
} catch (error) {
21+
throw error;
22+
}
23+
}

src/commands/fs/mv.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { cp } from "./cp.js"
22
import { remove } from "./rm.js"
33

4-
export const mv = async (command) => {
5-
await cp(command);
6-
await remove(command);
4+
export const mv = async (...params) => {
5+
await cp(...params);
6+
await remove(...params);
77
}

src/commands/fs/rm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ import { rm_success } from '../../common/messages.js';
77
export const remove = async (path) => {
88
const filePath = resolve(cwd(), path);
99
await rmFile(filePath);
10-
console.log(rm_success);
10+
1111
};

src/commands/fs/rn.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import { rename } from 'fs/promises';
22
import { resolve } from 'path';
33
import { cwd } from 'process';
4-
54
import { rn_success } from '../../common/messages.js';
65

76
export const rn = async (currentName, newName) => {
87
const filePath = resolve(cwd(), currentName);
98
const newFile = resolve(cwd(), newName);
109
await rename(filePath, newFile);
11-
if (rename){
12-
console.log(rn_success);
13-
}
1410
};

src/commands/zip/brotli.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { createReadStream, createWriteStream } from 'fs';
2+
import { resolve, basename, parse } from 'path';
3+
import { createBrotliDecompress, createBrotliCompress } from 'zlib';
4+
5+
export const brotli = async (option, path, newPath) => {
6+
const EXT = 'br';
7+
try {
8+
const needCompress = option === 'compress';
9+
10+
const fileToConvertPath = resolve(process.cwd(), path);
11+
12+
const baseName = basename(fileToConvertPath);
13+
const fileName = parse(baseName).name;
14+
15+
const fileNameToWrite = needCompress ? `${baseName}.${EXT}` : fileName;
16+
const dirToWritePath = resolve(process.cwd(), newPath);
17+
const fileToWritePath = resolve(dirToWritePath, fileNameToWrite);
18+
19+
await new Promise((res, rej) => {
20+
const fileToConvert = createReadStream(fileToConvertPath);
21+
const fileToWrite = createWriteStream(fileToWritePath, { flags: 'wx',});
22+
23+
const brotliHandler = needCompress ? createBrotliCompress() : createBrotliDecompress();
24+
25+
fileToConvert.pipe(brotliHandler).pipe(fileToWrite);
26+
fileToConvert.on('error', rej);
27+
fileToConvert.on('end', res);
28+
});
29+
} catch (err) {
30+
throw err;
31+
}
32+
};
33+

src/commands/zip/compress.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/commands/zip/decompress.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)