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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.idea
.DS_Store
56 changes: 56 additions & 0 deletions package-lock.json

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

12 changes: 11 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2);
const list = [];
const keyPrefix = '--';

args.forEach((key, index) => {
if (key.includes(keyPrefix)) {
list.push(key + ' is ' + args[index + 1])
}
});

console.log(list.join(', '));
};

parseArgs();
14 changes: 13 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import * as dotenv from 'dotenv'

const parseEnv = () => {
// Write your code here
dotenv.config()
const list = [];
const prefix = 'RSS_';

Object.keys(process.env).forEach(key => {
if (key.includes(prefix)) {
list.push(key + '=' + process.env[key])
}
});

console.log(list.join('; '))
};

parseEnv();
3 changes: 2 additions & 1 deletion src/cp/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ const spawnChildProcess = async (args) => {
// Write your code here
};

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

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

const copy = async () => {
// Write your code here
const folderPath = path.join(dirname, 'files');
const copyFolderPath = path.join(dirname, 'files_copy');

let isNoExistFilesFolder = !fs.existsSync(folderPath)
let isExistFilesFolder = fs.existsSync(copyFolderPath)

if (isExistFilesFolder || isNoExistFilesFolder) {
throw Error('FS operation failed')
} else {
fs.cp(folderPath, copyFolderPath, {recursive: true}, function (err) {
if (err) {
console.error(err);
} else {
console.log("success!");
}
});
}
};

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

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

const create = async () => {
// Write your code here
const filePath = path.join(dirname, 'files', 'fresh.txt');

open(filePath, 'wx', (err) => {
if (err && err.code === 'EEXIST') {
throw Error('FS operation failed')
}

fs.writeFile(filePath, 'I am fresh and young', function (err) {
if (err) throw err;
console.log('File is created successfully.');
});
});
};

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

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

const remove = async () => {
// Write your code here
const filenamePath = path.join(dirname, 'files', 'fileToRead.txt');

let isNoExistFilename = !fs.existsSync(filenamePath)

if (isNoExistFilename) {
throw Error('FS operation failed')
} else {
fs.unlink(filenamePath, function (err) {
if (err) {
console.error(err);
} else {
console.log("success!");
}
});
}
};

await remove();
28 changes: 27 additions & 1 deletion src/fs/list.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 list = async () => {
// Write your code here
const folderPath = path.join(dirname, 'files');

let isNoExistFilesFolder = !fs.existsSync(folderPath)

if (isNoExistFilesFolder) {
throw Error('FS operation failed')
} else {
fs.readdir(folderPath, function (err, files) {
if (err) {
console.error(err);
} else {
if (err) {
return console.log('Unable to read folder: ' + err);
}
files.forEach(function (file) {
console.log(file);
});
}
});
}
};

await list();
23 changes: 22 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
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 filenamePath = path.join(dirname, 'files', 'fileToRead.txt');

let isNoExistFilename = !fs.existsSync(filenamePath)

if (isNoExistFilename) {
throw Error('FS operation failed')
} else {
fs.readFile(filenamePath, 'utf8', function (err, data) {
if (err) {
console.error(err);
} else {
console.log(data);
}
});
}
};

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

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

const rename = async () => {
// Write your code here
const wrongFilenamePath = path.join(dirname, 'files', 'wrongFilename.txt');
const properFilenamePath = path.join(dirname, 'files', 'properFilename.md');

let isNoExistWrongFilename = !fs.existsSync(wrongFilenamePath)
let isExistProperFilename = fs.existsSync(properFilenamePath)

if (isNoExistWrongFilename || isExistProperFilename) {
throw Error('FS operation failed')
} else {
fs.rename(properFilenamePath, wrongFilenamePath, function (err) {
if (err) {
console.error(err);
} else {
console.log("success!");
}
});
}
};

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

const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
const hashSum = crypto.createHash('sha256');

const calculateHash = async () => {
// Write your code here
const filenamePath = path.join(dirname, 'files', 'fileToCalculateHashFor.txt');

fs.readFile(filenamePath, 'utf8', function (err, data) {
if (err) {
console.error(err);
} else {
hashSum.update(data)
const hex = hashSum.digest('hex');

console.log(hex);
}
});
};

await calculateHash();
2 changes: 1 addition & 1 deletion src/modules/cjsToEsm.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ myServer.listen(PORT, () => {

module.exports = {
unknownObject,
createMyServer,
myServer,
};

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

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

const random = Math.random();

export async function unknownObject() {
let unknownObject;

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

console.log(unknownObject);

return unknownObject;
}

export async function myServer() {
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;

myServer.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
console.log('To terminate it, use Ctrl+C combination');
});

return myServer;
}

await unknownObject();
await myServer();
Loading