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
18 changes: 1 addition & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,7 @@
},
"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 src/index.js"
},
"repository": {
"type": "git",
Expand Down
5 changes: 0 additions & 5 deletions src/cli/env.js

This file was deleted.

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

This file was deleted.

19 changes: 0 additions & 19 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.

228 changes: 228 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
import readline from 'node:readline';
import { homedir } from 'node:os';
import { goUp, changeDirectory, listDirectory } from './modules/navigation.js';
import * as fileOps from './modules/fileOperations.js';
import { handleOSCommand } from './modules/osInfo.js';
import { calculateHash } from './modules/hash.js';
import { compressFile, decompressFile } from './modules/compression.js';
import { validateArgs } from './modules/utils.js';
import { logger } from './modules/logger.js';
import { COMMANDS, ERROR_MESSAGES, SUCCESS_MESSAGES } from './modules/constants.js';

function getUsername() {
const args = process.argv.slice(2);

for (const arg of args) {
if (arg.startsWith('--username=')) {
const username = arg.split('=')[1];
if (username && username.trim() !== '') {
return username.trim();
}
}
}

throw new Error(ERROR_MESSAGES.USERNAME_REQUIRED);
}

function createReadlineInterface() {
return readline.createInterface({
input: process.stdin,
output: process.stdout
});
}

function displayCurrentDirectory(pathArg) {
logger.directory(`You are currently in ${pathArg}`);
}

function parseCommand(input) {
if (!input || typeof input !== 'string') {
return { command: '', args: [] };
}

const trimmed = input.trim();
if (trimmed === '') {
return { command: '', args: [] };
}

const parts = trimmed.split(/\s+/);
const command = parts[0];
const args = parts.slice(1);

return { command, args };
}

async function processCommand(input, currentDir) {
const { command, args } = parseCommand(input);

if (command === '') {
return { error: ERROR_MESSAGES.INVALID_INPUT };
}

try {
switch (command) {
case COMMANDS.UP:
if (!validateArgs(args, 0, 0)) {
return { error: ERROR_MESSAGES.INVALID_INPUT };
}
const newDirUp = await goUp(currentDir);
return { newDir: newDirUp };

case COMMANDS.CD:
if (!validateArgs(args, 1, 1)) {
return { error: ERROR_MESSAGES.INVALID_INPUT };
}
const newDirCd = await changeDirectory(currentDir, args[0]);
return { newDir: newDirCd };

case COMMANDS.LS:
if (!validateArgs(args, 0, 0)) {
return { error: ERROR_MESSAGES.INVALID_INPUT };
}
const listing = await listDirectory(currentDir);
return { result: '' };

case COMMANDS.CAT:
if (!validateArgs(args, 1, 1)) {
return { error: ERROR_MESSAGES.INVALID_INPUT };
}
const content = await fileOps.readFile(currentDir, args[0]);
if (content === '') {
return { result: SUCCESS_MESSAGES.FILE_EMPTY };
}
return { result: content };

case COMMANDS.ADD:
if (!validateArgs(args, 1, 1)) {
return { error: ERROR_MESSAGES.INVALID_INPUT };
}
await fileOps.createFile(currentDir, args[0]);
return { result: SUCCESS_MESSAGES.FILE_CREATED };

case COMMANDS.MKDIR:
if (!validateArgs(args, 1, 1)) {
return { error: ERROR_MESSAGES.INVALID_INPUT };
}
await fileOps.createDirectory(currentDir, args[0]);
return { result: SUCCESS_MESSAGES.DIRECTORY_CREATED };

case COMMANDS.RENAME:
if (!validateArgs(args, 2, 2)) {
return { error: ERROR_MESSAGES.INVALID_INPUT };
}
await fileOps.rename(currentDir, args[0], args[1]);
return { result: SUCCESS_MESSAGES.FILE_RENAMED };

case COMMANDS.COPY:
if (!validateArgs(args, 2, 2)) {
return { error: ERROR_MESSAGES.INVALID_INPUT };
}
await fileOps.copyFile(currentDir, args[0], args[1]);
return { result: SUCCESS_MESSAGES.FILE_COPIED };

case COMMANDS.MOVE:
if (!validateArgs(args, 2, 2)) {
return { error: ERROR_MESSAGES.INVALID_INPUT };
}
await fileOps.moveFile(currentDir, args[0], args[1]);
return { result: SUCCESS_MESSAGES.FILE_MOVED };

case COMMANDS.DELETE:
if (!validateArgs(args, 1, 1)) {
return { error: ERROR_MESSAGES.INVALID_INPUT };
}
await fileOps.deleteFile(currentDir, args[0]);
return { result: SUCCESS_MESSAGES.FILE_DELETED };

case COMMANDS.OS:
if (!validateArgs(args, 1, 1)) {
return { error: ERROR_MESSAGES.INVALID_INPUT };
}
const osResult = handleOSCommand(args[0]);
logger.osInfo(osResult);
return { result: '' };

case COMMANDS.HASH:
if (!validateArgs(args, 1, 1)) {
return { error: ERROR_MESSAGES.INVALID_INPUT };
}
const hashResult = await calculateHash(currentDir, args[0]);
logger.hash(hashResult);
return { result: '' };

case COMMANDS.COMPRESS:
if (!validateArgs(args, 2, 2)) {
return { error: ERROR_MESSAGES.INVALID_INPUT };
}
await compressFile(currentDir, args[0], args[1]);
return { result: SUCCESS_MESSAGES.FILE_COMPRESSED };

case COMMANDS.DECOMPRESS:
if (!validateArgs(args, 2, 2)) {
return { error: ERROR_MESSAGES.INVALID_INPUT };
}
await decompressFile(currentDir, args[0], args[1]);
return { result: SUCCESS_MESSAGES.FILE_DECOMPRESSED };

case COMMANDS.EXIT:
return { exit: true };

default:
return { error: ERROR_MESSAGES.INVALID_COMMAND };
}
} catch (error) {
return { error: ERROR_MESSAGES.OPERATION_FAILED };
}
}

function handleExit(username) {
logger.welcome(`\nThank you for using File Manager, ${username}, goodbye!`);
process.exit(0);
}

function main() {
let username;
try {
username = getUsername();
} catch (error) {
logger.error(error.message);
process.exit(1);
}

logger.welcome(`Welcome to the File Manager, ${username}!`);

let currentDir = homedir();
displayCurrentDirectory(currentDir);

const rl = createReadlineInterface();

rl.on('SIGINT', () => {
handleExit(username);
});

rl.on('line', async (input) => {
const { result, error, newDir, exit } = await processCommand(input, currentDir);

if (exit) {
rl.close();
handleExit(username);
return;
}

if (error) {
logger.error(error);
} else if (result !== undefined) {
if (result !== '') {
logger.log(result);
}
}

if (newDir) {
currentDir = newDir;
}

displayCurrentDirectory(currentDir);
});
}

main();
34 changes: 0 additions & 34 deletions src/modules/cjsToEsm.cjs

This file was deleted.

Loading