Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aaa #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

aaa #11

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
Binary file added mitso_dasha-main.zip
Binary file not shown.
1 change: 1 addition & 0 deletions mitso_dasha-main/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# mitso-nodejs-basic
39 changes: 39 additions & 0 deletions mitso_dasha-main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "mitso-nodejs-basics",
"version": "1.0.0",
"description": "",
"type": "module",
"scripts": {
"cli:args": "node src/cli/args.js --some-arg value1 --other 1337 --arg2 42",
"cli:env": "npx cross-env SOME=any MITSO_foo=bar MITSO_bar=baz node src/cli/env.js",
"cp": "node src/cp/cp.js",
"fs:copy": "node src/fs/copy.js",
"fs:create": "node src/fs/create.js",
"fs:delete": "node src/fs/delete.js",
"fs:list": "node src/fs/list.js",
"fs:read": "node src/fs/read.js",
"fs:rename": "node src/fs/rename.js",
"hash": "node src/hash/calcHash.js",
"modules": "node src/modules/esm.mjs",
"streams:read": "node src/streams/read.js",
"streams:transform": "node src/streams/transform.js",
"streams:write": "node src/streams/write.js",
"wt": "node src/wt/main.js",
"zip:compress": "node src/zip/compress.js",
"zip:decompress": "node src/zip/decompress.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/KalinkinFiz/mitso-nodejs-basic"
},
"keywords": [
"nodejs",
"mitso"
],
"author": "KalinkinFiz",
"license": "ISC",
"bugs": {
"url": "https://github.com/KalinkinFiz/mitso-nodejs-basic/issues"
},
"homepage": "https://github.com/KalinkinFiz/mitso-nodejs-basic#readme"
}
17 changes: 17 additions & 0 deletions mitso_dasha-main/src/cli/args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const parseArgs = () => {

const args = process.argv.slice(2);
const props = {};

for (let i = 0; i < args.length; i += 2)
{
const prop = args[i].slice(2);
const value = args[i + 1];
props[prop] = value;
}

for (const prop in props) {
console.log(prop, ' is ', props[prop] )
}
}

15 changes: 15 additions & 0 deletions mitso_dasha-main/src/cli/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const parseEnv = () => {

const prefix = 'MITSO_';
const envVariables = process.env;
const mitsoVariables = Object.keys(envVariables)

.filter(key => key.startsWith(prefix))
.map(key => `${key}=${envVariables[key]}`)
.join('; ');

console.log(mitsoVariables);

};

parseEnv();
15 changes: 15 additions & 0 deletions mitso_dasha-main/src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { fileURLToPath } from "url";
import { fork } from "child_process";
import path from "path";

const _dirname = fileURLToPath(new URL(".", import.meta.url));
const scriptFiles = path.join(__dirname, "files", "script.js");
const args = process.argv.slice(2);

const spawnChildProcess = async (args) => {
fork(scriptFiles, args,{
stdio: [process.stdin, process.stdout, "ipc"],
});
};

spawnChildProcess(args);
12 changes: 12 additions & 0 deletions mitso_dasha-main/src/cp/files/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const args = process.argv.slice(2);

console.log(`Total number of arguments is ${args.length}`);
console.log(`Arguments: ${JSON.stringify(args)}`);

const echoInput = (chunk) => {
const chunkStringified = chunk.toString();
if (chunkStringified.includes('CLOSE')) process.exit(0);
process.stdout.write(`Received from master process: ${chunk.toString()}\n`)
};

process.stdin.on('data', echoInput);
35 changes: 35 additions & 0 deletions mitso_dasha-main/src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import fs from 'fs/promises'
import path from 'path';
import url from 'url';

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

const copy = async () => {
const files = await fs.readdir(path.join(__dirname));
const files_copy = await fs.readdir(path.join(__dirname));

try {
if(files.includes('files')) {
if(files_copy.includes('files_copy')) throw new Error("Такая папка уже существует")

fs.mkdir(path.join(__dirname, 'files_copy'), {

});


await fs.cp(path.join(__dirname, 'files'), path.join(__dirname, 'files_copy'), {
errorOnExist: true,
force: true,
recursive: true,
});

console.log("Файлы скопированы")
} else
throw new Error("Такой папки не существует")

} catch (err) {
console.log(err.message);
}
};

await copy();
23 changes: 23 additions & 0 deletions mitso_dasha-main/src/fs/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import fs from 'fs/promises'
import path from 'path';
import url from 'url';

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

const create = async () => {
try{
const files = await fs.readdir(path.join(__dirname, 'files'));

if (files.includes("fresh.txt")) throw new Error("Уже существует");

await fs.writeFile(path.join(__dirname, "files", "fresh.txt"), "I am fresh and young", {
flag: "wx"
});
console.log("good");

} catch (err) {
console.log(err.message);
}
}

await create();
28 changes: 28 additions & 0 deletions mitso_dasha-main/src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import fs from 'fs/promises'
import path from 'path';
import url from 'url';

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

const remove = async () => {
try{
const files = await fs.readdir(path.join(__dirname, 'files'));

if(files.includes("fileToRemove.txt")){

await fs.unlink(path.join(__dirname, 'files', "hhh.txt"), {
flag: "wx"
});

console.log("Удален");

}else{
throw new Error("Уже удален")
}

}catch (err) {
console.log(err.message);
}
};

await remove();
1 change: 1 addition & 0 deletions mitso_dasha-main/src/fs/files/fresh.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am fresh and young
1 change: 1 addition & 0 deletions mitso_dasha-main/src/fs/files/hhh.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am fresh and young
1 change: 1 addition & 0 deletions mitso_dasha-main/src/fs/files/xd.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
111111
1 change: 1 addition & 0 deletions mitso_dasha-main/src/fs/files_copy/fresh.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am fresh and young
1 change: 1 addition & 0 deletions mitso_dasha-main/src/fs/files_copy/hhh.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am fresh and young
1 change: 1 addition & 0 deletions mitso_dasha-main/src/fs/files_copy/xd.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
111111
26 changes: 26 additions & 0 deletions mitso_dasha-main/src/fs/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import fs from 'fs/promises'
import path from 'path';
import url from 'url';

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

const list = async () => {
try{
const files = await fs.readdir(path.join(__dirname, 'files'));

if(files) {

await fs.readdir(path.join(__dirname, "files"), 'utf-8', {
flag: "wx"
});
console.log("Файлы: " + files);
}
else
throw new Error("Такой папки нет")

}catch (err) {
console.log(err.message);
}
};

await list();
25 changes: 25 additions & 0 deletions mitso_dasha-main/src/fs/read.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import fs from 'fs/promises'
import path from 'path';
import url from 'url';

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

const read = async () => {
try{
const files = await fs.readdir(path.join(__dirname, 'files'));

if (files.includes("xd.txt")) {

console.log(await fs.readFile(path.join(__dirname, 'files', "xd.txt"), 'utf-8', {
flag: "wx"
}));
}
else
throw new Error("Файл не существует")

}catch (err) {
console.log(err.message);
}
};

await read();
24 changes: 24 additions & 0 deletions mitso_dasha-main/src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import fs from 'fs/promises'
import path from 'path';
import url from 'url';

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

const rename = async () => {
try{
const files = await fs.readdir(path.join(__dirname, 'files'));

if (files.includes("properFilename.md")) throw new Error("Уже существует");

await fs.rename(path.join(__dirname, 'files', "wrongFilename.txt"), path.join(__dirname, 'files', "properFilename.md"), {
flag: "wx"
});
console.log("good")


}catch (err) {
console.log(err.message);
}
};

await rename();
16 changes: 16 additions & 0 deletions mitso_dasha-main/src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import fs from 'fs/promises'
import path from 'path'
import url from 'url'
import crypto from 'crypto'

const __dirname = url.fileURLToPath(new URL('.', import.meta.url))

const calculateHash = async () => {

const fileData = fs.readFile(path.join(__dirname, 'files', 'fileToCalculateHashFor.txt'))

const hash = crypto.createHash('sha256').update(fileData.toString()).digest('hex')
console.log(`Hash for ${hash}`)
}

await calculateHash()
1 change: 1 addition & 0 deletions mitso_dasha-main/src/hash/files/fileToCalculateHashFor.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Calculate hash for me!
46 changes: 46 additions & 0 deletions mitso_dasha-main/src/modules/Esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import http from 'http';
import { sep } from 'path';
import { release, version } from 'os';
import { fileURLToPath } from 'url'
import { dirname } from 'path'

import './files/c.js';

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

const random = Math.random();

import unknownObject1 from "./files/a.json" assert { type: "json" };
import unknownObject2 from "./files/b.json" assert { type: "json" };
if (random > 0.5) {
console.log(unknownObject1);
}
else {
console.log(unknownObject2);
}

console.log(`Release ${release()}`);
console.log(`Version ${version()}`);
console.log(`Path segment separator is "${sep}"`);
console.log(`Path to current file is ${__filename}`);
console.log(`Path to current directory is ${__dirname}`);

const myServer = http.createServer((_, res) => {
res.end('Request accepted');
});

const PORT = 3000;

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

export default {
unknownObject1,
unknownObject2,
myServer,
};
5 changes: 5 additions & 0 deletions mitso_dasha-main/src/modules/files/a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"a": 1,
"b": 2,
"c": 3
}
5 changes: 5 additions & 0 deletions mitso_dasha-main/src/modules/files/b.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"a": 11,
"b": 22,
"c": 33
}
1 change: 1 addition & 0 deletions mitso_dasha-main/src/modules/files/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Hello from c.js!');
1 change: 1 addition & 0 deletions mitso_dasha-main/src/streams/files/fileToRead.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file should be read using Streams API
2 changes: 2 additions & 0 deletions mitso_dasha-main/src/streams/files/fileToWrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
wedghfd
dd
Loading