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
64 changes: 63 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1 +1,63 @@
// write code here
/* eslint-disable no-console */

'use strict';

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

function moveFile() {
const args = process.argv.slice(2);
const [source, destination] = args;

if (!source || !destination) {
console.error('Both source and destination are required');

return;
}

if (!fs.existsSync(source)) {
console.error('Source file does not exist');

return;
}

const sourceStat = fs.statSync(source);

if (!sourceStat.isFile()) {
console.error('Source is not a file');

return;
}

let targetDestination = destination;

if (fs.existsSync(destination)) {
const destinationStat = fs.statSync(destination);

if (destinationStat.isDirectory()) {
targetDestination = path.join(destination, path.basename(source));
}
} else {
if (destination.endsWith(path.sep)) {
console.error('Destination directory does not exist');

return;
}

const parentDir = path.dirname(destination);

if (!fs.existsSync(parentDir)) {
console.error('Parent directory does not exist');

return;
}
}

try {
fs.renameSync(source, targetDestination);
} catch (e) {
console.error(e.message);
}
}

moveFile();