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

argv.filter((el, index) => el.startsWith('--') && argsIndexes.push(index));

const result = argsIndexes.map(
(index) => `${argv[index].slice(2)} is ${argv[index + 1]}`
);

if (result.length) {
console.log(result.join(', '));
}
};

parseArgs();
7 changes: 6 additions & 1 deletion 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 result = Object.entries(process.env)
.filter((el) => el[0].startsWith('RSS_'))
.map((el) => `${el[0]}=${el[1]}`)
.join('; ');

console.log(result);
};

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

const FILE_PATH = fileURLToPath(import.meta.url);
const DIRECTORY_PATH = path.dirname(FILE_PATH);

const spawnChildProcess = async (args) => {
// Write your code here
const childProcess = spawn('node', [
`${DIRECTORY_PATH}/files/script.js`,
...args,
]);

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

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
//spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(process.argv);
20 changes: 19 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import path from 'path';
import { fileURLToPath } from 'url';
import { cp } from 'fs/promises';

const FILE_PATH = fileURLToPath(import.meta.url);
const FS_DIRECTORY_PATH = path.dirname(FILE_PATH);

const copy = async () => {
// Write your code here
try {
const sourcePath = path.join(FS_DIRECTORY_PATH, 'files');
const targetPath = path.join(FS_DIRECTORY_PATH, 'files_copy');

await cp(sourcePath, targetPath, {
errorOnExist: true,
recursive: true,
force: false,
});
} catch (error) {
throw new Error('FS operation failed');
}
};

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

const FILE_PATH = fileURLToPath(import.meta.url);
const FS_DIRECTORY_PATH = path.dirname(FILE_PATH);

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

try {
const sourcePath = path.join(FS_DIRECTORY_PATH, 'files');
file = await open(path.join(sourcePath, 'fresh.txt'), 'wx');
await file.writeFile('I am fresh and young');
} catch (error) {
throw new Error('FS operation failed');
}
};

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

const FILE_PATH = fileURLToPath(import.meta.url);
const FS_DIRECTORY_PATH = path.dirname(FILE_PATH);

const remove = async () => {
// Write your code here
const fileToRemove = path.join(
FS_DIRECTORY_PATH,
'files',
'fileToRemove.txt'
);
const isFileToRemoveExisted = existsSync(fileToRemove);

if (isFileToRemoveExisted) {
try {
await rm(fileToRemove);
} catch (error) {
throw new Error('FS operation failed');
}
} else {
throw new Error('FS operation failed');
}
};

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

const FILE_PATH = fileURLToPath(import.meta.url);
const FS_DIRECTORY_PATH = path.dirname(FILE_PATH);

const list = async () => {
// Write your code here
const filesDir = path.join(FS_DIRECTORY_PATH, 'files');

if (existsSync(filesDir)) {
try {
const files = await readdir(filesDir);

const fileNames = [];
for (const file of files) {
fileNames.push(file);
}
console.log(fileNames);
} catch (err) {
throw new Error('FS operation failed');
}
} else {
throw new Error('FS operation failed');
}
};

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 path from 'path';
import { fileURLToPath } from 'url';
import { existsSync } from 'fs';
import { readFile } from 'fs/promises';

const FILE_PATH = fileURLToPath(import.meta.url);
const FS_DIRECTORY_PATH = path.dirname(FILE_PATH);

const read = async () => {
// Write your code here
const sourcePath = path.join(FS_DIRECTORY_PATH, 'files', 'fileToRead.txt');
const isFileExisted = existsSync(sourcePath);

if (isFileExisted) {
try {
const fileContent = await readFile(sourcePath, { encoding: 'utf8' });
console.log(`${fileContent}`);
} catch (error) {
throw new Error('FS operation failed');
}
} else {
throw new Error('FS operation failed');
}
};

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 path from 'path';
import { fileURLToPath } from 'url';
import { existsSync } from 'fs';
import { rename as fsRename } from 'fs/promises';

const FILE_PATH = fileURLToPath(import.meta.url);
const FS_DIRECTORY_PATH = path.dirname(FILE_PATH);

const rename = async () => {
// Write your code here
const sourcePath = path.join(FS_DIRECTORY_PATH, 'files', 'wrongFilename.txt');
const targetPath = path.join(FS_DIRECTORY_PATH, 'files', 'properFilename.md');

const isTargetFileExisted = existsSync(targetPath);

if (!isTargetFileExisted) {
try {
await fsRename(sourcePath, targetPath);
} catch (error) {
throw new Error('FS operation failed');
}
} else {
throw new Error('FS operation failed');
}
};

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

const FILE_PATH = fileURLToPath(import.meta.url);
const DIRECTORY_PATH = path.dirname(FILE_PATH);

const calculateHash = async () => {
// Write your code here
return new Promise((resolve, reject) => {
const filePath = path.join(
DIRECTORY_PATH,
'files',
'fileToCalculateHashFor.txt'
);

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

stream.on('data', (chunk) => hash.update(chunk));
stream.on('end', () => {
console.log(hash.digest('hex'));
resolve();
});
stream.on('error', (err) => reject(err));
});
};

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

const FILE_PATH = fileURLToPath(import.meta.url);
const DIRECTORY_PATH = path.dirname(FILE_PATH);

import './files/c.mjs';

const random = Math.random();

const loadFile = async (fileName) => {
const filePath = path.join(DIRECTORY_PATH, 'files', fileName);
const fileDataRaw = await readFile(filePath, 'utf-8');
return JSON.parse(fileDataRaw);
};

const unknownObject =
random > 0.5 ? await loadFile('a.json') : await loadFile('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 ${FILE_PATH}`);
console.log(`Path to current directory is ${DIRECTORY_PATH}`);

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

const PORT = 3000;

console.log(unknownObject);

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

export { unknownObject, myServer };
1 change: 0 additions & 1 deletion src/modules/files/c.cjs

This file was deleted.

1 change: 1 addition & 0 deletions src/modules/files/c.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Hello from c.mjs!');
2 changes: 1 addition & 1 deletion src/streams/files/fileToRead.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This file should be read using Streams API
This file should be read using Streams API
21 changes: 20 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import path from 'path';
import { fileURLToPath } from 'url';
import { createReadStream } from 'fs';

const FILE_PATH = fileURLToPath(import.meta.url);
const DIRECTORY_PATH = path.dirname(FILE_PATH);

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

stream.on('error', (err) => {
console.error('Error reading file:', err.message);
});

stream.on('end', () => {
console.log('\n--- File reading finished ---');
});

stream.pipe(process.stdout);
};

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

const transform = async () => {
// Write your code here
const result = new Transform({
transform(chunk, _, callBack) {
callBack(null, chunk.toString().split('').reverse().join(''));
},
});
stdin.pipe(result).pipe(stdout);
};

await transform();
13 changes: 12 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import path from 'path';
import { createWriteStream } from 'fs';
import { fileURLToPath } from 'url';
import { dirname } from 'path';

const FILE_PATH = fileURLToPath(import.meta.url);
const DIRECTORY_PATH = dirname(FILE_PATH);

const write = async () => {
// Write your code here
const stream = createWriteStream(
path.join(DIRECTORY_PATH, 'files', 'fileToWrite.txt')
);
process.stdin.pipe(stream);
};

await write();
Loading