This repository has been archived by the owner on Jun 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 345
add commander example, copy and remove file #27
Open
hyeongjun231
wants to merge
3
commits into
nodejs:main
Choose a base branch
from
hyeongjun231:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# copyAndDelete | ||
|
||
This is a CLI built with [commander](https://www.npmjs.com/package/commander). You can copy files to a specific directory path and delete the specified path and its sub-files/folders. | ||
|
||
## Usage | ||
|
||
Here is the output of running `node bin/cli.js -h` from the `copyAndDelete` directory: | ||
|
||
```bash | ||
Usage: cli.js [file] [path] | ||
|
||
Options: | ||
--help, -h Show help [boolean] | ||
--version, v Show version number [boolean] | ||
``` | ||
|
||
### As a Local Project | ||
|
||
You'll want to start by installing dependencies: | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
Since this is a CLI, you'll probably want to get it available on your PATH. There's two ways you can do this: | ||
|
||
**Via global installation:** | ||
|
||
```bash | ||
npm i -g . # this will install the current working directory as a global module. you will want to do npm uninstall -g . to uninstall it. | ||
copyAndDelete copy [file] [path] # run the CLI | ||
copyAndDelete rimraf [path] # run the CLI | ||
``` | ||
|
||
**Via symlink:** | ||
|
||
```bash | ||
npm link # this will create a symlink that makes the module act as a global module. npm unlink to remove this. | ||
copyAndDelete copy [file] [path] # run the CLI | ||
copyAndDelete rimraf [file] # run the CLI | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env node | ||
const { program } = require('commander') | ||
|
||
const copyFile = require('../lib/copyFile') | ||
const rimraf = require('../lib/rimraf') | ||
|
||
program.version('0.0.1', '-v, --version').usage('[options]') | ||
|
||
program | ||
.command('copy <name> <directory>') | ||
.usage('<name> <directory>') | ||
.description('Copy file.') | ||
.action((name, direcotry) => { | ||
copyFile(name, direcotry) | ||
}) | ||
|
||
program | ||
.command('rimraf <path>') | ||
.usage('<path>') | ||
.description('Deletes the specified path and its sub-files/folders.') | ||
.action((path) => { | ||
rimraf(path) | ||
}) | ||
|
||
program.command('*', { noHelp: true }).action(() => { | ||
console.log('Command not found') | ||
program.help() | ||
}) | ||
|
||
program.parse(process.argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const path = require('path') | ||
const fs = require('fs') | ||
|
||
const exist = (dir) => { | ||
try { | ||
fs.accessSync( | ||
dir, | ||
fs.constants.F_OK | fs.constants.R_OK | fs.constants.W_OK | ||
) // tests a user's permissions for the file or directory specified by path | ||
return true // if dir exists, return true. | ||
} catch (e) { | ||
return false // if dir not exists, return false. | ||
} | ||
} | ||
|
||
const mkdirp = (dir) => { | ||
const dirname = path | ||
.relative('.', path.normalize(dir)) | ||
.split(path.sep) | ||
.filter((p) => !!p) | ||
dirname.forEach((d, idx) => { | ||
const pathBuilder = dirname.slice(0, idx + 1).join(path.sep) | ||
if (!exist(pathBuilder)) { | ||
fs.mkdirSync(pathBuilder) // make directory | ||
} | ||
}) | ||
} | ||
|
||
const copyFile = (name, directory) => { | ||
if (exist(name)) { | ||
mkdirp(directory) | ||
fs.copyFileSync(name, path.join(directory, name)) | ||
console.log(`${name} file is copied.`) | ||
} else { | ||
console.error('File does not exist.') | ||
} | ||
} | ||
|
||
module.exports = copyFile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
const exist = (dir) => { | ||
try { | ||
fs.accessSync( | ||
dir, | ||
fs.constants.F_OK | fs.constants.R_OK | fs.constants.W_OK | ||
) | ||
return true | ||
} catch (e) { | ||
return false | ||
} | ||
} | ||
|
||
const rimraf = (p) => { | ||
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. Variable names such as |
||
if (exist(p)) { | ||
try { | ||
const dir = fs.readdirSync(p) | ||
console.log(dir) | ||
dir.forEach((d) => { | ||
rimraf(path.join(p, d)) | ||
}) | ||
fs.rmdirSync(p) | ||
console.log(`Remove ${p} folder success`) | ||
} catch (e) { | ||
fs.unlinkSync(p) | ||
console.log(`Remove ${p} file success`) | ||
} | ||
} else { | ||
console.error('No such file or directory') | ||
} | ||
} | ||
|
||
module.exports = rimraf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "copy-and-delete", | ||
"version": "1.0.0", | ||
"description": "You can copy files to a specific directory path or delete specific files and directory.", | ||
"main": "index.js", | ||
"scripts": { | ||
"lint": "standard", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
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. Please add some unit tests |
||
}, | ||
"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. Include Author Name |
||
"license": "ISC", | ||
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. Use "MIT" License please |
||
"dependencies": { | ||
"commander": "^6.1.0" | ||
}, | ||
"bin": { | ||
"copyAndDelete": "./bin/cli.js" | ||
}, | ||
"devDependencies": { | ||
"standard": "^14.3.4" | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'd suggest to move this function into separate module and reuse in both command files
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.
Okay it's good i will change the code. thank you 👍