Skip to content
Open
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
70 changes: 69 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1 +1,69 @@
// write code here
'use strict';

/* eslint-disable no-console */

const fs = require('fs/promises');
const path = require('path');

async function moveApp() {
const src = process.argv[2];
const dest = process.argv[3];

try {
// 1. Перевірка аргументів
if (process.argv.length !== 4) {
console.error('Error: Source and destination required');
process.exit(0);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this works, it's a standard convention for command-line applications to exit with a non-zero status code (e.g., process.exit(1)) to signal that an error has occurred. This is helpful for scripting and automation where the exit code is checked to determine success or failure.

}

if (src === dest) {
return;
}

// 2. Перевірка джерела
const srcStat = await fs.stat(src);

if (!srcStat.isFile()) {
console.error('Error: Source is not a file');
process.exit(0);
}

// 3. Визначаємо фінальний шлях
let finalDest = dest;
const isFolderTarget = dest.endsWith('/') || dest.endsWith('\\');

try {
const destStat = await fs.stat(dest);

if (destStat.isDirectory()) {
// Якщо dest - існуюча папка, додаємо ім'я файлу до шляху
finalDest = path.join(dest, path.basename(src));
}
} catch (err) {
// Якщо шляху не існує,
// але він закінчується на / — це помилка (папки нема)
if (isFolderTarget) {
console.error('Error: Destination directory does not exist');
process.exit(0);
}

// Якщо шляху не існує і немає / — перевіряємо, чи існує батьківська папка
const parentDir = path.dirname(dest);

try {
await fs.access(parentDir);
} catch (accessErr) {
console.error('Error: Parent directory does not exist');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message is a bit inconsistent with the one on line 46. Both this case (e.g., mv file.txt non-existent-dir/new-file.txt) and the case on line 46 (e.g., mv file.txt non-existent-dir/) are about the destination directory not existing. It would be clearer to use a consistent error message like 'Error: Destination directory does not exist' in both places.

process.exit(0);
}
}

// 4. Переміщення
await fs.rename(src, finalDest);
} catch (err) {
console.error(err.message);
process.exit(0);
}
}

moveApp();
Loading