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

const parseArgs = () => {
// Write your code here
let parsedData = []
argv.forEach(arg => {
parsedData.push(arg)
if (parsedData.length === 2) {
console.log(`${parsedData[0].toString().slice(2)} is ${parsedData[1]}`)
parsedData = []
}
})
};

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

env.RSS_name1 = 'value1';
env.RSS_name2 = 'value2';

const parseEnv = () => {
// Write your code here
Object.keys(env).forEach(k => {
if (k.includes('RSS_')) {
console.log(`${k}=${env[k]}`)
}
})
};

parseEnv();
16 changes: 14 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { spawn } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';

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

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

const child = spawn('node', [`${__dirname}/files/script.js`, ...args])


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

spawnChildProcess();
spawnChildProcess(['test']);
34 changes: 33 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
import {
mkdir,
readdir
} from 'node:fs/promises';
import {
readFileSync,
writeFileSync
} from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';

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

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

try {
const fileNames = await readdir(`${__dirname}/files`);
if (fileNames.length === 0) {
throw new Error('FS operation failed')
}

await mkdir(`${__dirname}/files_copy`);

fileNames.forEach(name => {
const data = readFileSync(`${__dirname}/files/${name}`, {
encoding: 'utf-8'
})
writeFileSync(`${__dirname}/files_copy/${name}`, data)
})

} catch (err) {
throw new Error(err.message)
}
};

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

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


const errMess = 'FS operation failed'

const create = async () => {
// Write your code here
try {
await open(`${__dirname}/files/fresh.txt`, 'r',)
throw new Error(errMess)
} catch (err) {
await appendFile(`${__dirname}/files/fresh.txt`, 'I am fresh and young');
if (err.message === errMess) {
console.error(err.message)
}

}

};

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 { unlink } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';

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


const remove = async () => {
// Write your code here
try {
await unlink(`${__dirname}/files/fileToRemove.txt`);
} catch (error) {
throw new Error('FS operation failed')
}
};

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

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


const list = async () => {
// Write your code here
try {
const files = await readdir(`${__dirname}/files`);
for (const file of files)
console.log(file);
} catch (err) {
throw new Error('FS operation failed')
}
};

await list();
17 changes: 16 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { readFile } from 'fs/promises';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';

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


const errMessage = 'FS operation failed'

const read = async () => {
// Write your code here
try {
const contents = await readFile(`${__dirname}/files/fileToRead.txt`, { encoding: 'utf8' });
console.log(contents);
} catch (err) {
throw new Error(errMessage)
}
};

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 { rename as changeName } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';

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

const from = `${__dirname}/files/wrongFilename.txt`;
const to = `${__dirname}/files/properFilename.md`;

const rename = async () => {
// Write your code here
try {
await changeName(from, to);
} catch (err) {
throw new Error('FS operation failed')
}
};

await rename();
16 changes: 15 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import {
readFile
} from 'fs/promises';
const {
createHash,
} = await import('node:crypto');
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';

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

const calculateHash = async () => {
// Write your code here
const data = await readFile(`${__dirname}/files/fileToCalculateHashFor.txt`);
var hashForLog = createHash('sha256').update(data).digest("hex");
console.log(hashForLog);
};

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

This file was deleted.

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

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

const random = Math.random();

let unknownObject;

if (random > 0.5) {
unknownObject = JSON.parse(await readFile(`${__dirname}/files/a.json`, "utf8"));
} else {
unknownObject = JSON.parse(await readFile(`${__dirname}/files/b.json`, "utf8"));
}

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

export default {
unknownObject,
myServer,
};

1 change: 1 addition & 0 deletions src/streams/files/fileToWrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

16 changes: 15 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import {
createReadStream
} from 'node:fs';
import { stdout } from 'node:process';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';

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

const read = async () => {
// Write your code here
const stream = createReadStream(`${__dirname}/files/fileToRead.txt`);

stream.on('data', (chunk) => {
stdout.write(chunk.toString())
})
};

await read();
27 changes: 26 additions & 1 deletion src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import { stdin, stdout } from 'node:process'
import { Transform, pipeline } from 'node:stream';

const readable = stdin
const writable = stdout

const myTransform = new Transform({
transform(chunk, encoding, callback) {
try {
const resultString = `${chunk.toString().trim().split('').reverse().join('')}`;
callback(null, resultString + '\n');
} catch (err) {
callback(err);
}
},
});


const transform = async () => {
// Write your code here
pipeline(
readable,
myTransform,
writable,
err => {
console.log(err)
}
)
};

await transform();
Loading