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
12 changes: 11 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2);
const composedArgs = args.reduce((acc, arg, i, arr) => {
if(arg.startsWith('--')) {
acc[arg] = null
} else {
acc[arr[i-1]] = arg
}
return acc;
}, {})
const out = Object.entries(composedArgs).map(([k,v]) => `${k} is ${v}`)
console.log(out.join(', '))
};

parseArgs();
3 changes: 2 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const parseEnv = () => {
// Write your code here
const rssEnv = Object.entries(process.env).filter(([k,]) => k.startsWith('RSS_')).map(([k,v]) => `${k}=${v}`).join('; ');
console.log(rssEnv);
};

parseEnv();
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 fs from 'node:fs'
import { createFolder } from '../utils/file-helper.js'
import { OPERATION_FAILED } from '../utils/constants.js'

const copy = async () => {
// Write your code here
const folderName = 'files_copy';
const from = './files'
const to = `./${folderName}`;

try {
const maybeFolder = createFolder(to);
if(!maybeFolder) {
throw new Error(OPERATION_FAILED)
}

fs.cpSync(from, to, {recursive: true})
}
catch (err) {
console.log(err);
}


};

await copy();
19 changes: 18 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import fs from 'fs/promises';
import { isFileExist } from '../utils/file-helper.js'
import { OPERATION_FAILED } from '../utils/constants.js'

const create = async () => {
// Write your code here
const path = './files/fresh.txt';
const content = 'I am fresh and young';

try {

if(isFileExist(path)) {
throw new Error(OPERATION_FAILED)
}

await fs.writeFile(path, content);

} catch (err) {
console.warn(err)
}
};

await create();
12 changes: 11 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import fs from 'fs/promises';
import { isFileExist } from '../utils/file-helper.js'
import { OPERATION_FAILED } from '../utils/constants.js'

const remove = async () => {
// Write your code here
const path = './files/fileToRemove.txt';

if(!isFileExist(path)) {
throw new Error(OPERATION_FAILED);
}

await fs.unlink(path);
};

await remove();
23 changes: 22 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import fs from 'fs/promises';
import { isFileExist } from '../utils/file-helper.js'
import { OPERATION_FAILED } from '../utils/constants.js'

const list = async () => {
// Write your code here
try {
const path = './files';

if(!isFileExist(path)) {
throw new Error(OPERATION_FAILED);
}

const files = await fs.readdir(path);

for(let file in files) {
console.log(file);
}

} catch (err) {
console.log(err)
}


};

await list();
19 changes: 18 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import fs from 'fs/promises';
import { isFileExist } from '../utils/file-helper.js'
import { OPERATION_FAILED } from '../utils/constants.js'

const read = async () => {
// Write your code here
try {
const path = './files/fileToRead.txt';

if(!isFileExist(path)) {
throw new Error(OPERATION_FAILED);
}

const content = await fs.readFile(path, 'utf-8');
console.log(content);


} catch (err) {
console.log(err)
}
};

await read();
13 changes: 12 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { isFileExist } from '../utils/file-helper.js'
import { OPERATION_FAILED } from '../utils/constants.js'
import fs from 'node:fs';

const rename = async () => {
// Write your code here
const from = './files/wrongFilename.txt';
const to = './files/properFilename.md';

if(!isFileExist(from) || isFileExist(to)) {
throw new Error(OPERATION_FAILED)
}

fs.renameSync(from,to)
};

await rename();
8 changes: 7 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import fs from 'node:fs';
import crypto from 'node:crypto';

const calculateHash = async () => {
// Write your code here
const input = './files/fileToCalculateHashFor.txt';
const hash = crypto.createHash('sha256');
const in$ = fs.createReadStream(input, 'utf-8');
in$.pipe(hash).setEncoding('hex').pipe(process.stdout);
};

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

This file was deleted.

40 changes: 40 additions & 0 deletions src/modules/cjsToEsm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import path from 'node:path'
import { release, version } from 'node:os';
import { createServer as createServerHttp } from 'node:http';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';

const cjs = await import ('./files/c.cjs');

const random = Math.random();

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

console.log(`Release ${release()}`);
console.log(`Version ${version()}`);
console.log(`Path segment separator is "${path.sep}"`);

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

console.log(`Path to current file is ${path.__filename}`);
console.log(`Path to current directory is ${path.__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,
};
11 changes: 10 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import streams from 'node:stream/promises';
import fs from 'node:fs';

const read = async () => {
// Write your code here
const file = './files/fileToRead.txt';
const fileStream = fs.createReadStream(file, 'utf-8');

for await (const chunk of fileStream) {
process.stdout.write(chunk)
}

};

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

class ReverseText extends Transform {
constructor(options) {
super(options)
}

_transform(chunk, encoding, cb) {
const str = chunk.toString().split('').reduceRight((acc, char) => {
return acc + char
}, '')

this.push(str);

cb();
}
}


const transform = async () => {
// Write your code here
process.stdin.pipe(new ReverseText()).pipe(process.stdout)
};

await transform();
9 changes: 8 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import stream from 'node:stream';
import fs from 'node:fs';

const write = async () => {
// Write your code here
const file = './files/fileToWrite.txt';
const writable = fs.createWriteStream(file);
process.stdin.on("data", data => {
writable.write(data)
})
};

await write();
1 change: 1 addition & 0 deletions src/utils/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const OPERATION_FAILED = 'FS operation failed';
13 changes: 13 additions & 0 deletions src/utils/file-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { existsSync, mkdirSync } from 'node:fs';

export const isFileExist = (path, error) => existsSync(path);

export const createFolder = (path) => {
if(isFileExist(path)) {
return false
}

mkdirSync(path);

return true
}
13 changes: 12 additions & 1 deletion src/zip/compress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import fs from 'node:fs';
import streams from 'node:stream/promises';
import zlib from 'node:zlib';

const compress = async () => {
// Write your code here
const fromFile = './files/fileToCompress.txt';
const toFile = './files/archive.gz'
const from = fs.createReadStream(fromFile, 'utf-8');
const to = fs.createWriteStream(toFile);
const gzip = zlib.createGzip();

await streams.pipeline(from,gzip,to)

};

await compress();
12 changes: 11 additions & 1 deletion src/zip/decompress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import fs from 'node:fs';
import streams from 'node:stream/promises';
import zlib from 'node:zlib';

const decompress = async () => {
// Write your code here
const toFile = './files/fileToCompress.txt';
const fromFile = './files/archive.gz'
const from = fs.createReadStream(fromFile);
const to = fs.createWriteStream(toFile, 'utf-8');
const gzip = zlib.createGunzip();

await streams.pipeline(from,gzip,to)
};

await decompress();