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
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules

yarn.lock
.prettierrc
/src/fs/files
package.json
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.slice(2) || [];
const list = new Array(args.length / 2)
.fill()
.map((_, idx) => `${args[idx * 2]} is ${args[idx * 2 + 1]}`)
.join(', ');

console.log(list);
};

parseArgs();
parseArgs();
9 changes: 7 additions & 2 deletions 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 list = Object.entries(process.env)
.filter(([key]) => key.indexOf('RSS_') === 0)
.map(([key, value]) => `${key}=${value}`)
.join('; ');

console.log(list);
};

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

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

const spawnChildProcess = async (args) => {
// Write your code here
const child = spawn(
'node',
[path.resolve(__dirname, './files/script.js'), ...args],
{
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
},
);

child.on('exit', (code) => {
console.log(`Child process exited with code ${code}`);
});

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

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(/* [someArgument1, someArgument2, ...] */);
16 changes: 15 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import promises from 'node:fs/promises';
import fs from 'node:fs';

const copy = async () => {
// Write your code here
const folderName = './src/fs/files';
const folderCopyName = './src/fs/files_copy';

try {
if (!fs.existsSync(folderName) || fs.existsSync(folderCopyName)) {
throw 'error';
}

await promises.cp(folderName, folderCopyName, { recursive: true });
} catch (err) {
throw new Error('FS operation failed');
}
};

await copy();
20 changes: 18 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import promises from 'node:fs/promises';
import fs from 'node:fs';

const create = async () => {
// Write your code here
const folderName = './src/fs/files';
const fileName = 'fresh.txt';
const path = `${folderName}/${fileName}`;
const content = 'I am fresh and young';

try {
if (fs.existsSync(path)) {
throw 'error';
}

await promises.writeFile(path, content);
} catch (err) {
throw new Error('FS operation failed');
}
};

await create();
await create();
19 changes: 17 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import promises from 'node:fs/promises';
import fs from 'node:fs';

const remove = async () => {
// Write your code here
const folderName = './src/fs/files';
const fileName = 'fileToRemove.txt';
const path = `${folderName}/${fileName}`;

try {
if (!fs.existsSync(path)) {
throw 'error';
}

await promises.unlink(path);
} catch (err) {
throw new Error('FS operation failed');
}
};

await remove();
await remove();
18 changes: 16 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import promises from 'node:fs/promises';
import fs from 'node:fs';

const list = async () => {
// Write your code here
const folderName = './src/fs/files';

try {
if (!fs.existsSync(folderName)) {
throw 'error';
}

const list = await promises.readdir(folderName);
console.log(list.join('\n'));
} catch (err) {
throw new Error('FS operation failed');
}
};

await list();
await list();
20 changes: 18 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import promises from 'node:fs/promises';
import fs from 'node:fs';

const read = async () => {
// Write your code here
const folderName = './src/fs/files';
const fileName = 'fileToRead.txt';
const path = `${folderName}/${fileName}`;

try {
if (!fs.existsSync(path)) {
throw 'error';
}

const data = await promises.readFile(path, { encoding: 'utf8' });
console.log(data);
} catch (err) {
throw new Error('FS operation failed');
}
};

await read();
await read();
25 changes: 23 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import promises from 'node:fs/promises';
import fs from 'node:fs';

const rename = async () => {
// Write your code here
const folderName = './src/fs/files';
const wrongFileName = 'wrongFilename.txt';
const properFileName = 'properFilename.md';

try {
if (
!fs.existsSync(`${folderName}/${wrongFileName}`) ||
fs.existsSync(`${folderName}/${properFileName}`)
) {
throw 'error';
}

await promises.rename(
`${folderName}/${wrongFileName}`,
`${folderName}/${properFileName}`,
);
} catch (err) {
throw new Error('FS operation failed');
}
};

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

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

const calculateHash = async () => {
// Write your code here
const hash = crypto.createHash('sha256');
fs.createReadStream(
path.resolve(__dirname, './files/fileToCalculateHashFor.txt'),
)
.on('data', (data) => {
hash.update(data);
})
.on('end', () => {
const fileHash = hash.digest('hex');
console.log('Hash:', fileHash);
})
.on('error', (error) => {
console.log('Error:', error?.message);
});
};

await calculateHash();
await calculateHash();
40 changes: 0 additions & 40 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 from 'path';
import { release, version } from 'os';
import { createServer as createServerHttp } from 'http';
import './files/c.js';
import { fileURLToPath } from 'url';
import fs from 'node:fs';

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

const random = Math.random();

let unknownObject;

if (random > 0.5) {
unknownObject = JSON.parse(
fs.readFileSync(path.resolve(__dirname, './files/a.json')),
);
} else {
unknownObject = JSON.parse(
fs.readFileSync(path.resolve(__dirname, './files/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 ${__filename}`);
console.log(`Path to current directory is ${__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 default {
unknownObject,
myServer,
};
2 changes: 1 addition & 1 deletion src/modules/files/c.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log('Hello from c.js!');
export default console.log('Hello from c.js!');
15 changes: 13 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import fs from 'node:fs';
import path from 'path';
import { fileURLToPath } from 'url';

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

const read = async () => {
// Write your code here
fs.createReadStream(path.resolve(__dirname, './files/fileToRead.txt'))
.pipe(process.stdout)
.on('data', function () {
this.destroy();
});
};

await read();
await read();
15 changes: 13 additions & 2 deletions src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { Transform } from 'stream';

const transform = async () => {
// Write your code here
const reverseStream = new Transform({
transform(chunk, _, callback) {
const reversedText = chunk.toString().split('').reverse().join('');
this.push(reversedText);
this.push('\n');
callback();
},
});

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

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

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

const write = async () => {
// Write your code here
const writeStream = fs.createWriteStream(
path.resolve(__dirname, './files/fileToWrite.txt'),
);

process.stdin.pipe(writeStream);
};

await write();
await write();
Loading