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

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

15 changes: 14 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { argv } from "node:process";
const parseArgs = () => {
// Write your code here
const commands = argv.slice(2);
const statements = commands.reduce((acc, v, ind, arr) => {
let property = v;
let value = arr[ind + 1];
if (property.indexOf('--') == 0 && value!=undefined && value?.indexOf('--') != 0) {
property = property.slice(2);
acc.push(`${property} is ${value}`);
}
return acc;
}, [])
console.log(statements.join(', '));
};



parseArgs();
12 changes: 11 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import {env} from 'node:process'
const parseEnv = () => {
// Write your code here
let statements = []
for (let key in env) {
if (key.startsWith('RSS_')) {
let statement = `${key}=${env[key]}`;
statements.push(statement)
}
}
let str = statements.join('; ');
console.log(str)

};

parseEnv();
13 changes: 11 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { exec,fork,spawn } from 'child_process';
import path, { dirname } from 'path';
import { stderr, stdin, stdout } from 'process';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const srcPath = path.join(__dirname, 'files', 'script.js');

const spawnChildProcess = async (args) => {
// Write your code here
const forked = fork(srcPath, args);
};

spawnChildProcess();
spawnChildProcess(['hello','name']);
44 changes: 43 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
import fs from 'fs';
import { readdir,copyFile,access,mkdir,stat } from 'fs/promises';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';


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


const copy = async () => {
// Write your code here
const originPath = path.resolve(__dirname, 'files');
const copyPath = path.resolve(__dirname, 'files_copy');
if (await isFileExists(originPath) && ! await isFileExists(copyPath)) {
await mkdir(copyPath);
await copyDir(originPath, copyPath);
} else {
throw new Error('Fs operation failed')
}

};

const copyDir = async (src,dest) => {
const names = await readdir(src);
names.forEach(async (fileName) => {
const newSrc = path.join(src, fileName);
const newDest = path.join(dest, fileName);
const newStat = await stat(newSrc)
if (newStat.isFile()) {
await copyFile(newSrc, newDest);
} else {
await mkdir(newDest);
copyDir(newSrc, newDest);
}
})
}

const isFileExists = async (path) => {
try {
await access(path);
return true
} catch (e) {
return false;
}
}

copy();
22 changes: 21 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import fs from 'fs';
import { readFile, writeFile } from 'fs/promises';
import { url } from 'inspector';
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';

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

const create = async () => {
// Write your code here
const pathToFile = path.join(__dirname, 'files', 'fresh.txt');
const content = 'Im fresh and young';
fs.access(pathToFile, fs.F_OK, async (err) => {
if (err) {
await writeFile(pathToFile, content);
} else {
console.log('Error: fs operation failed')
}
})
};



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


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


const remove = async () => {
// Write your code here
const pathToFile = path.join(__dirname, 'files', 'fileToRemove.txt');
try {
await unlink(pathToFile);
} catch (err) {
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 'fs';
import path from 'path';
import { readdir } from 'fs/promises';
import { fileURLToPath } from 'url';
import { dirname } from 'path';

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


const list = async () => {
// Write your code here
const pathToFiles = path.resolve(__dirname, 'files');
try {
const names = await readdir(pathToFiles);
console.log(names)
} catch (err) {
throw new Error('FS operation failed')
}
};

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

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

const read = async () => {
// Write your code here
const pathToTxt = path.join(__dirname, "files", 'fileToRead.txt');
try {
const txt = await readFile(pathToTxt, 'utf-8')
console.log(txt)

}
catch (err) {
console.log('ERROR FS operation failed ');
}
};

await read();
28 changes: 27 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import { access } from 'fs/promises';

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

const rename = async () => {
// Write your code here
const wrongFilePath = path.resolve(__dirname, 'files', 'wrongFilename.txt');
const properPath = path.resolve(__dirname, 'files', 'properFilename.md');
if (await isFileExists(wrongFilePath) || ! await isFileExists(properPath)) {

fs.rename(wrongFilePath, properPath,err=>err);
} else {
console.log('ERROR FS operation failed ');
}
};


const isFileExists = async (path) => {
try {
await access(path);
return true
} catch (e) {
return false;
}
}

await rename();
12 changes: 11 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import crypto, { createHash } from 'crypto';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
import { readFile } from 'fs/promises';

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

const calculateHash = async () => {
// Write your code here
const pathFile = path.resolve(__dirname, 'files', 'fileToCalculateHashFor.txt');
const text = await readFile(pathFile);
return createHash('sha256').update(pathFile).digest('hex')
};

await calculateHash();
24 changes: 17 additions & 7 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
const path = require('path');
const { release, version } = require('os');
const { createServer: createServerHttp } = require('http');
require('./files/c');
// const path = require('path');
// const { release, version } = require('os');
// const { createServer: createServerHttp } = require('http');
// require('./files/c');
import path from 'path';
import { createServer as createServerHttp } from 'http';
import { release, type, version } from 'os';
import a from './files/a.json' assert {type:'json'};
import b from './files/b.json' assert {type: 'json'};
import { fileURLToPath } from 'url';
import { dirname } from 'path';

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');
unknownObject = a
} else {
unknownObject = require('./files/b.json');
unknownObject = b
}

console.log(`Release ${release()}`);
Expand All @@ -33,7 +43,7 @@ myServer.listen(PORT, () => {
console.log('To terminate it, use Ctrl+C combination');
});

module.exports = {
export default {
unknownObject,
myServer,
};
Expand Down
3 changes: 3 additions & 0 deletions src/streams/files/fileToWrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fda
fsad

18 changes: 16 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { createReadStream } from 'fs';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
import { stdout } from 'process';


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


const read = async () => {
// Write your code here
const txtPath = path.resolve(__dirname, 'files', 'fileToRead.txt');
const readStream = createReadStream(txtPath,'utf-8');
let str = '';
readStream.on('data', chunk => str += chunk);
readStream.on('end', () => stdout.write(str));
};

await read();
read();
24 changes: 23 additions & 1 deletion src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import { dir } from 'console';
import fs from 'fs';
import path, { dirname } from 'path';
import { stdin, stdout } from 'process';
import { Transform } from 'stream';
import { fileURLToPath } from 'url';

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

const createReverse = () => {
return new Transform({
transform(chunk, enc, cb) {
const reverse = chunk.toString().split('').reverse().join('');
cb(null, reverse);
}
})
}

const transform = async () => {
// Write your code here
const reverse = createReverse();
stdin.pipe(reverse).pipe(stdout);
};



await transform();
18 changes: 17 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { stdin, stdout } from 'process';
import { createWriteStream } from 'fs';
import { appendFile } from 'fs/promises';
import { fileURLToPath } from 'url';
import path, { dirname } from 'path';


const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const pathToFile = path.resolve(__dirname, 'files', 'fileToWrite.txt');

const write = async () => {
// Write your code here
const writeFIle = createWriteStream(pathToFile, 'utf-8');
stdout.write('Hello! Write smth: ');
stdin.on('data', async data => {
writeFIle.write(data);
})
stdin.on('error', err => { throw new Error(err) });
};

await write();
Loading