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
17 changes: 17 additions & 0 deletions package-lock.json

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

13 changes: 11 additions & 2 deletions 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 res = process.argv.slice(2);
// considering that all pairs have both keys and values
const length = res.length / 2
const keys = res.filter((arg, i) => i % 2 === 0 && arg.startsWith('--'))
const cleanKeys = keys.map(key => key.replace(/^--/, ''))
const values = res.filter((arg, i) => i % 2 === 1 && !arg.startsWith('--') )
for (let i = 0; i < length; i++)
if (cleanKeys !== undefined && values !== undefined)
console.log(`${cleanKeys[i]} is ${values[i]}`)

}

parseArgs();
9 changes: 8 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const parseEnv = () => {
// Write your code here
const objEnv = process.env
const arr = []
for (const [key, value] of Object.entries(objEnv)) {
if (key.startsWith('RSS_')){
arr.push(`${key}=${value}`)
}
}
console.log(arr.join('; '))
};

parseEnv();
15 changes: 14 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { promises as fs } from 'fs';
import { readdir } from 'node:fs/promises';

const copy = async () => {
// Write your code here
try {
const copyFolderCheckFiles = await readdir('src/fs/files');
if (copyFolderCheckFiles){
await fs.cp('src/fs/files', 'src/fs/files_copy', {errorOnExist: true, recursive: true, force: false})
} else {
throw new Error('FS operation failed')
}
}
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 path from 'path';
import { fileURLToPath } from 'url';

const fileName = 'fresh.txt'
const text = 'I am fresh and young';
// it could be simplified by providing manually constructed path, example 'src/fs/files'
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

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

const create = async () => {
// Write your code here
};
try {
await fs.writeFile(fileToWrite, text, {flag: 'wx'});
}
catch(error) {
throw new Error('FS operation failed');
}
}

await create();
11 changes: 9 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { promises as fs } from 'fs';

const remove = async () => {
// Write your code here
try {
await fs.rm('src/fs/files/fileToRemove.txt');
}
catch(error) {
throw new Error('FS operation failed')
}
};

await remove();
await remove()
12 changes: 11 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { promises as fs } from 'fs';

const list = async () => {
// Write your code here
try {
const files = await fs.readdir('src/fs/files')
const arr = [];
files.map(file => arr.push(file))
console.log(arr)
}
catch {
throw new Error('FS operation failed')
}
};

await list();
11 changes: 10 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { promises as fs } from 'fs';


const read = async () => {
// Write your code here
try {
const content = await fs.readFile('src/fs/files/fileToRead.txt', 'utf8')
console.log(content)
}
catch (err){
throw new Error('FS operation failed')
}
};

await read();
9 changes: 8 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { promises as fs } from 'fs';

const rename = async () => {
// Write your code here
try {
await fs.rename('src/fs/files/wrongFilename.txt', 'src/fs/files/properFilename.md')
}
catch {
throw new Error('FS operation failed')
}
};

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

This file was deleted.

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

import './files/c.cjs';
const filesA = JSON.parse(
await readFile(
new URL('./files/a.json', import.meta.url)
)
);
const filesB = JSON.parse(
await readFile(
new URL('./files/b.json', import.meta.url)
)
);

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

const unknownObject = random > 0.5 ? filesA : filesB;

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 = createServer((_, 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,
};