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
20 changes: 19 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import {cp} from "fs/promises";
import {dirname} from "../utils/dir.js";
import {COPY_FILE_EXISTS} from "../utils/fs_error_codes.js";

const copy = async () => {
// Write your code here
try {
const __dirname = dirname(import.meta.url);

await cp(
`${__dirname}/files`,
`${__dirname}/files_copy`,
{errorOnExist: true, recursive: true, force: false}
);
} catch (error) {
if (error.code === COPY_FILE_EXISTS) {
throw new Error('FS operation failed');
}

throw error;
}
};

await copy();
16 changes: 15 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import {writeFile} from 'fs/promises';
import {dirname} from "../utils/dir.js";
import {FILE_EXISTS} from "../utils/fs_error_codes.js";

const create = async () => {
// Write your code here
try {
const __dirname = dirname(import.meta.url);

await writeFile(`${__dirname}/files/fresh.txt`, 'I am fresh and young', { flag: 'wx' });
} catch (error) {
if (error.code === FILE_EXISTS) {
throw new Error('FS operation failed');
}

throw error;
}
};

await create();
15 changes: 14 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { unlink } from 'node:fs/promises';
import {dirname} from "../utils/dir.js";
import {FILE_NOT_FOUND} from "../utils/fs_error_codes.js";
const remove = async () => {
// Write your code here
try {
const __dirname = dirname(import.meta.url);

await unlink(`${__dirname}/files/fileToRemove.txt`);
} catch (error) {console.log(error);
if (error.code === FILE_NOT_FOUND) {
throw new Error('FS operation failed');
}

throw error;
}
};

await remove();
1 change: 0 additions & 1 deletion src/fs/files/fileToRemove.txt

This file was deleted.

20 changes: 19 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import {readdir} from "fs/promises";
import {dirname} from "../utils/dir.js";
import {FILE_NOT_FOUND} from "../utils/fs_error_codes.js";

const list = async () => {
// Write your code here
try {
const __dirname = dirname(import.meta.url);

const files = await readdir(`${__dirname}/files`);

for (const file of files) {
console.log(file);
}
} catch (error) {
if (error.code === FILE_NOT_FOUND) {
throw new Error('FS operation failed');
}

throw error;
}
};

await list();
24 changes: 23 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import readline from "readline";
import events from "events";
import {createReadStream} from 'fs';
import {dirname} from "../utils/dir.js";
import {FILE_NOT_FOUND} from "../utils/fs_error_codes.js";

const read = async () => {
// Write your code here
try {
const __dirname = dirname(import.meta.url);

const readLine = readline.createInterface({
input: createReadStream(`${__dirname}/files/fileToRead.txt`)
});

readLine.on('line', line => console.log(line));

await events.once(readLine, 'close');
} catch (error) {
if (error.code === FILE_NOT_FOUND) {
throw new Error('FS operation failed');
}

throw error;
}
};

await read();
20 changes: 19 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import {cp} from "fs/promises";
import {dirname} from "../utils/dir.js";
import {rename as fileRename} from 'fs/promises';
import {COPY_FILE_EXISTS, FILE_NOT_FOUND} from "../utils/fs_error_codes.js";

const rename = async () => {
// Write your code here
try {
const __dirname = dirname(import.meta.url);

await fileRename(
`${__dirname}/files/wrongFilename.txt`,
`${__dirname}/files/properFilename.md`
);
} catch (error) {
if ([FILE_NOT_FOUND, COPY_FILE_EXISTS].indexOf(error.code) !== -1) {
throw new Error('FS operation failed');
}

throw error;
}
};

await rename();