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 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert code to standard code-style and merge two actions files with …
…1 CLI
- Loading branch information
1 parent
46e5b6a
commit 92e5353
Showing
12 changed files
with
132 additions
and
194 deletions.
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
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) => { | ||
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" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"commander": "^6.1.0" | ||
}, | ||
"bin": { | ||
"copyAndDelete": "./bin/cli.js" | ||
}, | ||
"devDependencies": { | ||
"standard": "^14.3.4" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.