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 .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"npm-scripts.showStartNotification": false
}
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Node.js basics

## !!! Please don't submit Pull Requests to this repository !!!
## Rs-School course!
21 changes: 20 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
const parseArgs = () => {
// Write your code here
const parsedArgs = process.argv.slice(2);
const output = {};

for (let i = 0; i < parsedArgs.length; i += 2) {
const key = parsedArgs[i].replace("--", "");
const value = parsedArgs[i + 1];
output[key] = value;

}

// for (const [key, value] of Object.entries(output)) {
// console.log(`${key} is ${value}`);
// }


const result = Object.entries(output)
.map(([key, value]) => `${key} is ${value}`);
console.log(result.join(', '));


};

parseArgs();
7 changes: 6 additions & 1 deletion 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 rssVars = Object.entries(process.env)
.filter(([key]) => key.startsWith('RSS_'))
.map(([key, value]) => `${key}=${value}`);
console.log(rssVars.join('; '));

};

parseEnv();
31 changes: 29 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
import {spawn} from 'child_process'
const script = './src/cp/files/script.js'
const input = 'Lorem ipsum dolor sit amet'
//const input = 'CLOSE'



const spawnChildProcess = async (args) => {
// Write your code here

if (!Array.isArray(args)) {
console.error('Incorrect arguments');
return;
}

const childProcess = spawn('node', [script, ...args], {
stdio: ['pipe', 'pipe', 'inherit'] // stdin and stdout for IPC
});

process.stdin.pipe(childProcess.stdin);
childProcess.stdout.pipe(process.stdout);

childProcess.on('message', (msg) => {
console.log(`Message from child: ${msg}`);
})

childProcess.stdin.write(input);


};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
await spawnChildProcess(['arg1', 'arg2', 'arg3']);

25 changes: 21 additions & 4 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
const copy = async () => {
// Write your code here
};
import fs from 'fs'
const source = './src/fs/files'
const destination = './src/fs/files_copy'

await copy();

// fs.cp(src, dest[, options], callback) = Asynchronously copies the entire directory structure from src to dest, including subdirectories and files.
// throws error if source don't exist by default
// error on exist throws error if destination exists

const copy = async (src, dest) => {
fs.promises.cp(src, dest, {force:false, errorOnExist:true, recursive:true })
.then( () => {
console.log("Copied")
})
.catch( (err) => {
console.error("FS operation failed", err)
})
}



await copy(source, destination);
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 fs from 'fs'

const folder = "./src/fs/files/";
const filename = "fresh.txt";
const content = "I am fresh and young";

const create = async () => {
// Write your code here
fs.open(folder + filename, "wx", (err, descriptor) => { // wx flag creates and opens, throws an error if exist
if (err) {
console.log("FS operation failed", err);
} else {
//console.log(descriptor);
fs.write(descriptor, content, (err, bytes) => {
if (err) {
console.error("FS operation failed", err);
} else {
console.log("File created");
}
})
}
})
};

await create();
14 changes: 13 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import fs from 'fs'
const path = './src/fs/files/'
const file = 'fileToRemove.txt'




const remove = async () => {
// Write your code here
fs.unlink(path + file, (err) => {
if (err) {
console.error("FS operation failed", err)
} else
console.log("Removed")
})
};

await remove();
8 changes: 7 additions & 1 deletion src/fs/files/fileToRemove.txt
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
How dare you!
My content
should
be
printed
into
console
!
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'
const path = './src/fs/files/'


const list = async () => {
// Write your code here
fs.readdir(path, (err,files) =>{
if (err) {
console.error('FS operation failed', err)
} else {
files.forEach(function(file) {
console.log(file)
})
}
}
)
};

await list();
17 changes: 13 additions & 4 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
const read = async () => {
// Write your code here
};
import fs from 'fs'
const path = './src/fs/files/'
const file = 'fileToRead.txt'

await read();
const read = async (source) => {

const data = await fs.promises.readFile(source, 'utf8')
.then((data) => console.log(data))
.catch((error) => console.error("FS Operation failed", error))

}


await read(path + file);
14 changes: 13 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import fs from 'fs'
const path = './src/fs/files/'
const oldname = 'wrongFilename.txt'
const newname = 'properFilename.md'


const rename = async () => {
// Write your code here
fs.rename( path + oldname, path + newname, (err) =>{
if (err) {
console.error('FS operation failed', err)
} else {
console.log('Renamed')
}
})
};

await rename();
20 changes: 19 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import fs from 'fs';
import crypto from 'crypto';
const file = "src/hash/files/fileToCalculateHashFor.txt"


const calculateHash = async () => {
// Write your code here
const hash = crypto.createHash('sha256');
const readStream = fs.createReadStream(file);
readStream.on('error', (err) => {
console.error('Crypto operation failed ', err);
})

readStream.on('data', (chunk) => {
hash.update(chunk);
})

readStream.on('end', () => {
const hashHex = hash.digest('hex');
console.log(hashHex);
})
};

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

This file was deleted.

39 changes: 39 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import path from 'path';
import { release, version } from 'os';
import { createServer as createServerHttp } from 'http';
import './files/c.js';
import { readFile } from 'fs/promises';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const random = Math.random();

export let unknownObject;

if (random > 0.5) {
unknownObject = JSON.parse( await readFile(new URL('./files/a.json', import.meta.url)));
} else {
unknownObject = JSON.parse( await readFile(new URL('./files/b.json', import.meta.url)));
}

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

1 change: 1 addition & 0 deletions src/streams/files/fileToWrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
will you write
21 changes: 20 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import fs, { ReadStream } from 'fs'

const file = 'src/streams/files/fileToRead.txt'

const read = async () => {
// Write your code here

const readStream = fs.createReadStream(file);

readStream.on('data', (chunk) => {
process.stdout.write(chunk);
});

readStream.on('end', () => {
console.log('\n');
console.log(`${file} has been read successfully.`)
})

readStream.on('error', (err) =>{
console.error("Stream operation failed", err);
});

};

await read();
18 changes: 17 additions & 1 deletion src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { Transform } from 'stream';
import { pipeline } from 'stream/promises';

const transform = async () => {
// Write your code here
class ReverseTransform extends Transform {
_transform(chunk, encoding, callback) {
const reversed = chunk.toString().split('').reverse().join('');
this.push(reversed);
callback();
}
}
const reverseTransform = new ReverseTransform();

await pipeline(
process.stdin,
reverseTransform,
process.stdout
);
};

await transform();
Loading