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
14 changes: 13 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
const parseArgs = () => {
// Write your code here
let formatted =
process.argv.slice(2)
.reduce((acc, curr, index, arr) => {
if(!(index % 2)) {
return acc += curr.slice(2);
}

acc += ` is ${curr}`;
if(index !== arr.length - 1) acc += ", ";
return acc;
}, "");

console.log(formatted);
};

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 formatted = Object.entries(process.env)
.filter(([key]) => key.startsWith("RSS_"))
.reduce((acc, [key, value]) => acc += `${key}=${value}; `, "")
.trimEnd();

console.log(formatted);
};

parseEnv();
16 changes: 13 additions & 3 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { fork } from "child_process"
import { join } from "path";

import { getPaths } from "../helpers/index.js";
import { stderr, stdin, stdout } from "process";

const { __dirname } = getPaths(import.meta.url);

const spawnChildProcess = async (args) => {
// Write your code here
const path = join(__dirname, "files", "script.js");
fork(path, args, {
stdio: { stdin, stdout, stderr }
});
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(process.argv.slice(2));
2 changes: 1 addition & 1 deletion src/cp/files/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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.stdout.write(`Received from master process: ${chunk.toString().trim()}\n`)
};

process.stdin.on('data', echoInput);
27 changes: 26 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import { join } from "path";
import { cp } from "fs/promises";
import { access } from "fs";

import { getPaths, fsErrorMessage } from "../helpers/index.js";

const { __dirname } = getPaths(import.meta.url);

const copy = async () => {
// Write your code here
const source = join(__dirname, "files");
const dest = join(__dirname, "files_copy");

access(dest, async (e) => {
if (!e) throw new Error(fsErrorMessage);

if (e.code === "ENOENT") {
try {
await cp(source, dest, { recursive: true });
return
} catch (e) {
if (e.code === "ENOENT") throw new Error(fsErrorMessage);
throw e;
}
}

throw e;
})
};

await copy();
26 changes: 25 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
import { join } from "path";
import { writeFile } from "fs/promises"
import { access } from "fs";

import { getPaths, fsErrorMessage } from "../helpers/index.js"

const { __dirname } = getPaths(import.meta.url);

const create = async () => {
// Write your code here
const path = join(__dirname, "files", "fresh.txt");
const data = "I am fresh and young";

access(path, async (e) => {
if (!e) throw new Error(fsErrorMessage);

if (e.code === "ENOENT") {
try {
await writeFile(path, data);
return
} catch (e) {
throw e
}
}

throw e
});
};

await create();
16 changes: 15 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { join } from "path";
import { rm } from "fs/promises";

import { getPaths, fsErrorMessage } from "../helpers/index.js"

const { __dirname } = getPaths(import.meta.url);

const remove = async () => {
// Write your code here
const path = join(__dirname, "files", "fileToRemove.txt");

try {
await rm(path);
} catch (e) {
if (e?.code === "ENOENT") throw new Error(fsErrorMessage);
throw e;
}
};

await remove();
17 changes: 16 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { join } from "path";
import { readdir } from "fs/promises"

import { getPaths, fsErrorMessage } from "../helpers/index.js"

const { __dirname } = getPaths(import.meta.url);

const list = async () => {
// Write your code here
const path = join(__dirname, "files");

try {
const folderList = await readdir(path);
console.log(folderList);
} catch (e) {
if(e.code === "ENOENT") throw new Error(fsErrorMessage);
throw e;
}
};

await list();
20 changes: 19 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { join } from "path";
import { readFile } from "fs/promises";

import { getPaths, fsErrorMessage } from "../helpers/index.js"

const { __dirname } = getPaths(import.meta.url);

const read = async () => {
// Write your code here
const path = join(__dirname, "files", "fileToRead.txt");

try {
const data = await readFile(path);
console.log(data.toString("utf-8"));
} catch (e) {
if(e.code === "ENOENT") {
throw new Error(fsErrorMessage);
}

throw e;
}
};

await read();
28 changes: 27 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
import { access } from "fs";
import { rename as renameFS } from "fs/promises";
import { join } from "path";

import { getPaths, fsErrorMessage } from "../helpers/index.js"

const { __dirname } = getPaths(import.meta.url);

const rename = async () => {
// Write your code here
const filesPath = join(__dirname, "files");
const source = join(filesPath, "wrongFilename.txt");
const destination = join(filesPath, "properFilename.md");

access(destination, async (e) => {
if (!e) {
throw new Error(fsErrorMessage);
}

if (e.code === "ENOENT") {
try{
await renameFS(source, destination);
return
} catch (e) {
if(e.code === "ENOENT") throw new Error(fsErrorMessage);
throw e;
}
}
})
};

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 { join } from "path";
import { createHash } from "crypto";
import { ReadStream } from "fs";

import { getPaths } from "../helpers/index.js";

const { __dirname } = getPaths(import.meta.url);

const calculateHash = async () => {
// Write your code here
const path = join(__dirname, "files", "fileToCalculateHashFor.txt");
const hash = createHash("sha256")

ReadStream(path)
.on("data", (data) => {
hash.update(data);
})
.on("end", () => {
console.log(hash.digest("hex"));
})
.on("error", (e) => {
console.log(e);
})
};

await calculateHash();
1 change: 1 addition & 0 deletions src/helpers/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const fsErrorMessage = "FS operation failed";
2 changes: 2 additions & 0 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { getPaths } from "./paths.js"
export { fsErrorMessage } from "./errors.js"
12 changes: 12 additions & 0 deletions src/helpers/paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';

export const getPaths = (url) => {
const __filename = fileURLToPath(url);
const __dirname = dirname(__filename);

return {
__filename,
__dirname
}
}
40 changes: 40 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import path from "path";
import { release, version } from "os";
import { createServer as createServerHttp } from "http";

import { getPaths } from "../helpers/index.js";
const { __dirname, __filename } = getPaths(import.meta.url);

await import('./files/c.js');

const random = Math.random();

export let unknownObject;

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

unknownObject = JSON.parse(JSON.stringify(unknownObject)).default;

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');
});
3 changes: 1 addition & 2 deletions src/modules/cjsToEsm.cjs → src/modules/test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,4 @@ myServer.listen(PORT, () => {
module.exports = {
unknownObject,
myServer,
};

};
14 changes: 13 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { join } from "path";
import { ReadStream } from "fs";
import { pipeline } from "stream";

import { getPaths } from "../helpers/index.js";

const { __dirname } = getPaths(import.meta.url);

const read = async () => {
// Write your code here
const path = join(__dirname, "files", "fileToRead.txt");

pipeline(ReadStream(path), process.stdout, (e) => {
if (e) console.log(e);
});
};

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

const transform = async () => {
// Write your code here
const tStream = new Transform({
transform(chunk, _, callback) {
this.push(chunk.toString().split("").reverse().join("").trim() + "\n");
callback();
}
});

pipeline(process.stdin, tStream, process.stdout, (e) => {
if(e) {
console.log(e);
}
});
};

await transform();
16 changes: 15 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { WriteStream } from "fs"
import { join } from "path";
import { pipeline } from "stream";

import { getPaths } from "../helpers/index.js";

const { __dirname } = getPaths(import.meta.url);

const write = async () => {
// Write your code here
const path = join(__dirname, "files", "fileToWrite.txt");

pipeline(process.stdin, WriteStream(path), (e) => {
if(e) {
console.log(e);
}
});
};

await write();
Loading