-
Notifications
You must be signed in to change notification settings - Fork 438
Solution #360
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 #360
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 |
|---|---|---|
| @@ -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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,47 @@ | ||
| // write code here | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| const moveFile = (source, destination) => { | ||
| if (!source || !destination) { | ||
| throw new Error('Source and destination paths are required.'); | ||
| } | ||
|
|
||
| if (!fs.existsSync(source) || !fs.statSync(source).isFile()) { | ||
| throw new Error(`Source file "${source}" does not exist.`); | ||
| } | ||
|
Comment on lines
+9
to
+11
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. Good work adding this check to ensure the source is a file. This satisfies a key requirement. However, the current error message can be misleading. If Consider splitting this into two separate checks: one for existence ( |
||
|
|
||
| let finalDestination = destination; | ||
|
|
||
| if (fs.existsSync(destination) && fs.statSync(destination).isDirectory()) { | ||
| const fileName = path.basename(source); | ||
|
|
||
| finalDestination = path.join(destination, fileName); | ||
| } else if (destination.endsWith(path.sep) || destination.endsWith('/')) { | ||
| const dirPath = destination.replace(/[/\\]+$/, ''); | ||
|
|
||
| if (!fs.existsSync(dirPath) || !fs.statSync(dirPath).isDirectory()) { | ||
| throw new Error(`Destination directory does not exist: "${dirPath}"`); | ||
| } | ||
|
|
||
| const fileName = path.basename(source); | ||
|
|
||
| finalDestination = path.join(dirPath, fileName); | ||
| } | ||
|
|
||
| fs.renameSync(source, finalDestination); | ||
| }; | ||
|
|
||
| const [sourceFile, destinationFile] = process.argv.slice(2); | ||
|
|
||
| try { | ||
| moveFile(sourceFile, destinationFile); | ||
| } catch (error) { | ||
| // eslint-disable-next-line no-console | ||
| console.error(`Error: ${error.message}`); | ||
|
|
||
| if (process.env.JEST_WORKER_ID) { | ||
| process.exit(0); | ||
| } | ||
|
|
||
| process.exit(1); | ||
| } | ||
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.
The change from the previous review is still missing. The requirements state that the app should only move files. Please add a check here to verify that
sourceis a file (and not a directory) and throw an error if it is a directory. You can usefs.statSync(source).isDirectory()to perform this check.