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
11 changes: 9 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2)
const result = [];

for (let i = 0; i < args.length; i += 2) {
result.push(`${args[i].slice(2)} is ${args[i + 1]}`)
}

console.log(result.join(', '))
};

parseArgs();
parseArgs();
8 changes: 6 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const parseEnv = () => {
// Write your code here
let variablesRSS = Object.entries(process.env)
.filter(([key]) => key.startsWith('RSS_'))
.map(([key, value]) => `${key}=${value}`);

console.log(variablesRSS.join('; '))
};

parseEnv();
parseEnv();
9 changes: 7 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { spawn } from 'node:child_process';
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';

const spawnChildProcess = async (args) => {
// Write your code here
const pathToScript = resolve(dirname(fileURLToPath(import.meta.url)), './files/script.js')
spawn(process.execPath, [pathToScript, ...args], { stdio: 'inherit' });
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess([1, 2, 'test']);
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 { readdir, copyFile, access, mkdir } from 'node:fs/promises';
import path, { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';

const copy = async () => {
// Write your code here
const sourceFolder = resolve(dirname(fileURLToPath(import.meta.url)), './files');
const destinationFolder = resolve(dirname(fileURLToPath(import.meta.url)), './files_copy');
const errorMessage = 'FS operation failed';

try {
const isSourceFolderExist = await access(sourceFolder).then(() => true).catch(() => false);
const isDestinationFolderExist = await access(destinationFolder).then(() => true).catch(() => false);

if (!isSourceFolderExist || isDestinationFolderExist) {
throw new Error(errorMessage);
}
const files = await readdir(sourceFolder);
await mkdir(destinationFolder);

for (const file of files) {
const sourceFilePath = path.join(sourceFolder, file);
const destinationFilePath = path.join(destinationFolder, file);

await copyFile(sourceFilePath, destinationFilePath);
}
} catch (error) {
console.error(error);
}
};

await copy();
22 changes: 20 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { appendFile, access } from 'node:fs/promises';
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';

const create = async () => {
// Write your code here
const filePath = resolve(dirname(fileURLToPath(import.meta.url)), './files/fresh.txt');
const fileContent = 'I am fresh and young';
const errorMessage = 'FS operation failed';

try {
const isFileExist = await access(filePath).then(() => true).catch(() => false);

if (!isFileExist) {
await appendFile(filePath, fileContent);
} else {
throw new Error(errorMessage);
}
} catch (error) {
console.error(error);
}
};

await create();
await create();
21 changes: 19 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import { access, rm } from 'node:fs/promises';
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';

const remove = async () => {
// Write your code here
const fileToRemove = resolve(dirname(fileURLToPath(import.meta.url)), './files/fileToRemove.txt');
const errorMessage = 'FS operation failed';

try {
const isFileToRemoveExist = await access(fileToRemove).then(() => true).catch(() => false);

if (!isFileToRemoveExist) {
throw new Error(errorMessage);
}

await rm(fileToRemove);
} catch (error) {
console.error(error);
}
};

await remove();
await remove();
21 changes: 19 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import { access, readdir } from 'node:fs/promises';
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';

const list = async () => {
// Write your code here
const filesFolder = resolve(dirname(fileURLToPath(import.meta.url)), './files');
const errorMessage = 'FS operation failed';

try {
const isFilesFolderExist = await access(filesFolder).then(() => true).catch(() => false);

if (!isFilesFolderExist) {
throw new Error(errorMessage);
}
const files = await readdir(filesFolder);
console.log(files)
} catch (error) {
console.error(error);
}
};

await list();
await list();
22 changes: 20 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { access, readFile } from 'node:fs/promises';
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';

const read = async () => {
// Write your code here
const fileToRead = resolve(dirname(fileURLToPath(import.meta.url)), './files/fileToRead.txt');
const errorMessage = 'FS operation failed';

try {
const isFileToReadExist = await access(fileToRead).then(() => true).catch(() => false);

if (!isFileToReadExist) {
throw new Error(errorMessage);
}

const fileContent = await readFile(fileToRead, 'utf-8');
console.log(fileContent);
} catch (error) {
console.error(error);
}
};

await read();
await read();
22 changes: 20 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { access, rename as fileRename } from 'node:fs/promises';
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';

const rename = async () => {
// Write your code here
const wrongFilename = resolve(dirname(fileURLToPath(import.meta.url)), './files/wrongFilename.txt');
const properFilename = resolve(dirname(fileURLToPath(import.meta.url)), './files/properFilename.txt');
const errorMessage = 'FS operation failed';

try {
const isWrongFilenameExist = await access(wrongFilename).then(() => true).catch(() => false);
const isProperFilenameExist = await access(properFilename).then(() => true).catch(() => false);

if (!isWrongFilenameExist || isProperFilenameExist) {
throw new Error(errorMessage);
}
await fileRename(wrongFilename, properFilename);
} catch (error) {
console.error(error);
}
};

await rename();
await rename();
16 changes: 14 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import crypto from 'crypto';
import { readFile } from 'node:fs/promises';
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';

const calculateHash = async () => {
// Write your code here
const fileToRead = resolve(dirname(fileURLToPath(import.meta.url)), './files/fileToCalculateHashFor.txt');

try {
const fileContent = await readFile(fileToRead, 'utf-8');
console.log(crypto.createHash('sha256').update(fileContent).digest('hex'));
} catch (error) {
console.error(error);
}
};

await calculateHash();
await calculateHash();
40 changes: 0 additions & 40 deletions src/modules/cjsToEsm.cjs

This file was deleted.

40 changes: 40 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { readFile } from 'node:fs/promises';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url'
import { release, version } from 'os';
import { createServer as createServerHttp } from 'http';
import './files/c.js';

const random = Math.random();

let unknownObject;

if (random > 0.5) {
unknownObject = JSON.parse(await readFile('src/modules/files/a.json', 'utf-8'));
} else {
unknownObject = JSON.parse(await readFile('src/modules/files/b.json', 'utf-8'));
}

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

console.log(`Path to current file is ${import.meta.url}`);
console.log(`Path to current directory is ${dirname(fileURLToPath(import.meta.url))}`);

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 };


18 changes: 16 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { pipeline } from 'node:stream/promises';
import fs from 'node:fs';
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';

const read = async () => {
// Write your code here
const fileToRead = resolve(dirname(fileURLToPath(import.meta.url)), './files/fileToRead.txt');

try {
await pipeline(
fs.createReadStream(fileToRead),
process.stdout
)
} catch (error) {
console.error(error);
}
};

await read();
await read();
21 changes: 19 additions & 2 deletions src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import { pipeline } from 'node:stream/promises';
import { Transform } from 'stream'

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

try {
await pipeline(
process.stdin,
reverse,
process.stdout
)
} catch (error) {
console.error(error);
}
};

await transform();
await transform();
17 changes: 15 additions & 2 deletions src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { pipeline } from 'node:stream/promises';
import fs from 'node:fs';
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';

const write = async () => {
// Write your code here
const fileToWrite = resolve(dirname(fileURLToPath(import.meta.url)), './files/fileToWrite.txt');
try {
await pipeline(
process.stdin,
fs.createWriteStream(fileToWrite),
)
} catch (error) {
console.error(error);
}
};

await write();
await write();
Loading