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
22 changes: 21 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import { mkdir, cp } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

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

try {
const filePath = fileURLToPath(import.meta.url);
const dirName = path.dirname(filePath);
const sourceDir = path.join(dirName, 'files');
const destDir = path.join(dirName, 'files_copy');
await cp( sourceDir, destDir, {force: false, errorOnExist: true, recursive : true});

} catch (error) {
if(error.code === 'ERR_FS_CP_EEXIST'){
throw new Error('FS operation failed');
} else {
throw error;
}
console.log(`Error! ${error}`);

}
};

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


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

let fileHandle;
try {
const fileURL = new URL('./files/fresh.txt', import.meta.url);
const filePath = fileURLToPath(fileURL);

fileHandle = await open(filePath, 'ax');

fileHandle.appendFile('I am fresh and young');

} catch (error) {
if(error.code === 'EEXIST'){
throw new Error('FS operation failed');
} else {
throw error;
}
} finally {
await fileHandle?.close();
}
};

await create();
15 changes: 15 additions & 0 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { rm } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';

const remove = async () => {
// Write your code here
try {
const fileURL = new URL('./files/fileToRemove.txt', import.meta.url);
const filePath = fileURLToPath(fileURL);
await rm(filePath);

} catch (error) {
if(error.code === 'ENOENT'){
throw new Error('FS operation failed');
} else {
throw error;
}
}
};

await remove();
20 changes: 18 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { readdir } from "node:fs/promises";
import { fileURLToPath } from "node:url";

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

try {
const dirURL = new URL("./files/", import.meta.url);
const dirPath = fileURLToPath(dirURL);

const files = await readdir(dirPath);
for (const file of files) console.log(file);
} catch (err) {
if (err.code === "ENOENT") {
throw new Error("FS operation failed");
} else {
throw err;
}
}
};

await list();
await list();
18 changes: 18 additions & 0 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { fileURLToPath } from 'node:url';
import { readFile } from "node:fs/promises";


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

const fileURL = new URL('./files/fileToRead.txt', import.meta.url);
const filePath = fileURLToPath(fileURL);
const contents = await readFile(filePath, { encoding: 'utf8' });
console.log(contents);

} catch (error) {
if(error.code === 'ENOENT'){
throw new Error('FS operation failed');
} else {
throw error;
}
}
};

await read();
34 changes: 34 additions & 0 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
import { rename as fs_rename, access} from 'node:fs/promises';
import { fileURLToPath } from 'node:url';

const rename = async () => {
// Write your code here
let src = 'wrongFilename.txt';
let dest = 'properFilename.md';

const srcURL = new URL('./files/' + src, import.meta.url);
const srcPath = fileURLToPath(srcURL);

const destURL = new URL('./files/' + dest, import.meta.url);
const destPath = fileURLToPath(destURL);

try {
await access(destPath);
throw new Error('FS operation failed');
} catch (error) {
if(error.code === 'ENOENT'){}
else {
throw error;
}
}


try {
await fs_rename(srcPath, destPath);

} catch (error) {
if(error.code === 'ENOENT'){

throw new Error('FS operation failed');
} else {
throw error;
}
}
};

await rename();
31 changes: 30 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
import { createReadStream } from 'fs';
import{ createHash } from 'crypto';
import { dirname, join } from 'path';
import { fileURLToPath } from 'node:url';

const calculateHash = async () => {
// Write your code here
try{
const fileURL = new URL('./files/fileToCalculateHashFor.txt', import.meta.url);
const filePath = fileURLToPath(fileURL);

const hash = createHash('sha256');
const stream = createReadStream(filePath);

stream.on('data', (chunk) => hash.update(chunk));

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

stream.on('error', (error) => {
if(error.code === 'ENOENT') {
throw new Error ('operation failed');
} else {
throw error;
}
});
} catch (error) {
console.error(error.message);
}

};

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

This file was deleted.

37 changes: 37 additions & 0 deletions src/modules/cjsToEsm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import path from 'path';
import { fileURLToPath } from 'url';
import { release, version } from 'os';
import { createServer as createServerHttp } from 'http';
import './files/c.cjs' assert { type: 'json' }; // or without assert depending on the actual contents

import a from './files/a.json' assert { type: 'json' };
import b from './files/b.json' assert { type: 'json' };

const random = Math.random();

let unknownObject = random > 0.5 ? a : b;

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

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

// Re-export as a named export (no module.exports in ESM)
export { unknownObject, myServer };
28 changes: 27 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
import { createReadStream } from 'fs';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'path';

const read = async () => {
// Write your code here
try {
const fileURL = new URL('./files/fileToRead.txt', import.meta.url);
const filePath = fileURLToPath(fileURL);


/* const stream = createReadStream(filePath);
stream.on('data', (chunk) => process.stdout.write(chunk));
stream.on('end', () => console.log('\nDone')); */

const stream = createReadStream(filePath); //create readable stream
stream.pipe(process.stdout); //redirect stream in standart out(like console.log)

stream.on('error', (err) => {
if (err.code === "ENOENT") {
throw new Error('FS operation failed');
} else {
throw err;
}
});
} catch (error) {

}

};

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

const reverseStream = new Transform({
transform(chunk, encoding, callback){
const transformedChunk = chunk.toString().split('').reverse().join('');
callback(null, transformedChunk);
}
})

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

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

await transform();
30 changes: 30 additions & 0 deletions src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
import { createWriteStream } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';


const write = async () => {
// Write your code here
try {
const fileURL = new URL('./files/fileToWrite.txt', import.meta.url);
const filePath = fileURLToPath(fileURL);

const writableStream = createWriteStream(filePath);

//process.stdin.pipe(writableStream);
// writableStream.on('error', (err) => {
// throw new Error('FS operation failed');
// });

process.stdin.on('data', (chunk) => {
writableStream.write(chunk);
});

process.stdin.on('end', () =>{
writableStream.end();
})




} catch (error) {

}
};

await write();