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

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

function writeError(message) {
process.stderr.write(`${message}\n`);
}

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

if (!source || !destination) {
writeError('Source and destination are required');

return;
}

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

return;
}

if (source === destination) {
return;
}

let finalDestination = destination;

if (destination.endsWith('/') || destination.endsWith('\\')) {
if (!fs.existsSync(destination)) {
writeError('Destination directory does not exist');

return;
}

finalDestination = path.join(destination, path.basename(source));
} else if (
fs.existsSync(destination) &&
fs.statSync(destination).isDirectory()
) {
finalDestination = path.join(destination, path.basename(source));
} else {
const destinationDir = path.dirname(destination);

if (destinationDir !== '.' && !fs.existsSync(destinationDir)) {
writeError('Destination directory does not exist');

return;
}
}

fs.renameSync(source, finalDestination);
}

main();
Loading