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: 17 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
const executionPathArgumentIndex = 0
const filePathArgumentIndex = 1

const parseArgs = () => {
// Write your code here
process.argv.forEach((_, argIndex) => {
const argName = process.argv[argIndex]
const argValue = process.argv[argIndex + 1]

if (argIndex === executionPathArgumentIndex || argIndex === filePathArgumentIndex) {
return
}

if (argIndex % 2 !== 0) {
return
}

process.stdout.write(`${argName.slice(2, argName.length)} is ${argValue}, `)
})
};

parseArgs();
4 changes: 3 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const parseEnv = () => {
// Write your code here
Object.keys(process.env).forEach(envKey => {
console.log(`RSS_${envKey}=${process.env[envKey]}`)
})
};

parseEnv();
28 changes: 27 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
import fs, { promises as fsPromises } from 'fs'
import path from 'path'

const __dirname = path.resolve()

const taskFolderToCopyPath = path.join(__dirname, 'src', 'fs', 'files')
const taskCopiedFolderPath = path.join(__dirname, 'src', 'fs', 'files_copy')
const taskErrorText = 'FS operation failed'

const copy = async () => {
// Write your code here
const copiedFolderExistenceCheck = fs.existsSync(taskCopiedFolderPath)
const folderToCopyExistenceCheck = fs.existsSync(taskFolderToCopyPath)

if (!folderToCopyExistenceCheck) {
throw new Error(taskErrorText)
}

if (copiedFolderExistenceCheck) {
throw new Error(taskErrorText)
}

await fsPromises.mkdir(taskCopiedFolderPath)

const filesToCopyList = await fsPromises.readdir(taskFolderToCopyPath)

for (const fileToCopy of filesToCopyList) {
await fsPromises.copyFile(`${taskFolderToCopyPath}/${fileToCopy}`, `${taskCopiedFolderPath}/${fileToCopy}`)
}
};

await copy();
21 changes: 20 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import fs, { promises as fsPromises } from 'fs'
import path from 'path'

const __dirname = path.resolve()

const taskFilePath = path.join(__dirname, 'src', 'fs', 'files', 'fresh.txt')
const taskFileText = 'I am fresh and young'
const taskErrorText = 'FS operation failed'

const create = async () => {
// Write your code here
const fileExistenceCheck = fs.existsSync(taskFilePath)

if (fileExistenceCheck) {
throw new Error(taskErrorText)
}

try {
await fsPromises.writeFile(taskFilePath, taskFileText)
} catch (err) {
throw new Error(err)
}
};

await create();
16 changes: 15 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import fs, { promises as fsPromises } from 'fs'
import path from 'path'

const __dirname = path.resolve()

const fileToDelete = path.join(__dirname, 'src', 'fs', 'files', 'fileToRemove.txt')
const taskErrorText = 'FS operation failed'

const remove = async () => {
// Write your code here
const fileExistenceCheck = fs.existsSync(fileToDelete)

if (!fileExistenceCheck) {
throw new Error(taskErrorText)
}

fs.unlinkSync(fileToDelete)
};

await remove();
18 changes: 17 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import fs, { promises as fsPromises } from 'fs'
import path from 'path'

const __dirname = path.resolve()

const taskFolder = path.join(__dirname, 'src', 'fs', 'files')
const taskErrorText = 'FS operation failed'

const list = async () => {
// Write your code here
const taskFolderExistenceCheck = fs.existsSync(taskFolder)

if (!taskFolderExistenceCheck) {
throw new Error(taskErrorText)
}

const filesArray = await fsPromises.readdir(taskFolder)

console.log(filesArray)
};

await list();
18 changes: 17 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import fs, { promises as fsPromises } from 'fs'
import path from 'path'

const __dirname = path.resolve()

const fileToRead = path.join(__dirname, 'src', 'fs', 'files', 'fileToRead.txt')
const taskErrorText = 'FS operation failed'

const read = async () => {
// Write your code here
const fileExistenceCheck = fs.existsSync(fileToRead)

if (!fileExistenceCheck) {
throw new Error(taskErrorText)
}

const fileContents = await fsPromises.readFile(fileToRead, 'utf-8')

console.log(fileContents)
};

await read();
22 changes: 21 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import fs, { promises as fsPromises } from 'fs'
import path from 'path'

const __dirname = path.resolve()

const initialFile = path.join(__dirname, 'src', 'fs', 'files', 'wrongFilename.txt')
const newFile = path.join(__dirname, 'src', 'fs', 'files', 'properFilename.md')
const taskErrorText = 'FS operation failed'

const rename = async () => {
// Write your code here
const initialFileExistenceCheck = fs.existsSync(initialFile)
const newFileExistenceCheck = fs.existsSync(newFile)

if (!initialFileExistenceCheck) {
throw new Error(taskErrorText)
}

if (newFileExistenceCheck) {
throw new Error(taskErrorText)
}

fs.renameSync(initialFile, newFile)
};

await rename();
20 changes: 19 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import fs from 'fs'
import path from 'path'
import crypto from 'crypto'

const __dirname = path.resolve()
const filePath = path.join(__dirname, 'src', 'hash', 'files', 'fileToCalculateHashFor.txt')

const calculateHash = async () => {
// Write your code here
const readStream = fs.createReadStream(filePath)
const hash = crypto.createHash('sha256')

hash.setEncoding('hex')

readStream.on('end', () => {
hash.end()

console.log(hash.read())
})

readStream.pipe(hash)
};

await calculateHash();
22 changes: 14 additions & 8 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
const path = require('path');
const { release, version } = require('os');
const { createServer: createServerHttp } = require('http');
require('./files/c');
import path from 'path'
import { release, version } from 'os'
import { createServer as createServerHttp } from 'http'
import { fileURLToPath } from 'node:url'

import './files/c.js'
import aJSON from './files/a.json' assert { type: 'json' }
import bJSON from './files/b.json' assert { type: 'json' }

const __dirname = path.resolve()
const __filename = fileURLToPath(import.meta.url)
const random = Math.random();

let unknownObject;

if (random > 0.5) {
unknownObject = require('./files/a.json');
unknownObject = aJSON;
} else {
unknownObject = require('./files/b.json');
unknownObject = bJSON;
}

console.log(`Release ${release()}`);
Expand All @@ -33,8 +39,8 @@ myServer.listen(PORT, () => {
console.log('To terminate it, use Ctrl+C combination');
});

module.exports = {
export default {
unknownObject,
myServer,
};
}

17 changes: 16 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import fs from 'fs'
import path from 'path'

const __dirname = path.resolve()

const filePath = path.join(__dirname, 'src', 'streams', 'files', 'fileToRead.txt')

const read = async () => {
// Write your code here
const readableStream = fs.createReadStream(filePath)

readableStream.on('error', err => {
throw new Error(err)
})

readableStream.on('data', chunck => {
process.stdout.write(chunck)
})
};

await read();
28 changes: 27 additions & 1 deletion src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
import readline from 'readline'
import { Transform } from 'stream'

const transform = async () => {
// Write your code here
const reverseTransformStream = new Transform({
transform(chunk, encoding, callback) {
callback(null, chunk.reverse())
}
})


reverseTransformStream.pipe(process.stdout)

const readlineInterface = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: "Enter text to reverse: "
})

readlineInterface.prompt()

readlineInterface.on('line', line => {
reverseTransformStream.write(line)
readlineInterface.close()
}).on('close', () => {
reverseTransformStream.end()
process.exit()
})
};

await transform();
30 changes: 29 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
import fs from 'fs'
import path from 'path'
import readline from 'readline'

const __dirname = path.resolve()

const filePath = path.join(__dirname, 'src', 'streams', 'files', 'fileToWrite.txt')

const write = async () => {
// Write your code here
const writableStream = fs.createWriteStream(filePath)

writableStream.on('error', err => {
throw new Error(err)
})

const readlineInterface = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'Enter text: '
})

readlineInterface.prompt()

readlineInterface.on('line', line => {
writableStream.write(line)
readlineInterface.close()
}).on('close', () => {
writableStream.end()
process.exit()
})
};

await write();