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
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/node-nodejs-basics.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
const parseArgs = () => {
// Write your code here

const args = process.argv.slice(2);
const result = [];

for (let i = 0, l = args.length; i < l; i+=2) {
const prop = args[i].replace('--', '');
const val = args[i + 1];
result.push(`${prop} is ${val}`);
}
console.log(result.join(', '));
};

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

const prefix = 'RSS_';

const filteredVariables = Object.entries(process.env)
.filter(([key]) => key.startsWith(prefix))
.map(([key, value]) => `${key}=${value}`)
.join('; ');
console.log(filteredVariables);

};

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

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const spawnChildProcess = async (args) => {
// Write your code here

const pathFile = path.join(__dirname, 'files', 'script.js');
const childProcess = spawn('node', [pathFile, ...args], {
stdio: ['pipe', 'pipe', 'inherit'],
});

process.stdin.pipe(childProcess.stdin);

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

childProcess.on('error', (error) => {
console.error(`Error in child process: ${error.message}`);
});

childProcess.on('exit', (code) => {
process.stdin.unpipe(childProcess.stdin);
});

process.stdin.on('data', (data) => {
childProcess.stdin.write(data);
});

process.stdin.on('end', () => {
childProcess.stdin.end();
});

};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess( ['someArgument1', 'someArgument2']);
30 changes: 29 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
import { fileURLToPath } from 'node:url';
import path, { dirname } from 'node:path';
import { readdir, mkdir, copyFile } from 'node:fs/promises';

import { exists } from "../utils/index.js";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const folderPath = path.join(__dirname, 'files');
const destPath = path.join(__dirname, 'files_copy');

const copy = async () => {
// Write your code here

if (!(await exists(folderPath))) throw new Error(`FS operation failed`);
if (await exists(destPath)) throw new Error(`FS operation failed`);

try {
await mkdir(destPath);
const files = await readdir(folderPath);
for (const file of files) {
const srcFilePath = path.join(folderPath, file);
const destFilePath = path.join(destPath, file);

await copyFile(srcFilePath, destFilePath);
}
} catch (err) {
console.error(err);
}

};

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

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

import { exists } from '../utils/index.js';

const filePath = path.join(__dirname, 'files', 'fresh.txt');
const content = 'I am fresh and young';

const create = async () => {
// Write your code here

if (await exists(filePath)) throw new Error(`FS operation failed`);

try {
await fs.writeFile(filePath, content, { encoding: 'utf8' });
} catch (e) {
console.error(e);
}

};

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

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

import { exists } from '../utils/index.js';

const filePath = path.join(__dirname, 'files', 'fileToRemove.txt');

const remove = async () => {
// Write your code here

if (!(await exists(filePath))) throw new Error(`FS operation failed`);

try {
await unlink(filePath);
} catch (e) {
console.error(e);
}

};

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

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

import { exists } from '../utils/index.js';

const folderPath = path.join(__dirname, 'files');

const list = async () => {
// Write your code here

if (!(await exists(folderPath))) throw new Error(`FS operation failed`);

try {
const files = await readdir(folderPath);
console.dir(files);
} catch (e) {
console.error(e);
}

};

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

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

import { exists } from '../utils/index.js';

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

const read = async () => {
// Write your code here

if (!(await exists(filePath))) throw new Error(`FS operation failed`);

try {
const contents = await fs.readFile(filePath, { encoding: 'utf8' });
console.log(contents);
} catch (e) {
console.error(e)
}

};

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

import { exists } from "../utils/index.js";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const oldFilePath = path.join(__dirname, 'files', 'wrongFilename.txt');
const newFilePath = path.join(__dirname, 'files', 'properFilename.md');

const rename = async () => {
// Write your code here

if (!(await exists(oldFilePath))) throw new Error(`FS operation failed`);
if (await exists(newFilePath)) throw new Error(`FS operation failed`);

try {
await fsRename(oldFilePath, newFilePath);
} catch (err) {
console.error(err.message);
}

};

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

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const filePath = path.join(__dirname, 'files', 'fileToCalculateHashFor.txt');

const calculateHash = async () => {
// Write your code here

const hash = createHash('sha256');
const readStream = createReadStream(filePath);

readStream.on('data', (chunk) => {
hash.update(chunk);
})

readStream.on('end', () => {
console.log(hash.digest('hex'));
})

};

await calculateHash();
20 changes: 11 additions & 9 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
const path = require('path');
const { release, version } = require('os');
const { createServer: createServerHttp } = require('http');
require('./files/c');
import path from 'node:path';
import { release, version } from 'node:os';
import { createServer as createServerHttp } from 'node:http';
import { createRequire } from "node:module";

import './files/c.js';

const random = Math.random();

let unknownObject;

if (random > 0.5) {
unknownObject = require('./files/a.json');
unknownObject = createRequire(import.meta.url)('./files/a.json');
} else {
unknownObject = require('./files/b.json');
unknownObject = createRequire(import.meta.url)('./files/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}`);
console.log(`Path to current file is ${import.meta.url}`);
console.log(`Path to current directory is ${new URL('.', import.meta.url).pathname}`);

const myServer = createServerHttp((_, res) => {
res.end('Request accepted');
Expand All @@ -33,7 +35,7 @@ myServer.listen(PORT, () => {
console.log('To terminate it, use Ctrl+C combination');
});

module.exports = {
export {
unknownObject,
myServer,
};
Expand Down
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 { createReadStream } from 'node:fs';
import { pipeline } from 'node:stream/promises';
import { fileURLToPath } from 'node:url';
import path, { dirname } from 'node:path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

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

const read = async () => {
// Write your code here

await pipeline(
createReadStream(filePath),
process.stdout,
)

};

await read();
Loading