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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
1 change: 0 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# Node.js basics

## !!! Please don't submit Pull Requests to this repository !!!
19 changes: 18 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,24 @@
"description": "This repository is the part of nodejs-assignments https://github.com/AlreadyBored/nodejs-assignments",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"read": "node src/fs/read.js",
"copy": "node src/fs/copy.js",
"create": "node src/fs/create.js",
"list-files": "node src/fs/list.js",
"delete": "node src/fs/delete.js",
"rename": "node src/fs/rename.js",
"cp": "node src/cp/cp.js",
"args": "node src/cli/args.js --first-name Masha --last-name Marchenko",
"env": "npx cross-env RSS_first_name=Masha RSS_last_name=Marchenko birthyear=2001 node src/cli/env.js",
"calc-hash": "node src/hash/calcHash.js",
"modules": "node src/modules/cjsToEsm.mjs",
"read-stream": "node src/streams/read.js",
"transform-stream": "node src/streams/transform.js",
"write-stream": "node src/streams/write.js",
"wt": "node src/wt/main.js",
"compress": "node src/zip/compress.js",
"decompress": "node src/zip/decompress.js"
},
"repository": {
"type": "git",
Expand Down
16 changes: 14 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
export const parseArgs = () => {
// Write your code here
};
const userInputArgs = process.argv.slice(2);
const cliArguments = userInputArgs.reduce((acc, arg, index, arr) => {
const value = arr[index + 1];
if (value && arg.startsWith('--')) {
const transformedArg = arg.slice(2);
const cliArgumentsTransformed = `${transformedArg} is ${value}`;
acc.push(cliArgumentsTransformed);
} return acc;
}, [])

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

parseArgs();
10 changes: 8 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export const parseEnv = () => {
// Write your code here
};
const rssVariables = Object.entries(process.env)
.filter(([key, _]) => key.startsWith('RSS_'))
.map(([key, value]) => `${key}=${value}`)

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

parseEnv();
8 changes: 8 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { dirname } from "path";
import { fileURLToPath } from "url";

export function getDirname(url) {
const __filename = fileURLToPath(url);

return dirname(__filename);
}
16 changes: 14 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
import child_process from 'child_process';
import path from "path";
import {getDirname} from "../common.js";

export const spawnChildProcess = async (args) => {
// Write your code here
};
const __dirname = getDirname(import.meta.url);
const filePath = path.join(__dirname, 'files', 'script.js');

const child = child_process.fork(filePath, args)

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

spawnChildProcess('--silent --all')
1 change: 1 addition & 0 deletions src/fs/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DEFAULT_ERROR_MESSAGE = 'FS operation failed.';
23 changes: 21 additions & 2 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
import fs from 'fs/promises';
import path from 'path';
import {DEFAULT_ERROR_MESSAGE} from "./constants.js";
import { getDirname } from '../common.js'


export const copy = async () => {
// Write your code here
};
const __dirname = getDirname(import.meta.url);
const srcPath = path.join(__dirname, 'files');
const destPath = path.join(__dirname, 'files_copy');

try {
await fs.mkdir(destPath);
const srcFiles = await fs.readdir(srcPath);
await Promise.all(srcFiles.map(file => fs.copyFile(path.join(srcPath, file), path.join(destPath, file))))
.then(() => 'The files were copied to a new folder')
} catch {
throw new Error(DEFAULT_ERROR_MESSAGE);
}
};

copy();
18 changes: 16 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
import fs from 'fs/promises';
import path from 'path';
import { DEFAULT_ERROR_MESSAGE } from "./constants.js";
import { getDirname } from "../common.js";


export const create = async () => {
// Write your code here
};
const filePath = path.join(getDirname(import.meta.url), 'files', 'fresh.txt');
const content = 'I am fresh and young';
try {
await fs.writeFile(filePath, content, {flag: 'wx'}).then(() => console.log('"fresh.txt" was created'))
} catch {
throw new Error(DEFAULT_ERROR_MESSAGE)
}
};

create();
20 changes: 18 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
import path from "path";
import fs from 'fs';
import {DEFAULT_ERROR_MESSAGE} from "./constants.js";
import {getDirname} from "../common.js";

export const remove = async () => {
// Write your code here
};
const __dirname = getDirname(import.meta.url);
const filePath = path.join(__dirname, 'files', 'fileToRemove.txt');

fs.rm(filePath, (err) => {
if (err) {
throw new Error(DEFAULT_ERROR_MESSAGE);
} else {
console.log('The file was deleted');
}
})
};

remove();
2 changes: 1 addition & 1 deletion src/fs/files/hello.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Hello Node.js
Hello node.js
20 changes: 18 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
import path from "path";
import fs from "fs";
import {DEFAULT_ERROR_MESSAGE} from "./constants.js";
import {getDirname} from "../common.js";

export const list = async () => {
// Write your code here
};
const __dirname = getDirname(import.meta.url);
const filesPath = path.join(__dirname, 'files');

if (!fs.existsSync(filesPath)) {
throw new Error(DEFAULT_ERROR_MESSAGE);
}

fs.readdir(filesPath, (_, files) => {
console.log(files)
})
}

list();
18 changes: 16 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
import path from "path";
import fs from "fs";
import {DEFAULT_ERROR_MESSAGE} from "./constants.js";
import {getDirname} from "../common.js";

export const read = async () => {
// Write your code here
};
const __dirname = getDirname(import.meta.url);
const filePath = path.join(__dirname, 'files', 'fileToRead.txt');

if (!fs.existsSync(filePath)) {
throw new Error(DEFAULT_ERROR_MESSAGE);
}

fs.readFile(filePath, 'utf8', (err, data) => console.log(data))
};

read();
26 changes: 24 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
import path from "path";
import fs from "fs";
import {DEFAULT_ERROR_MESSAGE} from "./constants.js";
import {getDirname} from "../common.js";

export const rename = async () => {
// Write your code here
};
const __dirname = getDirname(import.meta.url);
const wrongNamedFilePath = path.join(__dirname, 'files', 'wrongFilename.txt');
const newFilePath = path.join(__dirname, 'files', 'properFilename.md')

if (!fs.existsSync(wrongNamedFilePath) || fs.existsSync(newFilePath)) {
throw new Error(DEFAULT_ERROR_MESSAGE);
}

fs.rename(wrongNamedFilePath, newFilePath, (err) => {
if (err) {
throw new Error(DEFAULT_ERROR_MESSAGE);
} else {
console.log('The file was renamed')
}
});
};

rename();

18 changes: 16 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
import crypto from 'crypto';
import fs from 'fs';
import path, {dirname} from "path";
import {fileURLToPath} from "url";

export const calculateHash = async () => {
// Write your code here
};
const __dirname = dirname(fileURLToPath(import.meta.url));
const filePath = path.join(__dirname, 'files', 'fileToCalculateHashFor.txt');
const readable = fs.createReadStream(filePath);
const hash = crypto.createHash('sha256');

return new Promise(resolve => {
readable.on('data', data => hash.update(data)).on('end', () => resolve(hash.digest('hex')))
})
};

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

This file was deleted.

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

const random = Math.random();
const __dirname = getDirname(import.meta.url);
const __filename = fileURLToPath(import.meta.url);

const { default: unknownObject } = random > 0.5 ?
await import('./files/a.json', { assert: { type: "json" } }) :
import('./files/b.json', { assert: { 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 ${__filename}`);
console.log(`Path to current directory is ${__dirname}`);

const createMyServer = createServerHttp((_, res) => {
res.end('Request accepted');
});

export {
unknownObject,
createMyServer,
};

13 changes: 11 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import path from "path";
import fs from 'fs';
import {getDirname} from "../common.js";

export const read = async () => {
// Write your code here
};
const __dirname = getDirname(import.meta.url);
const filePath = path.join(__dirname, 'files', 'fileToRead.txt');
const readableStream = fs.createReadStream(filePath);
readableStream.pipe(process.stdout);
};

read();
25 changes: 23 additions & 2 deletions src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
import {pipeline, Transform} from 'stream'

export const transform = async () => {
// Write your code here
};
const readableFromTerminal = process.stdin;
const writableToTerminal = process.stdout;
const transform = new Transform({
transform(chunk, _, callback) {
const chunkStringified = chunk.toString().trim();
const reversedChunk = chunkStringified.split('').reverse().join('');

callback(null, reversedChunk + '\n')
}
})

pipeline(
readableFromTerminal,
transform,
writableToTerminal,
err => {
if (err) console.log(err);
});
};

transform();
Loading