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: 16 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { log } from 'console';

const parseArgs = () => {
// Write your code here
const result = [];
const args = process.argv.slice(2);

let temp = '';
for (let i = 0; i < args.length; i++) {
if (i % 2 === 0) {
temp = args[i].slice(2);
} else {
result.push(`${ temp } is ${ args[i] }`);
temp = '';
}
}

log(result.join(', '));
};

parseArgs();
10 changes: 9 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { log } from 'console';

const parseEnv = () => {
// Write your code here
const values = Object.keys(process.env)
.filter(value => value.includes('RSS'));

const result = values.map(value =>
`${ value }=${ process.env[value] }`);

log('env: ', result.join('; '));
};

parseEnv();
14 changes: 12 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { spawn } from 'child_process';

const spawnChildProcess = async (args) => {
// Write your code here
const child = spawn('node', ['src/cp/files/script.js', ...args]);

process.stdin.pipe(child.stdin);

child.stdout.pipe(process.stdout);

child.on('close', (code) => {
process.exit(code);
});
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(['someArgument1', 'someArgument2',]);
27 changes: 26 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { readdir } from 'fs/promises'

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

const FILES_DIR = `${ __dirname }/files`;

const getDirectories = async source =>
(await readdir(source, { withFileTypes: true }))
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);

const copy = async () => {
// Write your code here
const folders = await getDirectories(__dirname);

if (folders.includes('files_copy') || !folders.includes('files')) {
throw new Error('FS operation failed');
}

fs.cp(FILES_DIR, `${ __dirname }/files_copy`, { recursive: true }, (err) => {
if (err) {
throw new Error('FS operation failed');
}
});
};

await copy();
28 changes: 27 additions & 1 deletion src/fs/create.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';

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

const FILES_DIR = `${ __dirname }/files`;

const FILE_NAME = 'fresh.txt';
const FILE_CONTENT = 'I am fresh and young';

const create = async () => {
// Write your code here
fs.readdir(FILES_DIR, (err, files) => {
if (err) throw new Error(err);
else {
if (files.includes(FILE_NAME)) {
throw new Error('FS operation failed');
} else {
fs.writeFile(
`${ FILES_DIR }/${ FILE_NAME }`,
FILE_CONTENT,
(err) => {
if (err) throw new Error('FS operation failed');
});
}
}
});
};

await create();
15 changes: 14 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

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

const FILES_DIR = `${ __dirname }/files`;

const remove = async () => {
// Write your code here
fs.unlink(`${ FILES_DIR }/fileToRemove.txt`, (err) => {
if (err) {
throw new Error('FS operation failed');
}
});
};

await remove();
15 changes: 14 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { log } from 'console';

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

const FILES_DIR = `${ __dirname }/files`;

const list = async () => {
// Write your code here
fs.readdir(FILES_DIR, (err, files) => {
if (err) throw new Error('FS operation failed');
log(files);
});
};

await list();
21 changes: 20 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { log } from 'console';

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

const FILES_DIR = `${ __dirname }/files`;

const read = async () => {
// Write your code here
fs.readFile(
`${ FILES_DIR }/fileToRead.txt`,
'utf8',
(err, data) => {
if (err) {
throw new Error('FS operation failed');
}

log(data);
});
};

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

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

const FILES_DIR = `${ __dirname }/files`;

const rename = async () => {
// Write your code here
fs.rename(
`${ FILES_DIR }/wrongFilename.txt`,
`${ FILES_DIR }/properFilename.md`,
(err) => {
if (err) throw new Error('FS operation failed');
});
};

await rename();
15 changes: 14 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { createHash } from "crypto";
import fs from '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 reader = fs.createReadStream(`${ __dirname }/files/fileToCalculateHashFor.txt`);

reader.on('data', (chunk) => {
const hash = createHash("sha256").update(chunk).digest("hex");
console.log(hash);
});
};

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

This file was deleted.

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

import './files/c.js';

import fileA from './files/a.json' with { type: "json" };
import fileB from './files/b.json' with { type: "json" };

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

const random = Math.random();

export let unknownObject;

if (random > 0.5) {
unknownObject = fileA;
} else {
unknownObject = fileB;
}

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 }`);

export 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');
});
13 changes: 12 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import fs from '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
const readStream = fs.createReadStream(`${ __dirname }/files/fileToRead.txt`);

readStream.pipe(process.stdout);
};



await read();
5 changes: 4 additions & 1 deletion src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const transform = async () => {
// Write your code here
process.stdin.on("data", data => {
data = data.toString().split('').reverse().join("");
process.stdout.write(data + "\n");
})
};

await transform();
11 changes: 10 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import fs from '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(`${ __dirname }/files/fileToWrite.txt`);

process.stdin.pipe(writeStream);
};

await write();
Loading