Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finish filemanager #609

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
27 changes: 27 additions & 0 deletions bin/file-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import process from 'node:process'
import { printWelcome } from '../src/utils/printWelcome.js';
import { getUserName } from '../src/utils/getUserName.js';
import { commandDispatch, printCommandsPromts } from '../src/utils/commandDispatch.js';
import { homedir } from 'node:os';

const fileManager = async () => {
const [shell, filename, cliUserName] = process.argv
const userName = getUserName(cliUserName)

printWelcome(userName)

let currentWorkingDir = { path: homedir() }
printCommandsPromts(currentWorkingDir.path);
process.stdin.on("data", async (input) => {
const [command, ...args] = input.toString().trim().split(' ')
printCommandsPromts(currentWorkingDir.path);
await commandDispatch(command, args, currentWorkingDir, userName);
});

process.on('SIGINT', () => {
commandDispatch('SIGINT', '', userName)
process.exit(1);
})
}

await fileManager();
21 changes: 4 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,12 @@
"name": "node-nodejs-basics",
"version": "1.0.0",
"description": "This repository is the part of nodejs-assignments https://github.com/AlreadyBored/nodejs-assignments",
"engines": {
"node": ">=20.0.0"
},
"type": "module",
"scripts": {
"cli:args": "node src/cli/args.js --some-arg value1 --other 1337 --arg2 42",
"cli:env": "npx cross-env SOME=any RSS_foo=bar RSS_bar=baz node src/cli/env.js",
"cp": "node src/cp/cp.js",
"fs:copy": "node src/fs/copy.js",
"fs:create": "node src/fs/create.js",
"fs:delete": "node src/fs/delete.js",
"fs:list": "node src/fs/list.js",
"fs:read": "node src/fs/read.js",
"fs:rename": "node src/fs/rename.js",
"hash": "node src/hash/calcHash.js",
"modules": "node src/modules/esm.mjs",
"streams:read": "node src/streams/read.js",
"streams:transform": "node src/streams/transform.js",
"streams:write": "node src/streams/write.js",
"wt": "node src/wt/main.js",
"zip:compress": "node src/zip/compress.js",
"zip:decompress": "node src/zip/decompress.js"
"start": "node ./bin/file-manager.js"
},
"repository": {
"type": "git",
Expand Down
5 changes: 0 additions & 5 deletions src/cli/args.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/cli/env.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/cp/cp.js

This file was deleted.

12 changes: 0 additions & 12 deletions src/cp/files/script.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/fs/copy.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/fs/create.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/fs/delete.js

This file was deleted.

1 change: 0 additions & 1 deletion src/fs/files/dontLookAtMe.txt

This file was deleted.

7 changes: 0 additions & 7 deletions src/fs/files/fileToRead.txt

This file was deleted.

1 change: 0 additions & 1 deletion src/fs/files/fileToRemove.txt

This file was deleted.

1 change: 0 additions & 1 deletion src/fs/files/hello.txt

This file was deleted.

3 changes: 0 additions & 3 deletions src/fs/files/wrongFilename.txt

This file was deleted.

5 changes: 0 additions & 5 deletions src/fs/list.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/fs/read.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/fs/rename.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/hash/calcHash.js

This file was deleted.

1 change: 0 additions & 1 deletion src/hash/files/fileToCalculateHashFor.txt

This file was deleted.

40 changes: 0 additions & 40 deletions src/modules/cjsToEsm.cjs

This file was deleted.

5 changes: 0 additions & 5 deletions src/modules/files/a.json

This file was deleted.

5 changes: 0 additions & 5 deletions src/modules/files/b.json

This file was deleted.

1 change: 0 additions & 1 deletion src/modules/files/c.js

This file was deleted.

1 change: 0 additions & 1 deletion src/streams/files/fileToRead.txt

This file was deleted.

Empty file removed src/streams/files/fileToWrite.txt
Empty file.
5 changes: 0 additions & 5 deletions src/streams/read.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/streams/transform.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/streams/write.js

This file was deleted.

94 changes: 94 additions & 0 deletions src/utils/commandDispatch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { compressFile } from "./compressFile.js";
import { createNewFile } from "./createNewFile.js";
import { decompressFile } from "./decompressFile.js";
import { deleteFile } from "./deleteFile.js";
import { getUpperDir } from "./getUpperDir.js";
import { goToDir } from "./goToDir.js";
import { moveFile } from "./moveFile.js";
import { printBye } from "./printBye.js";
import { printFileContent } from "./printFileContent.js";
import { printHash } from "./printHash.js";
import { printListOfFiles } from "./printListOfFiles.js";
import { printOS } from "./printOS.js";
import { renameFile } from "./renameFile.js";

const commandList = {
".exit": (_, args, [userName]) => {
printBye(userName)
process.exit(1);
},
"SIGINT": (_, args, [userName]) => {
printBye(userName)
process.exit(1);
},
"up": async (currentPath) => {
currentPath.path = await getUpperDir(currentPath.path);

},
"cd": async (currentPath, [destination]) => {
currentPath.path = await goToDisr(currentPath.path, destination);
},
"ls": async ({ path }) => {
await printListOfFiles(path);

},
"cat": async ({ path }, [filepath]) => {
await printFileContent(path, filepath);
},
"add": async ({ path }, [filepath]) => {
await createNewFile(path, filepath);
},
"rn": async ({ path }, [oldpath, newpath]) => {
await renameFile(path, oldpath, newpath)
},
"mv": async ({ path }, [oldpath, newpath]) => {
await moveFile(path, oldpath, newpath)
},
"rm": async ({ path }, [filepath]) => {
await deleteFile(path, filepath)
},
"os": ({ path }, [optionType]) => {
printOS(path, optionType)
},
"hash": async ({ path }, [filepath]) => {
await printHash(path, filepath);
},
"compress": async ({ path }, [oldpath, newpath]) => {
await compressFile(path, oldpath, newpath)
},
"decompress": async ({ path }, [archivepath, decompresspath]) => {
await decompressFile(path, archivepath, decompresspath)
}
}

export const commandPrompts = {
".exit": "Exit to File Manager",
"up": "Go to parent DIR",
"cd": "Go to dirpath <path>",
"ls": "Print list of files, from current DIR",
"cat": "Print file content by path <filename>",
"add": "Create new empty file <filename>, from current DIR",
"rn": "Rename file <oldname> <newname>",
"mv": "Move file <oldpath> <newpath>",
"rm": "Delete file by <filename>",
"os": "Print OS information. By setting --EOL, --cpus, --homedir, --username, --architecture",
"hash": "Print hash of file by filepath <filepath>",
"compress": "Compress file <filepath> <archive_filepath_without_extension>",
"decompress": "Decompress file <archfilepath> <decompressfilepath>"
}

export const printCommandsPromts = (currentWorkingDir) => {
const prompts = Object.entries(commandPrompts).map(([command, description]) => `${command} - ${description}`).join('\n')
console.log(`\nYou are currently in ${currentWorkingDir}`)

console.log('\nAvailable commands list\n');
console.log(prompts)
}

export const commandDispatch = async (command, args, currentWorkingDir = '', usename = '') => {
if (command in commandList) {
await commandList[command](currentWorkingDir, args, usename);
printCommandsPromts(currentWorkingDir.path)
}
else console.error('Invalid input')
}
45 changes: 45 additions & 0 deletions src/utils/compressFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import path from 'node:path'
import { createReadStream, createWriteStream } from 'node:fs';
import { pipeline } from "node:stream/promises";
import { access, stat } from 'fs/promises'
import { createBrotliCompress } from 'node:zlib';

export const compressFile = async (currentPath, oldname, newpath) => {
if (!oldname || !newpath) {
console.error('Invalid input')
return;
}

const absoldpath = path.resolve(currentPath, oldname)
const absnewpath = path.resolve(currentPath, newpath)

const isCreated = await access(absoldpath).then(() => true).catch(() => false);

if (!isCreated) {
console.error("File doesn't exists")
return;
}

const stats = await stat(absoldpath)

if (stats.isDirectory()) {
console.error('It is directory')
return
}

const isDestinationUse = await access(absnewpath).then(() => true).catch(() => false);

if (isDestinationUse) {
console.error('New destination already use')
return;
}

const brotli = createBrotliCompress();
const source = createReadStream(absoldpath);
const destination = createWriteStream(absnewpath);
try {
await pipeline(source, brotli, destination);
} catch(_) {
console.error('Cannot compress file')
}
}
Loading