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
37 changes: 33 additions & 4 deletions src/cli/interactive.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
import readline from 'readline';

const rl = readline.createInterface({
input: process.stdin,
output: process.stdoutdate
});

const commands = {
uptime: () => process.stdout.write(`${process.uptime()}\n`),
cwd: () => process.stdout.write(`${process.cwd()}\n`),
date: () => process.stdout.write(`${new Date()}\n`),
exit: () => {
process.stdout.write('\nGoodbye!\n');
rl.close();
process.exit();
},
};

const interactive = () => {
// Write your code here
// Use readline module for interactive CLI
// Support commands: uptime, cwd, date, exit
// Handle Ctrl+C and unknown commands
rl.question('> ', (answer) => {
if (commands[answer]) {
commands[answer]();
} else {
process.stdout.write(`Unknown command\n`);
}
if (answer !== 'exit') {
interactive();
}
});
};

rl.on('SIGINT', () => {
process.stdout.write('\nGoodbye!\n');
process.exit();
});

interactive();
35 changes: 31 additions & 4 deletions src/cli/progress.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
const args = process.argv.slice(2);

const getArg = (name) => {
const idx = args.indexOf(`--${name}`);
return idx !== -1 ? args[idx + 1] : undefined;
};

const duration = parseInt(getArg('duration') ?? '5000');
const interval = parseInt(getArg('interval') ?? '100');
const length = parseInt(getArg('length') ?? '30');

const printBar = (percent) => {
const filled = Math.floor(length * percent / 100);
const empty = length - filled;
const bar = '█'.repeat(filled) + '░'.repeat(empty);
process.stdout.write(`\r[${bar}] ${percent}%`);
};

const progress = () => {
// Write your code here
// Simulate progress bar from 0% to 100% over ~5 seconds
// Update in place using \r every 100ms
// Format: [████████████████████ ] 67%
const steps = Math.floor(duration / interval);
const stepPercent = 100 / steps;
let elapsed = 0;

const intervalId = setInterval(() => {
elapsed++;
const percent = Math.min(Math.round(elapsed * stepPercent), 100);
printBar(percent);
if (percent >= 100) {
clearInterval(intervalId);
process.stdout.write('\nDone!\n');
}
}, 100);
};

progress();
35 changes: 30 additions & 5 deletions src/hash/verify.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
const verify = async () => {
// Write your code here
// Read checksums.json
// Calculate SHA256 hash using Streams API
// Print result: filename — OK/FAIL
import crypto from 'node:crypto';
import fs from 'node:fs';
import { pipeline } from 'node:stream/promises';
import { fileURLToPath } from 'node:url';
import path from 'node:path';

export const verify = async () => {
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const file = path.join(__dirname, 'checksums.json');
if (!fs.existsSync(file)) {
throw new Error('FS operation failed');
}

const checksums = JSON.parse(fs.readFileSync(file, 'utf8'));

for (const [filename, storedHash] of Object.entries(checksums)) {
const relativePath = path.join(__dirname, filename);
if (!fs.existsSync(relativePath)) {
throw new Error('FS operation failed');
}

const stream = fs.createReadStream(relativePath);
const hash = crypto.createHash('sha256');

await pipeline(stream, hash);

const result = hash.digest('hex');
const status = result === storedHash ? 'OK' : 'FAIL';
console.log(`${filename} — ${status}`);
}
};

await verify();