-
Notifications
You must be signed in to change notification settings - Fork 438
add solution #373
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?
add solution #373
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,61 @@ | ||
| // write code here | ||
| /* eslint no-console: ["error", { allow: ["warn", "error"] }] */ | ||
|
|
||
| const fs = require('fs/promises'); | ||
| const path = require('node:path'); | ||
|
|
||
| const sourceFile = process.argv[2]; | ||
| const destination = process.argv[3]; | ||
|
|
||
| if (process.argv.length <= 2) { | ||
| console.error('Zero argument is provided'); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| if (process.argv.length === 3) { | ||
| console.error('Only one argument is provided'); | ||
| process.exit(0); | ||
|
Comment on lines
+14
to
+16
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. Same as above: this is an error path but exits with code 0. Update to a non-zero code to align with correct CLI behavior. |
||
| } | ||
|
Comment on lines
+9
to
+17
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 current argument validation only checks the number of arguments, but the assignment also requires specific behavior based on the form of the destination (e.g. ending with |
||
|
|
||
| const filePath = path.join(destination, path.basename(sourceFile)); | ||
|
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.
|
||
|
|
||
| async function move(file, to, fullPath) { | ||
| let isDirectory = false; | ||
|
|
||
| try { | ||
| const stats = await fs.stat(to); | ||
|
|
||
| isDirectory = stats.isDirectory(); | ||
| } catch { | ||
| isDirectory = false; | ||
| } | ||
|
|
||
| if (to.endsWith('/') || to.endsWith('\\')) { | ||
| try { | ||
| if (isDirectory) { | ||
| await fs.rename(file, fullPath); | ||
| } else { | ||
| console.error('directory is not exist'); | ||
| process.exit(0); | ||
|
Comment on lines
+34
to
+38
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 message |
||
| } | ||
| } catch (err) { | ||
| console.error('Something went wrong'); | ||
| process.exit(0); | ||
|
Comment on lines
+40
to
+42
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. For unexpected errors you should not exit with code 0; use a non-zero exit code or let the error propagate so the process fails properly. |
||
| } | ||
| } else if (isDirectory) { | ||
| try { | ||
| await fs.rename(file, fullPath); | ||
| } catch { | ||
| console.error('directory does not exist'); | ||
| process.exit(0); | ||
|
Comment on lines
+45
to
+49
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 branch reports |
||
| } | ||
| } else { | ||
| try { | ||
| await fs.rename(file, to); | ||
| } catch { | ||
| console.error('Source file is not exist'); | ||
|
Comment on lines
+37
to
+55
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. Several error messages here ( |
||
| process.exit(0); | ||
|
Comment on lines
+10
to
+56
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. All error cases currently
Comment on lines
+52
to
+56
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.
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| move(sourceFile, destination, filePath); | ||
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.
Using
process.exit(0)for an error case signals success to the shell. For invalid argument counts you should use a non-zero exit code (e.g.process.exit(1)) so scripts can detect the failure.