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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.env
10 changes: 8 additions & 2 deletions 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;

args.forEach((arg, ind, arr) => {
if (arg.startsWith('--')) {
console.log(`${arg} is ${arr[ind + 1]}`);
}
});
};

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

for (let key in env) {
if (key.startsWith('RSS_')) {
console.log(`${key}=${env[key]}`);
}
}
};

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

const spawnChildProcess = async (args) => {
// Write your code here
const folderName = 'files';
const fileName = 'script.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const scriptPath = path.join(__dirname, folderName, fileName);

const child = childProcess.fork(scriptPath, args);

child.on('close', (code) => {
console.log(`\x1b[91mChild process exited with code ${code}\x1b[0m`);
});
};

// Put your arguments in function call to test this functionality
Expand Down
42 changes: 41 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
import path from 'path';
import {fileURLToPath} from 'url';
import fsPromises from 'fs/promises';

const copy = async () => {
// Write your code here
const srcFolder = 'files';
const distFolder = 'files_copy';
const errorMessage = 'FS operation failed';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const srcPath = path.join(__dirname, srcFolder);
const distPath = path.join(__dirname, distFolder);

const copyFolder = async (srcPath, distPath) => {
const files = await fsPromises.readdir(srcPath);

await fsPromises.mkdir(distPath);

for (const file of files) {
const stats = await fsPromises.stat(`${srcPath}/${file}`);

const srcFilePath = path.join(srcPath, file);
const distFilePath = path.join(distPath, file);

if (stats.isDirectory()) {
await copyFolder(srcFilePath, distFilePath);
} else {
await fsPromises.copyFile(srcFilePath, distFilePath);
}
}
};

try {
await copyFolder(srcPath, distPath);
} catch (error) {
if (error.code === 'EEXIST' || error.code === 'ENOENT') {
throw new Error(errorMessage);
} else {
console.error(error);
}
}
};

await copy();
31 changes: 31 additions & 0 deletions src/fs/copy_with_cp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import path from 'path';
import { fileURLToPath } from 'url';
import fsPromises from 'fs/promises';

const copy_with_cp = async () => {
const srcFolder = 'files';
const distFolder = 'files_copy';
const errorMessage = 'FS operation failed';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const srcPath = path.join(__dirname, srcFolder);
const distPath = path.join(__dirname, distFolder);

try {
await fsPromises.cp(srcPath, distPath, {
errorOnExist: true,
force: false,
recursive: true,
});
console.log('Folder copied');
} catch (error) {
if (error.code === 'ERR_FS_CP_EEXIST' || error.code === 'ENOENT') {
throw new Error(errorMessage);
} else {
console.error(error);
}
}
};

await copy_with_cp();
26 changes: 24 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import path from 'path';
import { fileURLToPath } from 'url';
import fsPromises from 'fs/promises';

const create = async () => {
// Write your code here
const distFolder = 'files';
const fileName = 'fresh.txt';
const content = 'I am fresh and young';
const errorMessage = 'FS operation failed';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const pathToFile = path.join(__dirname, distFolder, fileName);

try {
await fsPromises.writeFile(pathToFile, content, { flag: 'wx' });
console.log('File created');
} catch (error) {
if (error.code === 'EEXIST') {
throw new Error(errorMessage);
} else {
console.error(error);
}
}
};

await create();
await create();
25 changes: 23 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import path from 'path';
import { fileURLToPath } from 'url';
import fsPromises from 'fs/promises';

const remove = async () => {
// Write your code here
const folderName = 'files';
const fileName = 'fileToRemove.txt';
const errorMessage = 'FS operation failed';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const srcPath = path.join(__dirname, folderName, fileName);

try {
await fsPromises.rm(srcPath);
console.log('File removed');
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error(errorMessage);
} else {
console.error(error);
}
}
};

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

const list = async () => {
// Write your code here
const folderName = 'files';
const errorMessage = 'FS operation failed';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const srcPath = path.join(__dirname, folderName);

try {
const list =await fsPromises.readdir(srcPath);
console.log(list);
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error(errorMessage);
} else {
console.error(error);
}
}
};

await list();
await list();
26 changes: 24 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import path from 'path';
import { fileURLToPath } from 'url';
import fsPromises from 'fs/promises';

const read = async () => {
// Write your code here
const folderName = 'files';
const fileName = 'fileToRead.txt';
const errorMessage = 'FS operation failed';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const srcPath = path.join(__dirname, folderName, fileName);

try {
const fh = await fsPromises.open(srcPath);
const rs = fh.createReadStream();
rs.pipe(process.stdout);
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error(errorMessage);
} else {
console.error(error);
}
}
};

await read();
await read();
44 changes: 42 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
import path from 'path';
import { fileURLToPath } from 'url';
import fsPromises from 'fs/promises';

const rename = async () => {
// Write your code here
const folderName = 'files';
const srcFileName = 'wrongFilename.txt';
const distFileName = 'properFilename.md';
const errorMessage = 'FS operation failed';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const srcPath = path.join(__dirname, folderName, srcFileName);
const distPath = path.join(__dirname, folderName, distFileName);

const isFileExist = async (filePath) => {
try {
await fsPromises.access(filePath);
} catch (error) {
return false;
}

return true;
};

try {
const isDestFileExist = await isFileExist(distPath);
if (isDestFileExist) {
const err = new Error('Destination file already exists');
err.code = 'ENOENT';
throw err;
}

await fsPromises.rename(srcPath, distPath);
console.log('File renamed');
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error(errorMessage);
} else {
console.error(error);
}
}
};

await rename();
await rename();
28 changes: 26 additions & 2 deletions 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 fsPromises from 'fs/promises';
import { createHash } from 'crypto';

const calculateHash = async () => {
// Write your code here
const srcFolder = 'files';
const fileName = 'fileToCalculateHashFor.txt';
const errorMessage = 'FS operation failed';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const filePath = path.join(__dirname, srcFolder, fileName);

try {
const hash = createHash('sha256');
const fh = await fsPromises.open(filePath);
const rs = fh.createReadStream();
rs.pipe(hash).setEncoding('hex').pipe(process.stdout);
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error(errorMessage);
} else {
console.error(error);
}
}
};

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

const require = createRequire(import.meta.url);

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

await import('./files/c.js');

const random = Math.random();

Expand Down Expand Up @@ -33,8 +41,4 @@ myServer.listen(PORT, () => {
console.log('To terminate it, use Ctrl+C combination');
});

module.exports = {
unknownObject,
myServer,
};

export { unknownObject, myServer };
26 changes: 24 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import path from 'path';
import { fileURLToPath } from 'url';
import fsPromises from 'fs/promises';

const read = async () => {
// Write your code here
const folderName = 'files';
const fileName = 'fileToRead.txt';
const errorMessage = 'FS operation failed';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const srcPath = path.join(__dirname, folderName, fileName);

try {
const fh = await fsPromises.open(srcPath);
const rs = fh.createReadStream();
rs.pipe(process.stdout);
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error(errorMessage);
} else {
console.error(error);
}
}
};

await read();
await read();
Loading