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

Assignment: File Manager #1

Open
wants to merge 7 commits into
base: master
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
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## File Manager

Node.js APIs.
Use Node.js 20 LTS version.
Use Node.js 20 LTS version.
https://github.com/AlreadyBored/nodejs-assignments/blob/main/assignments/file-manager/assignment.md
42 changes: 42 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { createInterface } from 'readline'
import { chdir } from 'process'

import {
getUsernameFromArgs,
getHomeDir,
printCurrentWorkingDirectory,
exitFileManager,
} from './src/utils/index.js'
import processCommand from './src/commands/processCommand.js'

const username = getUsernameFromArgs(process.argv)

chdir(getHomeDir())
console.log(`Welcome to the File Manager, ${username}!`)
printCurrentWorkingDirectory()

const rl = createInterface({
input: process.stdin,
output: process.stdout,
})

rl.on('line', (line) => {
const matches = line.match(/(?:[^\s"']+|"[^"]*"|'[^']*')+/g)
const command = matches[0]

let payload = matches
.slice(1)
.map((arg) => arg.replace(/^"|"$/g, '').replace(/^'|'$/g, ''))

if (command === 'exit') {
exitFileManager(username)
rl.close()
return
}

processCommand({ command, payload })
})
.on('SIGINT', () => rl.close())
.on('close', () => {
exitFileManager(username)
})
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "file-manager",
"version": "1.0.0",
"description": "Node.js APIs. Use Node.js 20 LTS version. https://github.com/AlreadyBored/nodejs-assignments/blob/main/assignments/file-manager/assignment.md",
"main": "cli.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node cli.js"
},
"type": "module",
"author": "",
"license": "ISC"
}
17 changes: 17 additions & 0 deletions src/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export { default as processCommandCd } from './processCommandCd.js'
export { default as processCommandUp } from './processCommandUp.js'
export { default as processCommandLs } from './processCommandLs.js'
export { default as processCommandAdd } from './processCommandAdd.js'
export { default as processCommandRn } from './processCommandRn.js'
export { default as processCommandCp } from './processCommandCp.js'
export { default as processCommandMv } from './processCommandMv.js'
export { default as processCommandRm } from './processCommandRm.js'
export { default as processCommandCat } from './processCommandCat.js'
export { default as processCommandHash } from './processCommandHash.js'
export { default as processCommandCompress } from './processCommandCompress.js'
export { default as processCommandDecompress } from './processCommandDecompress.js'
export { default as processCommandOsEol } from './processCommandOsEol.js'
export { default as processCommandOsCpus } from './processCommandOsCpus.js'
export { default as processCommandOsHomeDir } from './processCommandOsHomeDir.js'
export { default as processCommandOsUsername } from './processCommandOsUsername.js'
export { default as processCommandOsArchitecture } from './processCommandOsArchitecture.js'
122 changes: 122 additions & 0 deletions src/commands/processCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { INVALID_INPUT, COMMAND_NO_PARAMETERS, COMMANDS } from '../constants.js'
import wrapperProcessCommand from './wrapperProcessCommand.js'
import {
processCommandAdd,
processCommandRn,
processCommandCp,
processCommandMv,
processCommandRm,
processCommandCat,
processCommandUp,
processCommandCd,
processCommandLs,
processCommandHash,
processCommandCompress,
processCommandDecompress,
processCommandOsEol,
processCommandOsCpus,
processCommandOsHomeDir,
processCommandOsUsername,
processCommandOsArchitecture,
} from './index.js'

const {
UP,
CD,
LS,
CAT,
ADD,
RN,
RM,
CP,
MV,
HASH,
COMPRESS,
DECOMPRESS,
OS,
EOL,
CPUS,
HOMEDIR,
USERNAME,
ARCHITECTURE,
} = COMMANDS

const processCommand = async (data) => {
const { command, payload } = data

switch (command) {
case UP:
if (payload.length > 0) {
console.log(COMMAND_NO_PARAMETERS)
break
}
wrapperProcessCommand(processCommandUp, payload)
break
case CD:
wrapperProcessCommand(processCommandCd, payload)
break
case LS:
if (payload.length > 0) {
console.log(COMMAND_NO_PARAMETERS)
break
}
await wrapperProcessCommand(processCommandLs, payload)
break
case CAT:
await wrapperProcessCommand(processCommandCat, payload)
break
case ADD:
await wrapperProcessCommand(processCommandAdd, payload)
break
case RN:
await wrapperProcessCommand(processCommandRn, payload)
break
case CP:
await wrapperProcessCommand(processCommandCp, payload)
break
case MV:
await wrapperProcessCommand(processCommandMv, payload)
break
case RM:
await wrapperProcessCommand(processCommandRm, payload)
break
case HASH:
await wrapperProcessCommand(processCommandHash, payload)
break
case COMPRESS:
await wrapperProcessCommand(processCommandCompress, payload)
break
case DECOMPRESS:
await wrapperProcessCommand(processCommandDecompress, payload)
break
case OS:
if (payload.length === 0) {
console.log(INVALID_INPUT)
break
}
switch (payload[0].toLowerCase()) {
case EOL:
wrapperProcessCommand(processCommandOsEol)
break
case CPUS:
wrapperProcessCommand(processCommandOsCpus)
break
case HOMEDIR:
wrapperProcessCommand(processCommandOsHomeDir)
break
case USERNAME:
wrapperProcessCommand(processCommandOsUsername)
break
case ARCHITECTURE:
wrapperProcessCommand(processCommandOsArchitecture)
break
default:
break
}
break
default:
console.log(INVALID_INPUT)
}
}

export default processCommand
14 changes: 14 additions & 0 deletions src/commands/processCommandAdd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { writeFile } from 'fs/promises'

import { getFilePath } from '../utils/index.js'


const processCommandAdd = async (params) => {
const [filename] = params
const filePath = getFilePath(filename)

await writeFile(filePath, '')
console.log(`Created empty file: ${filename}`)
}

export default processCommandAdd
24 changes: 24 additions & 0 deletions src/commands/processCommandCat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createReadStream } from 'fs'

import { getFilePath } from '../utils/index.js'

const processCommandCat = async (params) => {
const [filename] = params
const filePath = getFilePath(filename)

const readStream = createReadStream(filePath, { encoding: 'utf8' })

readStream.on('data', (chunk) => {
console.log(chunk)
})

readStream.on('end', () => {
console.log(`Finished reading file: ${filePath}`)
})

readStream.on('error', () => {
console.error('Error reading file!')
})
}

export default processCommandCat
8 changes: 8 additions & 0 deletions src/commands/processCommandCd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { chdir } from 'process'

const processCommandCd = (params) => {
const destPath = params[0]
chdir(destPath)
}

export default processCommandCd
9 changes: 9 additions & 0 deletions src/commands/processCommandCompress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import processCompression from './processCompression.js'

const processCommandCompress = async (params) => {
const [sourcePath, destinationPath] = params

await processCompression(sourcePath, destinationPath, true)
}

export default processCommandCompress
19 changes: 19 additions & 0 deletions src/commands/processCommandCp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createReadStream, createWriteStream } from 'fs'
import { pipeline } from 'stream/promises'
import { basename, join } from 'path'

const processCommandCp = async (params) => {
const [sourcePath, destinationPath] = params
const sourceFile = basename(sourcePath)
const destinationFilePath = join(destinationPath, sourceFile)

const sourceStream = createReadStream(sourcePath)
const destinationStream = createWriteStream(destinationFilePath)

await pipeline(sourceStream, destinationStream)
console.log(
`File ${sourceFile} copied successfully to: ${destinationFilePath}`
)
}

export default processCommandCp
9 changes: 9 additions & 0 deletions src/commands/processCommandDecompress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import processCompression from './processCompression.js'

const processCommandDecompress = async (params) => {
const [sourcePath, destinationPath] = params

await processCompression(sourcePath, destinationPath, false)
}

export default processCommandDecompress
25 changes: 25 additions & 0 deletions src/commands/processCommandHash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { getFilePath } from '../utils/index.js'
import { createReadStream } from 'fs'
import crypto from 'crypto'

import { HASH } from '../constants.js'

const processCommandHash = async (params) => {
const [filename] = params
const filePath = getFilePath(filename)

const stream = createReadStream(filePath)

const hash = crypto.createHash(HASH)

stream.pipe(hash).on('finish', () => {
console.log(hash.digest('hex'))
})

stream.on('error', () => {
console.error('Error reading file!')
})

}

export default processCommandHash
24 changes: 24 additions & 0 deletions src/commands/processCommandLs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { readdir } from 'fs/promises'
import { cwd } from 'process'

const processCommandLs = async (params) => {

const currentDirectory = cwd()

const items = await readdir(currentDirectory, { withFileTypes: true })
const sortedItems = items
.map((item) => ({
Name: item.name,
Type: item.isDirectory() ? 'directory' : 'file',
}))
.sort((a, b) => {
if (a.Type === b.Type) {
return a.Name.localeCompare(b.Name)
}
return a.Type === 'directory' ? -1 : 1
})

console.table(sortedItems)
}

export default processCommandLs
20 changes: 20 additions & 0 deletions src/commands/processCommandMv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createReadStream, createWriteStream } from 'fs'
import { unlink } from 'fs/promises'
import { pipeline } from 'stream/promises'
import { basename, join } from 'path'

const processCommandMv = async (params) => {
const [sourcePath, destinationPath] = params
const sourceFile = basename(sourcePath)
const destinationFilePath = join(destinationPath, sourceFile)

const sourceStream = createReadStream(sourcePath)
const destinationStream = createWriteStream(destinationFilePath)

await pipeline(sourceStream, destinationStream)

await unlink(sourcePath)
console.log(`Moved file ${sourceFile} to ${destinationFilePath}`)
}

export default processCommandMv
7 changes: 7 additions & 0 deletions src/commands/processCommandOsArchitecture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os from 'os'

const processCommandOsArchitecture = () => {
console.log(os.arch())
}

export default processCommandOsArchitecture
15 changes: 15 additions & 0 deletions src/commands/processCommandOsCpus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import os from 'os'

const processCommandOsCpus = () => {
const cpus = os.cpus()
console.log(`Amount of CPUs: ${cpus.length}`)
const info = cpus.map(
(cpu, index) =>
`CPU ${index + 1}: Model - ${cpu.model}, Clock Rate - ${(
cpu.speed / 1000
).toFixed(2)} GHz`
)
console.table(info)
}

export default processCommandOsCpus
Loading