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.

7 changes: 6 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const parseArgs = () => {
// Write your code here
const args = Object.values(process.argv).slice(2);
const strArgs = args
.map((arg, index) => index % 2 === 0 ? `${arg} is ${args[index + 1]}`: null)
.filter(Boolean)
.join(', ')
console.log(strArgs)
};

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 envVariables = Object.entries(process.env)
const rssVariables = envVariables
.filter(([key, _]) => key.includes('RSS_'))
.map(([key, value]) => `${key}=${value}`);
const strRssVariables = rssVariables.join('; ')
console.log(strRssVariables);
};

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 { copyFile, mkdir, readdir } from 'node:fs/promises';
import { join } from 'node:path';

const copy = async () => {
// Write your code here
try {
const originalFolderPath = await join(import.meta.dirname, './files');
const copyFolderPath = await join(import.meta.dirname, './files_copy');
await mkdir(copyFolderPath);
const files = await readdir(originalFolderPath);
for ( const file of files ) {
copyFile(join(originalFolderPath, file), join(copyFolderPath, file));
}
} catch {
throw new Error('FS operation failed');
}
};

await copy();
16 changes: 12 additions & 4 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const create = async () => {
// Write your code here
};
import * as fs from 'node:fs/promises';
import{ join } from 'node:path';

await create();
const create = async () => {
try {
const filePath = await join(import.meta.dirname, './files/fresh.txt');
await fs.writeFile(filePath, 'I am fresh and young');
} catch {
throw new Error('FS operation failed');
}
};

await create();
10 changes: 9 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { rm } from 'node:fs/promises';
import { join } from 'node:path'

const remove = async () => {
// Write your code here
try {
const path = await join(import.meta.dirname, './files/fileToRemove.txt');
await rm(path);
} catch {
throw new Error('FS operation failed');
}
};

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

const list = async () => {
// Write your code here
try {
const files = await readdir(join(import.meta.dirname, './files'));
console.log(files);
} catch {
throw new Error('FS operation failed');
}

};

await list();
7 changes: 6 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { join } from 'node:path'
import { readFile } from 'node:fs/promises';

const read = async () => {
// Write your code here
const path = await join(import.meta.dirname, './files/fileToRead.txt');
const fileContent = await readFile(path, { encoding: 'utf-8' });
console.log(fileContent);
};

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

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

};

await rename();
11 changes: 10 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { createHash } from 'node:crypto';
import { createReadStream } from 'node:fs';
import { join } from 'node:path';

const calculateHash = async () => {
// Write your code here
const hash = createHash('sha256');
const read = createReadStream(join(import.meta.dirname, './files/fileToCalculateHashFor.txt'), { encoding: 'utf-8' });
read.on('data', (chunk) => hash.update(chunk));
read.on('end', () => {
console.log(hash.digest('hex'))
});
};

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

This file was deleted.

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

import './files/c.cjs';


const random = Math.random();

const { default: unknownObject} = random > 0.5 ? await import('./files/a.json', { with: {type: 'json' } }) : await import('./files/b.json', { with: { type: 'json' } });

console.log(`Release ${release()}`);
console.log(`Version ${version()}`);
console.log(`Path segment separator is "${path.sep}"`);

console.log(`Path to current file is ${import.meta.filename}`);
console.log(`Path to current directory is ${import.meta.dirname}`);

const myServer = createServerHttp((_, 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 { myServer, unknownObject }
1 change: 1 addition & 0 deletions src/streams/files/fileToWrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

8 changes: 7 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { createReadStream } from 'fs';
import { stdout } from 'process';

const read = async () => {
// Write your code here
const read = createReadStream('./src/streams/files/fileToRead.txt', { encoding: 'utf-8' });
read.on('data', (chunk) => {
stdout.write(chunk);
})
};

await read();
9 changes: 8 additions & 1 deletion src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Transform } from 'stream';
const transform = async () => {
// Write your code here
const reverseData = new Transform({
transform(chunk, encoding, callback) {
const reversedChunk = chunk.toString().split('').reverse().join('');
callback(null, reversedChunk);
}
})
process.stdin.pipe(reverseData).pipe(process.stdout);
};

await transform();
7 changes: 6 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { createWriteStream } from 'fs';
import { stdin } from 'process';
import { join } from 'path';

const write = async () => {
// Write your code here
const output = createWriteStream(join(import.meta.dirname, './files/fileToWrite.txt'));
stdin.pipe(output);
};

await write();
2 changes: 1 addition & 1 deletion src/wt/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ const performCalculations = async () => {
// Write your code here
};

await performCalculations();
await performCalculations();
12 changes: 11 additions & 1 deletion src/zip/compress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import {
createReadStream,
createWriteStream,
} from 'node:fs';
import { createGzip } from 'node:zlib';
import { join } from 'path';

const compress = async () => {
// Write your code here
const gzip = createGzip();
const source = createReadStream(join(import.meta.dirname, './files/fileToCompress.txt'));
const destination = createWriteStream(join(import.meta.dirname, './files/archive.gz'));
source.pipe(gzip).pipe(destination);
};

await compress();
12 changes: 11 additions & 1 deletion src/zip/decompress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import {
createReadStream,
createWriteStream,
} from 'node:fs';
import { createGunzip } from 'node:zlib';
import { join } from 'path';

const decompress = async () => {
// Write your code here
const gunzip = createGunzip();
const destination = createWriteStream(join(import.meta.dirname, './files/fileToCompress.txt'));
const source = createReadStream(join(import.meta.dirname, './files/archive.gz'));
source.pipe(gunzip).pipe(destination);
};

await decompress();