-
Notifications
You must be signed in to change notification settings - Fork 407
Solution #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Solution #345
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
| } | ||
|
|
||
| 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'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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., |
||
| process.exit(0); | ||
| } | ||
| } | ||
|
|
||
| // 4. Переміщення | ||
| await fs.rename(src, finalDest); | ||
| } catch (err) { | ||
| console.error(err.message); | ||
| process.exit(0); | ||
| } | ||
| } | ||
|
|
||
| moveApp(); | ||
There was a problem hiding this comment.
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.