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

const result = [];
for (let i = 0; i < raw.length; i += 2) {
const key = raw[i].replace(/^--/, '');
const value = raw[i + 1];

result.push(`${key} is ${value}`);
}

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

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

console.log(envVars);
};

parseEnv();
parseEnv();
28 changes: 24 additions & 4 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
const spawnChildProcess = async (args) => {
// Write your code here
import { spawn } from 'child_process';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

const DIRNAME = dirname(fileURLToPath(import.meta.url));

export const spawnChildProcess = async (args) => {
const scriptPath = join(DIRNAME, 'files', 'script.js');

const child = spawn('node', [scriptPath, ...args], { stdio: ['pipe', 'pipe', process.stderr] });

process.stdin.pipe(child.stdin);
child.stdout.pipe(process.stdout);

child.on('exit', (code) => {
console.log(`Child exited with code ${code}`);
process.exit(code);
});

child.on('error', (err) => {
console.error('Failed to start child process:', err);
process.exit(1);
});
};

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

const DIRNAME = dirname(fileURLToPath(import.meta.url));

const copy = async () => {
// Write your code here
const filesPath = join(DIRNAME, 'files');
const filesCopyPath = join(DIRNAME, 'files_copy');

try {
await fs.access(filesPath);

try {
await fs.access(filesCopyPath);
throw new Error('FS operation failed');
} catch (err) {
if (err.code !== 'ENOENT') {
throw new Error('FS operation failed');
}
}

await fs.cp(filesPath, filesCopyPath, {recursive: true});
} catch (err) {
throw new Error('FS operation failed');
}
};

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

const DIRNAME = dirname(fileURLToPath(import.meta.url));

const create = async () => {
// Write your code here
const freshPath = join(DIRNAME, 'files', 'fresh.txt');

try {
await fs.access(freshPath);
throw new Error('FS operation failed');
} catch (err) {
if (err.code === 'ENOENT') {
await fs.writeFile(freshPath, 'I am fresh and young');
} else {
throw new Error('FS operation failed');
}
}
};

await create();
await create();
17 changes: 15 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { promises as fs } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

const DIRNAME = dirname(fileURLToPath(import.meta.url));

const remove = async () => {
// Write your code here
const fileToRemovePath = join(DIRNAME, 'files', 'fileToRemove.txt');

try {
await fs.access(fileToRemovePath);
await fs.unlink(fileToRemovePath);
} catch (err) {
throw new Error('FS operation failed');
}
};

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

const DIRNAME = dirname(fileURLToPath(import.meta.url));

const list = async () => {
// Write your code here
const filesPath = join(DIRNAME, 'files');

try {
await fs.access(filesPath);
const files = await fs.readdir(filesPath);

console.log(files);
} catch (err) {
throw new Error('FS operation failed');
}
};

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

const DIRNAME = dirname(fileURLToPath(import.meta.url));

const read = async () => {
// Write your code here
const fileToReadPath = join(DIRNAME, 'files', 'fileToRead.txt');

try {
await fs.access(fileToReadPath);
const content = await fs.readFile(fileToReadPath, 'utf-8');

console.log(content);
} catch (err) {
throw new Error('FS operation failed');
}
};

await read();
await read();
28 changes: 26 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
import { promises as fs } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

const DIRNAME = dirname(fileURLToPath(import.meta.url));

const rename = async () => {
// Write your code here
const wrongFilenamePath = join(DIRNAME, 'files', 'wrongFilename.txt');
const properFilenamePath = join(DIRNAME, 'files', 'properFilename.md');

try {
await fs.access(wrongFilenamePath);

try {
await fs.access(properFilenamePath);
throw new Error('FS operation failed');
} catch (err) {
if (err.code !== 'ENOENT') {
throw new Error('FS operation failed');
}
}

await fs.rename(wrongFilenamePath, properFilenamePath);
} catch (err) {
throw new Error('FS operation failed');
}
};

await rename();
await rename();
19 changes: 17 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { createHash } from 'crypto';
import { createReadStream } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

const DIRNAME = dirname(fileURLToPath(import.meta.url));

const calculateHash = async () => {
// Write your code here
const filesPath = join(DIRNAME, 'files', 'fileToCalculateHashFor.txt');
const hash = createHash('sha256');
const stream = createReadStream(filesPath);

stream.on('error', () => {
throw new Error('FS operation failed')
});
stream.on('data', (chunk) => hash.update(chunk));
stream.on('end', () => console.log(hash.digest('hex')));
};

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

This file was deleted.

37 changes: 37 additions & 0 deletions src/modules/cjsToEsm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import path from 'path';
import { release, version } from 'os';
import { createServer as createServerHttp } from 'http';
import { fileURLToPath } from 'url';
import { dirname } from 'path';

import './files/c.cjs';

import aJson from './files/a.json' with { type: 'json' };
import bJson from './files/b.json' with { type: 'json' };

const FILENAME = fileURLToPath(import.meta.url);
const DIRNAME = dirname(FILENAME);

const random = Math.random();

const PORT = 3000;

const unknownObject = random > 0.5 ? aJson : bJson;

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'));

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 };
17 changes: 15 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { createReadStream } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

const DIRNAME = dirname(fileURLToPath(import.meta.url));

const read = async () => {
// Write your code here
const filesPath = join(DIRNAME, 'files', 'fileToRead.txt');
const stream = createReadStream(filesPath);

stream.on('error', () => {
throw new Error('FS operation failed');
});

stream.pipe(process.stdout);
};

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

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

process.stdin.pipe(reverseStream).pipe(process.stdout);
};

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 { createWriteStream } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

const DIRNAME = dirname(fileURLToPath(import.meta.url));

const write = async () => {
// Write your code here
const filesPath = join(DIRNAME, 'files', 'fileToWrite.txt');
const stream = createWriteStream(filesPath);

stream.on('error', () => {
throw new Error('FS operation failed');
});

process.stdin.pipe(stream);
};

await write();
await write();
Loading