Skip to content
15 changes: 14 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
const parseArgs = () => {
// Write your code here
const resArray = [];
const args = process.argv.slice(2);

for (let i = 0; i < args.length; i++) {
if (args[i]?.startsWith('--')) {
if (args[i + 1]?.startsWith('--')) {
resArray.push(`${args[i]} undefined`);
continue;
}
resArray.push(`${args[i]} ${args[i + 1]}`);
}
}

return resArray.join(' ');
};

parseArgs();
9 changes: 8 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const parseEnv = () => {
// Write your code here
const resArray = [];

for (const key in process.env) {
if (key.startsWith('RSS_')) {
resArray.push(`${key}=${process.env[key]}`);
}
}
return resArray.join('; ');
};

parseEnv();
31 changes: 30 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
import { readdir, copyFile, mkdir } from 'node:fs/promises';
import { join } from 'path';

const CONFIG = {
dirName: 'files',
copyDirName: 'files_copy',
errorMessage: 'FS operation failed',
};

const __dirname = import.meta.dirname;
const targetDirPath = join(__dirname, CONFIG.dirName);
const copyDirPath = join(__dirname, CONFIG.copyDirName);

const copy = async () => {
// Write your code here
try {
await mkdir(copyDirPath);
} catch (error) {
throw new Error(CONFIG.errorMessage);
}

try {
const files = await readdir(targetDirPath);

for (const file of files) {
const targetFilePath = join(targetDirPath, file);
const copyFilePath = join(copyDirPath, file);
await copyFile(targetFilePath, copyFilePath);
}
} catch (err) {
throw new Error(CONFIG.errorMessage);
}
};

await copy();
23 changes: 22 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import { writeFile } from 'node:fs/promises';
import { join } from 'path';

const CONFIG = {
file: 'fresh.txt',
dirName: 'files',
content: 'I am fresh and young',
errorMessage: 'FS operation failed',
};

const __dirname = import.meta.dirname;
const targetDirPath = join(__dirname, CONFIG.dirName);
const targetFilePath = join(targetDirPath, CONFIG.file);

const create = async () => {
// Write your code here
try {
await writeFile(targetFilePath, CONFIG.content, {
encoding: 'utf8',
flag: 'wx',
});
} catch (err) {
throw new Error(CONFIG.errorMessage);
}
};

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

const CONFIG = {
file: 'fileToRemove.txt',
dirName: 'files',
errorMessage: 'FS operation failed',
};

const __dirname = import.meta.dirname;
const targetDirPath = join(__dirname, CONFIG.dirName);
const targetFilePath = join(targetDirPath, CONFIG.file);

const remove = async () => {
// Write your code here
try {
await unlink(targetFilePath);
console.log(`successfully deleted ${CONFIG.file}`);
} catch (error) {
throw new Error(CONFIG.errorMessage);
}
};

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 { readdir } from 'node:fs/promises';
import { join } from 'path';

const CONFIG = {
dirName: 'files',
errorMessage: 'FS operation failed',
};

const __dirname = import.meta.dirname;
const targetDirPath = join(__dirname, CONFIG.dirName);

const list = async () => {
// Write your code here
try {
const files = await readdir(targetDirPath);
console.log(files);
} catch (err) {
throw new Error(CONFIG.errorMessage);
}
};

await list();
20 changes: 19 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { readFile } from 'node:fs/promises';
import { join } from 'path';

const CONFIG = {
file: 'fileToRead.txt',
dirName: 'files',
errorMessage: 'FS operation failed',
};

const __dirname = import.meta.dirname;
const targetDirPath = join(__dirname, CONFIG.dirName);
const targetFilePath = join(targetDirPath, CONFIG.file);

const read = async () => {
// Write your code here
try {
const content = await readFile(targetFilePath, { encoding: 'utf8' });
console.log(content);
} catch (error) {
throw new Error(CONFIG.errorMessage);
}
};

await read();
24 changes: 23 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import { rename as renameFile } from 'node:fs/promises';
import { join } from 'path';

const CONFIG = {
file: 'wrongFilename.txt',
newFile: 'properFilename.txt',
dirName: 'files',
errorMessage: 'FS operation failed',
};

const __dirname = import.meta.dirname;
const targetDirPath = join(__dirname, CONFIG.dirName);
const targetOldFilePath = join(targetDirPath, CONFIG.file);
const targetNewFilePath = join(targetDirPath, CONFIG.newFile);

const rename = async () => {
// Write your code here
try {
await renameFile(targetOldFilePath, targetNewFilePath);
console.log(
`successfully renamed from ${CONFIG.file} to ${CONFIG.newFile}`
);
} catch (error) {
throw new Error(CONFIG.errorMessage);
}
};

await rename();
22 changes: 21 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import { createHash } from 'node:crypto';
import { createReadStream } from 'node:fs';
import { stdout } from 'node:process';
import { join } from 'node:path';

const CONFIG = {
file: 'fileToCalculateHashFor.txt',
dirName: 'files',
};

const __dirname = import.meta.dirname;
const targetFilePath = join(__dirname, CONFIG.dirName, CONFIG.file);

const calculateHash = async () => {
// Write your code here
const hash = createHash('sha256');

const input = createReadStream(targetFilePath);
input.pipe(hash);
input.on('end', () => {
const finalHash = hash.digest('hex');
console.log(finalHash);
});
};

await calculateHash();
47 changes: 47 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import path from 'node:path';
import { release, version } from 'node:os';
import { createServer as createServerHttp } from 'node:http';
import { join } from 'path';
import { readFileSync } from 'node:fs';

import './files/c.cjs';

const __dirname = import.meta.dirname;
const __filename = import.meta.filename;

const random = Math.random();

const getFileContent = (filePath) => {
try {
const fullPath = join(__dirname, 'files', filePath);
const fileContent = readFileSync(fullPath);
return JSON.parse(fileContent);
} catch (err) {
console.log(err);
}
};

const unknownObject =
random > 0.5 ? getFileContent('a.json') : getFileContent('b.json');

console.log(`Release ${release()}`);
console.log(`Version ${version()}`);
console.log(`Path segment separator is "${path.sep}"`);

console.log(`Path to current file is ${__filename}`);
console.log(`Path to current directory is ${__dirname}`);

const myServer = createServerHttp((_, res) => {
res.end('Request accepted');
});

const PORT = 3000;

console.log(unknownObject);

myServer.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
console.log('To terminate it, use Ctrl+C combination');
});

export { unknownObject, myServer };
23 changes: 22 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import { createReadStream } from 'node:fs';
import { stdout } from 'node:process';
import { join } from 'node:path';

const CONFIG = {
file: 'fileToRead.txt',
dirName: 'files',
};

const __dirname = import.meta.dirname;
const targetFilePath = join(__dirname, CONFIG.dirName, CONFIG.file);

const read = async () => {
// Write your code here
const input = createReadStream(targetFilePath);

input.on('error', (err) => {
console.error(err);
});
input.pipe(stdout, { end: false });
input.on('end', () => {
stdout.write('\n');
stdout.end();
});
};

await read();
12 changes: 11 additions & 1 deletion src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { Transform } from 'node:stream';
import { stdout, stdin } from 'node:process';

const transform = async () => {
// Write your code here
const transformStream = new Transform({
transform(chunk, encoding, callback) {
const upperCaseData = chunk.toString().split('').reverse().join('');
callback(null, upperCaseData);
},
});

stdin.pipe(transformStream).pipe(stdout);
};

await transform();
20 changes: 19 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { createReadStream, createWriteStream } from 'node:fs';
import { stdin } from 'node:process';
import { join } from 'node:path';

const CONFIG = {
file: 'fileToWrite.txt',
dirName: 'files',
};

const __dirname = import.meta.dirname;
const targetFilePath = join(__dirname, CONFIG.dirName, CONFIG.file);

const write = async () => {
// Write your code here
const input = createWriteStream(targetFilePath);

input.on('error', (err) => {
console.error(err);
});

stdin.pipe(input);
};

await write();
26 changes: 25 additions & 1 deletion src/zip/compress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
import { createReadStream, createWriteStream } from 'node:fs';
import { join } from 'node:path';
import { createGzip } from 'node:zlib';
import { pipeline } from 'node:stream/promises';

const CONFIG = {
fileFrom: 'fileToCompress.txt',
fileTo: 'archive.gz',
dirName: 'files',
};

const __dirname = import.meta.dirname;
const targetFileFromPath = join(__dirname, CONFIG.dirName, CONFIG.fileFrom);
const targetFileToPath = join(__dirname, CONFIG.dirName, CONFIG.fileTo);

const compress = async () => {
// Write your code here
const gzip = createGzip();
const sourceReadStream = createReadStream(targetFileFromPath);
const destinationWriteStream = createWriteStream(targetFileToPath);

try {
await pipeline(sourceReadStream, gzip, destinationWriteStream);
} catch (err) {
console.error(err);
process.exitCode = 1;
}
};

await compress();
Loading