-
Notifications
You must be signed in to change notification settings - Fork 439
Solution #366
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 #366
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,64 @@ | ||
| // write code here | ||
| const fs = require('node:fs'); | ||
| const path = require('node:path'); | ||
|
|
||
| function moveFiles() { | ||
| const argv = process.argv.slice(2); | ||
|
|
||
| if (argv.length !== 2) { | ||
| /* eslint-disable no-console */ | ||
| // throw new Error('You need to pass two arguments!'); | ||
| console.error('You need to pass two arguments!'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const [srcArg, destArgv] = argv; | ||
|
|
||
| const src = path.resolve(srcArg); | ||
| const dest = path.resolve(destArgv); | ||
|
|
||
| if (!fs.existsSync(src) || !fs.statSync(src).isFile()) { | ||
| // throw new Error('Please check if the file you want to transfer exists!'); | ||
|
|
||
| console.error('Please check if the file you want to transfer exists!'); | ||
|
|
||
| return; | ||
|
Comment on lines
+20
to
+25
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. Replace
Author
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. the test will crash, I cant do this |
||
| } | ||
|
|
||
| const destIsDir = destArgv.endsWith(path.sep); | ||
| let finalPath = ''; | ||
|
|
||
| if (destIsDir) { | ||
| if (!fs.existsSync(dest) || !fs.statSync(dest).isDirectory()) { | ||
| // throw new Error('Destination must be an existing directory!'); | ||
| console.error('Destination must be an existing directory!'); | ||
|
Comment on lines
+31
to
+34
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 should throw an Error instead of using console.error() + return. The task explicitly requires 'an error is thrown' when a destination directory (specified with trailing |
||
|
|
||
| return; | ||
| } | ||
|
|
||
| finalPath = path.join(dest, path.basename(src)); | ||
| } else { | ||
| if (fs.existsSync(dest) && fs.statSync(dest).isDirectory()) { | ||
| finalPath = path.join(dest, path.basename(src)); | ||
| } else { | ||
| finalPath = dest; | ||
| } | ||
| } | ||
|
|
||
| const parentDir = path.dirname(finalPath); | ||
|
|
||
| if (!fs.existsSync(parentDir)) { | ||
| // throw new Error('Destination directory does not exist!'); | ||
| console.error('Destination directory does not exist!'); | ||
|
|
||
| return; | ||
|
Comment on lines
+49
to
+54
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. Replace
Author
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. the test will crash, I cant do this |
||
| } | ||
|
|
||
| try { | ||
| fs.renameSync(src, finalPath); | ||
| } catch (err) { | ||
| console.error(`Failed to move file: ${err.message}`); | ||
| } | ||
|
Comment on lines
+57
to
+61
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. After throwing errors (or catching them), consider adding
Author
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. ok |
||
| } | ||
|
|
||
| moveFiles(); | ||
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.
Replace
console.error()+returnwiththrow new Error()- the task specification requires 'an error must be thrown' for invalid arguments.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 test will crash, I cant do this