Skip to content
This repository was archived by the owner on Jun 22, 2024. It is now read-only.

Commit 92e5353

Browse files
committed
Convert code to standard code-style and merge two actions files with 1 CLI
1 parent 46e5b6a commit 92e5353

12 files changed

Lines changed: 132 additions & 194 deletions

File tree

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# copyFile
1+
# copyAndDelete
22

3-
This is a CLI built with [commander](https://www.npmjs.com/package/commander) that copies files to a specific directory path.
3+
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.
44

55
## Usage
66

7-
Here is the output of running `node bin/cli.js -h` from the `copyFile` directory:
7+
Here is the output of running `node bin/cli.js -h` from the `copyAndDelete` directory:
88

99
```bash
1010
Usage: cli.js [file] [path]
@@ -28,12 +28,14 @@ Since this is a CLI, you'll probably want to get it available on your PATH. Ther
2828

2929
```bash
3030
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.
31-
copyFile copy [file] [path] # run the CLI
31+
copyAndDelete copy [file] [path] # run the CLI
32+
copyAndDelete rimraf [path] # run the CLI
3233
```
3334

3435
**Via symlink:**
3536

3637
```bash
3738
npm link # this will create a symlink that makes the module act as a global module. npm unlink to remove this.
38-
copyFile copy [file] [path] # run the CLI
39+
copyAndDelete copy [file] [path] # run the CLI
40+
copyAndDelete rimraf [file] # run the CLI
3941
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env node
2+
const { program } = require('commander')
3+
4+
const copyFile = require('../lib/copyFile')
5+
const rimraf = require('../lib/rimraf')
6+
7+
program.version('0.0.1', '-v, --version').usage('[options]')
8+
9+
program
10+
.command('copy <name> <directory>')
11+
.usage('<name> <directory>')
12+
.description('Copy file.')
13+
.action((name, direcotry) => {
14+
copyFile(name, direcotry)
15+
})
16+
17+
program
18+
.command('rimraf <path>')
19+
.usage('<path>')
20+
.description('Deletes the specified path and its sub-files/folders.')
21+
.action((path) => {
22+
rimraf(path)
23+
})
24+
25+
program.command('*', { noHelp: true }).action(() => {
26+
console.log('Command not found')
27+
program.help()
28+
})
29+
30+
program.parse(process.argv)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const path = require('path')
2+
const fs = require('fs')
3+
4+
const exist = (dir) => {
5+
try {
6+
fs.accessSync(
7+
dir,
8+
fs.constants.F_OK | fs.constants.R_OK | fs.constants.W_OK
9+
) // tests a user's permissions for the file or directory specified by path
10+
return true // if dir exists, return true.
11+
} catch (e) {
12+
return false // if dir not exists, return false.
13+
}
14+
}
15+
16+
const mkdirp = (dir) => {
17+
const dirname = path
18+
.relative('.', path.normalize(dir))
19+
.split(path.sep)
20+
.filter((p) => !!p)
21+
dirname.forEach((d, idx) => {
22+
const pathBuilder = dirname.slice(0, idx + 1).join(path.sep)
23+
if (!exist(pathBuilder)) {
24+
fs.mkdirSync(pathBuilder) // make directory
25+
}
26+
})
27+
}
28+
29+
const copyFile = (name, directory) => {
30+
if (exist(name)) {
31+
mkdirp(directory)
32+
fs.copyFileSync(name, path.join(directory, name))
33+
console.log(`${name} file is copied.`)
34+
} else {
35+
console.error('File does not exist.')
36+
}
37+
}
38+
39+
module.exports = copyFile
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
4+
const exist = (dir) => {
5+
try {
6+
fs.accessSync(
7+
dir,
8+
fs.constants.F_OK | fs.constants.R_OK | fs.constants.W_OK
9+
)
10+
return true
11+
} catch (e) {
12+
return false
13+
}
14+
}
15+
16+
const rimraf = (p) => {
17+
if (exist(p)) {
18+
try {
19+
const dir = fs.readdirSync(p)
20+
console.log(dir)
21+
dir.forEach((d) => {
22+
rimraf(path.join(p, d))
23+
})
24+
fs.rmdirSync(p)
25+
console.log(`Remove ${p} folder success`)
26+
} catch (e) {
27+
fs.unlinkSync(p)
28+
console.log(`Remove ${p} file success`)
29+
}
30+
} else {
31+
console.error('No such file or directory')
32+
}
33+
}
34+
35+
module.exports = rimraf
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "copy-and-delete",
3+
"version": "1.0.0",
4+
"description": "You can copy files to a specific directory path or delete specific files and directory.",
5+
"main": "index.js",
6+
"scripts": {
7+
"lint": "standard",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"commander": "^6.1.0"
14+
},
15+
"bin": {
16+
"copyAndDelete": "./bin/cli.js"
17+
},
18+
"devDependencies": {
19+
"standard": "^14.3.4"
20+
}
21+
}

cli/commander/copyFile/bin/cli.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

cli/commander/copyFile/lib/copyFile.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

cli/commander/copyFile/package.json

Lines changed: 0 additions & 17 deletions
This file was deleted.

cli/commander/removeFile/README.md

Lines changed: 0 additions & 39 deletions
This file was deleted.

cli/commander/removeFile/bin/cli.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)