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
8 changes: 7 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const parseArgs = () => {
// Write your code here
const args = [];
process.argv.forEach((arg, idx, arr) => {
if (arg.startsWith('--')) {
args.push(`${ arg.slice(2) } is ${ arr[idx + 1] }`);
}
});
console.log(args.join(', '));
};

parseArgs();
17 changes: 16 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
const findEnvVar = (varKey) => {
let entries = [];
for (const key in process.env) {
if (key.startsWith(varKey)) {
entries.push(`${ key }=${ process.env[key] }`);
}
}
return entries.join('; ');
};

const parseEnv = () => {
// Write your code here
try {
const parsedEnv = findEnvVar('RSS_');
console.log(parsedEnv);
} catch (e) {
console.log(e);
}
};

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

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const childProcessPath = resolve(__dirname, 'files', 'script.js');

const spawnChildProcess = async (args) => {
// Write your code here
const childProcess = fork(childProcessPath, args, { silent: true });
process.stdin.pipe(childProcess.stdin);
childProcess.stdout.pipe(process.stdout);
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
await spawnChildProcess([ 36, 'bar', 55, 'foo', 'baz' ]);
29 changes: 28 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
import fs from 'node:fs/promises';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { FS_OPERATION_FAILED } from './utils/error.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const srcDir = resolve(__dirname, 'files');
const targetDir = resolve(__dirname, 'files_copy');

const copyDir = async (srcPath, destPath) => {
const srcFiles = await fs.readdir(srcPath, { withFileTypes: true });
await fs.mkdir(destPath, { recursive: false });
return Promise.all(
srcFiles.map(async (file) => file.isFile()
? await fs.copyFile(resolve(file.path, file.name), resolve(destPath, file.name))
: await copyDir(resolve(file.path, file.name), resolve(destPath, file.name))
)
);
};

const copy = async () => {
// Write your code here
try {
await copyDir(srcDir, targetDir);
} catch (e) {
if (e.code === 'EEXIST' || e.code === 'ENOENT') {
throw new Error(FS_OPERATION_FAILED);
}
}
};

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

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const file = resolve(__dirname, 'files', 'fresh.txt');
const content = 'I am fresh and young';

const create = async () => {
// Write your code here
try {
await fs.writeFile(file, content, { flag: 'wx' });
} catch (e) {
if (e.code === 'EEXIST') {
throw new Error(FS_OPERATION_FAILED);
}
}
};

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

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const file = resolve(__dirname, 'files', 'fileToRemove.txt');

const remove = async () => {
// Write your code here
try {
await fs.rm(file);
} catch (e) {
if (e.code === 'ENOENT') {
throw new Error(FS_OPERATION_FAILED);
}
}
};

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 from 'node:fs/promises';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { FS_OPERATION_FAILED } from './utils/error.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const srcDir = resolve(__dirname, 'files');

const list = async () => {
// Write your code here
try {
const files = await fs.readdir(srcDir);
console.log(files);
} catch (e) {
if (e.code === 'ENOENT') {
throw new Error(FS_OPERATION_FAILED);
}
}
};

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 from 'node:fs/promises';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { FS_OPERATION_FAILED } from './utils/error.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const readFile = resolve(__dirname, 'files', 'fileToRead.txt');

const read = async () => {
// Write your code here
try {
const data = await fs.readFile(readFile, { encoding: 'utf8' });
console.log(data);
} catch (e) {
if (e.code === 'ENOENT') {
throw new Error(FS_OPERATION_FAILED);
}
}
};

await read();
25 changes: 24 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import fs from 'node:fs/promises';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { FS_OPERATION_FAILED } from './utils/error.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const srcFile = resolve(__dirname, 'files', 'wrongFilename.txt');
const targetFile = resolve(__dirname, 'files', 'properFilename.md');

const rename = async () => {
// Write your code here
try {
const isTargetExist = !!(await fs.stat(targetFile).catch(() => false));
if (isTargetExist) {
throw new Error(FS_OPERATION_FAILED);
} else {
await fs.rename(srcFile, targetFile);
}
} catch (e) {

console.log(e);
if (e.code === 'ENOENT') {
throw new Error(FS_OPERATION_FAILED);
}
}
};

await rename();
1 change: 1 addition & 0 deletions src/fs/utils/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const FS_OPERATION_FAILED = 'FS operation failed';
18 changes: 17 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { createReadStream } from 'node:fs';
import { createHash } from 'node:crypto';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const readFile = resolve(__dirname, 'files', 'fileToCalculateHashFor.txt');

const calculateHash = async () => {
// Write your code here
const fileStream = createReadStream(readFile, { encoding: 'utf8' });
fileStream.on('data', (data) => {
const hash = createHash('sha256').update(data).digest('hex');
console.log(hash);
});
fileStream.on('error', (err) => {
console.log(err);
});
};

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

This file was deleted.

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

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

const random = Math.random();

let unknownObject;

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

console.log(`Release ${ release() }`);
console.log(`Version ${ version() }`);
console.log(`Path segment separator is "${ 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 };


1 change: 1 addition & 0 deletions src/streams/files/fileToWrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

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 { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { createReadStream } from 'node:fs';
import { pipeline } from 'node:stream/promises';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const readFile = resolve(__dirname, 'files', 'fileToRead.txt');


const read = async () => {
// Write your code here
try {
const fileStream = createReadStream(readFile);
await pipeline(fileStream, process.stdout);
} catch (e) {
console.log(e);
}
};

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

const transformReverse = () => {
return new Transform({
transform(chunk, encoding, callback) {
const reversed = `${ chunk }`.trim().split('').reverse().join('');
callback(null, reversed);
}
});
};

const transform = async () => {
// Write your code here
try {
await pipeline(process.stdin, transformReverse(), process.stdout);
} catch (e) {
console.log(e);
}
};

await transform();
Loading