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
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test

on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^2.1.1",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
Expand Down
88 changes: 87 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1 +1,87 @@
// write code here
/* eslint-disable no-console */
const fs = require('fs');
const path = require('path');

const args = process.argv.slice(2);

function moveAFile(src, dest) {
if (args.length !== 2 || !src || !dest) {
console.error(new Error('Two valid arguments must be entered'));

return;
}

if (args.some((a) => a.startsWith('-'))) {
console.error(new Error('Invalid arguments: flags are not allowed'));

return;
}

const filePath = path.resolve(src);

const fileStats = fs.statSync(filePath, { throwIfNoEntry: false });

if (!fileStats || !fileStats.isFile()) {
console.error(
new Error('Source file does not exist or is not a regular file.'),
);

return;
}

let fullPath;

try {
if (dest.endsWith('/') || dest.endsWith('\\')) {
if (!fs.existsSync(dest)) {
console.error(
new Error('Destination directory does not exist: ' + dest),
);

return;
}

if (!fs.statSync(dest).isDirectory()) {
console.error(
new Error('Destination path is not a directory: ' + dest),
);

return;
}
fullPath = path.join(dest, path.basename(src));
} else {
const destStats = fs.statSync(dest, { throwIfNoEntry: false });

if (destStats && destStats.isDirectory()) {
fullPath = path.join(dest, path.basename(src));
} else {
fullPath = dest;

const parentDir = path.dirname(fullPath);
const parentStats = fs.statSync(parentDir, { throwIfNoEntry: false });

if (!parentStats || !parentStats.isDirectory()) {
console.error(
new Error(
'Parent directory does not exist or is not a directory: ' +
parentDir,
),
);

return;
}
}
}

if (path.resolve(fullPath) === filePath) {
return;
}

fs.renameSync(filePath, fullPath);
console.log(`File moved successfully to ${fullPath}`);
} catch (err) {
console.error(err);
}
}

moveAFile(args[0], args[1]);