From 46e5b6a6ce49669de0ce40fc9d17d1ae34670681 Mon Sep 17 00:00:00 2001 From: hyeongjun231 Date: Fri, 4 Sep 2020 18:42:07 +0900 Subject: [PATCH 1/3] add commander example, copy and remove file --- cli/commander/copyFile/README.md | 39 ++++++++++++++++++++++++++ cli/commander/copyFile/bin/cli.js | 21 ++++++++++++++ cli/commander/copyFile/lib/copyFile.js | 39 ++++++++++++++++++++++++++ cli/commander/copyFile/package.json | 17 +++++++++++ cli/commander/removeFile/README.md | 39 ++++++++++++++++++++++++++ cli/commander/removeFile/bin/cli.js | 21 ++++++++++++++ cli/commander/removeFile/lib/rimraf.js | 35 +++++++++++++++++++++++ cli/commander/removeFile/package.json | 17 +++++++++++ 8 files changed, 228 insertions(+) create mode 100644 cli/commander/copyFile/README.md create mode 100755 cli/commander/copyFile/bin/cli.js create mode 100644 cli/commander/copyFile/lib/copyFile.js create mode 100644 cli/commander/copyFile/package.json create mode 100644 cli/commander/removeFile/README.md create mode 100755 cli/commander/removeFile/bin/cli.js create mode 100644 cli/commander/removeFile/lib/rimraf.js create mode 100644 cli/commander/removeFile/package.json diff --git a/cli/commander/copyFile/README.md b/cli/commander/copyFile/README.md new file mode 100644 index 00000000..25de4867 --- /dev/null +++ b/cli/commander/copyFile/README.md @@ -0,0 +1,39 @@ +# copyFile + +This is a CLI built with [commander](https://www.npmjs.com/package/commander) that copies files to a specific directory path. + +## Usage + +Here is the output of running `node bin/cli.js -h` from the `copyFile` 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. +copyFile copy [file] [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. +copyFile copy [file] [path] # run the CLI +``` diff --git a/cli/commander/copyFile/bin/cli.js b/cli/commander/copyFile/bin/cli.js new file mode 100755 index 00000000..4f655278 --- /dev/null +++ b/cli/commander/copyFile/bin/cli.js @@ -0,0 +1,21 @@ +#!/usr/bin/env node +const { program } = require('commander'); + +const copyFile = require('../lib/copyFile'); + +program.version('0.0.1', '-v, --version').usage('[options]'); + +program + .command('copy ') + .usage(' ') + .description('Copy file.') + .action((name, direcotry) => { + copyFile(name, direcotry); + }); + +program.command('*', { noHelp: true }).action(() => { + console.log('Command not found'); + program.help(); +}); + +program.parse(process.argv); diff --git a/cli/commander/copyFile/lib/copyFile.js b/cli/commander/copyFile/lib/copyFile.js new file mode 100644 index 00000000..cfb85522 --- /dev/null +++ b/cli/commander/copyFile/lib/copyFile.js @@ -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; diff --git a/cli/commander/copyFile/package.json b/cli/commander/copyFile/package.json new file mode 100644 index 00000000..17cf98a5 --- /dev/null +++ b/cli/commander/copyFile/package.json @@ -0,0 +1,17 @@ +{ + "name": "copyfile", + "version": "1.0.0", + "description": "Copies files to a specific directory path.", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "commander": "^6.1.0" + }, + "bin": { + "copyFile": "./bin/cli.js" + } +} diff --git a/cli/commander/removeFile/README.md b/cli/commander/removeFile/README.md new file mode 100644 index 00000000..2e804a1e --- /dev/null +++ b/cli/commander/removeFile/README.md @@ -0,0 +1,39 @@ +# copyFile + +This is a CLI built with [commander](https://www.npmjs.com/package/commander) that deletes the specified path and its sub-files/folders.. + +## Usage + +Here is the output of running `node bin/cli.js -h` from the `removeFile` directory: + +```bash +Usage: cli.js [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. +removeFile 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. +removeFile rimraf [path] # run the CLI +``` diff --git a/cli/commander/removeFile/bin/cli.js b/cli/commander/removeFile/bin/cli.js new file mode 100755 index 00000000..d7eb334f --- /dev/null +++ b/cli/commander/removeFile/bin/cli.js @@ -0,0 +1,21 @@ +#!/usr/bin/env node +const { program } = require('commander'); + +const rimraf = require('../lib/rimraf'); + +program.version('0.0.1', '-v, --version').usage('[options]'); + +program + .command('rimraf ') + .usage('') + .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); diff --git a/cli/commander/removeFile/lib/rimraf.js b/cli/commander/removeFile/lib/rimraf.js new file mode 100644 index 00000000..0c1fd75b --- /dev/null +++ b/cli/commander/removeFile/lib/rimraf.js @@ -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; diff --git a/cli/commander/removeFile/package.json b/cli/commander/removeFile/package.json new file mode 100644 index 00000000..adba7b4b --- /dev/null +++ b/cli/commander/removeFile/package.json @@ -0,0 +1,17 @@ +{ + "name": "removefile", + "version": "1.0.0", + "description": "Deletes the specified path and its sub-files/folders.", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "commander": "^6.1.0" + }, + "bin": { + "removeFile": "./bin/cli.js" + } +} From 92e535353385edbff7248c0ef36582b231be78c5 Mon Sep 17 00:00:00 2001 From: hyeongjun231 Date: Thu, 1 Oct 2020 17:36:44 +0900 Subject: [PATCH 2/3] Convert code to standard code-style and merge two actions files with 1 CLI --- .../{copyFile => copyAndDelete}/README.md | 12 +++--- cli/commander/copyAndDelete/bin/cli.js | 30 ++++++++++++++ cli/commander/copyAndDelete/lib/copyFile.js | 39 +++++++++++++++++++ cli/commander/copyAndDelete/lib/rimraf.js | 35 +++++++++++++++++ cli/commander/copyAndDelete/package.json | 21 ++++++++++ cli/commander/copyFile/bin/cli.js | 21 ---------- cli/commander/copyFile/lib/copyFile.js | 39 ------------------- cli/commander/copyFile/package.json | 17 -------- cli/commander/removeFile/README.md | 39 ------------------- cli/commander/removeFile/bin/cli.js | 21 ---------- cli/commander/removeFile/lib/rimraf.js | 35 ----------------- cli/commander/removeFile/package.json | 17 -------- 12 files changed, 132 insertions(+), 194 deletions(-) rename cli/commander/{copyFile => copyAndDelete}/README.md (72%) create mode 100755 cli/commander/copyAndDelete/bin/cli.js create mode 100644 cli/commander/copyAndDelete/lib/copyFile.js create mode 100644 cli/commander/copyAndDelete/lib/rimraf.js create mode 100644 cli/commander/copyAndDelete/package.json delete mode 100755 cli/commander/copyFile/bin/cli.js delete mode 100644 cli/commander/copyFile/lib/copyFile.js delete mode 100644 cli/commander/copyFile/package.json delete mode 100644 cli/commander/removeFile/README.md delete mode 100755 cli/commander/removeFile/bin/cli.js delete mode 100644 cli/commander/removeFile/lib/rimraf.js delete mode 100644 cli/commander/removeFile/package.json diff --git a/cli/commander/copyFile/README.md b/cli/commander/copyAndDelete/README.md similarity index 72% rename from cli/commander/copyFile/README.md rename to cli/commander/copyAndDelete/README.md index 25de4867..8e220f83 100644 --- a/cli/commander/copyFile/README.md +++ b/cli/commander/copyAndDelete/README.md @@ -1,10 +1,10 @@ -# copyFile +# copyAndDelete -This is a CLI built with [commander](https://www.npmjs.com/package/commander) that copies files to a specific directory path. +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 `copyFile` directory: +Here is the output of running `node bin/cli.js -h` from the `copyAndDelete` directory: ```bash 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 ```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. -copyFile copy [file] [path] # run the CLI +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. -copyFile copy [file] [path] # run the CLI +copyAndDelete copy [file] [path] # run the CLI +copyAndDelete rimraf [file] # run the CLI ``` diff --git a/cli/commander/copyAndDelete/bin/cli.js b/cli/commander/copyAndDelete/bin/cli.js new file mode 100755 index 00000000..a8cce833 --- /dev/null +++ b/cli/commander/copyAndDelete/bin/cli.js @@ -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 ') + .usage(' ') + .description('Copy file.') + .action((name, direcotry) => { + copyFile(name, direcotry) + }) + +program + .command('rimraf ') + .usage('') + .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) diff --git a/cli/commander/copyAndDelete/lib/copyFile.js b/cli/commander/copyAndDelete/lib/copyFile.js new file mode 100644 index 00000000..23e2716e --- /dev/null +++ b/cli/commander/copyAndDelete/lib/copyFile.js @@ -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 diff --git a/cli/commander/copyAndDelete/lib/rimraf.js b/cli/commander/copyAndDelete/lib/rimraf.js new file mode 100644 index 00000000..38d63700 --- /dev/null +++ b/cli/commander/copyAndDelete/lib/rimraf.js @@ -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 diff --git a/cli/commander/copyAndDelete/package.json b/cli/commander/copyAndDelete/package.json new file mode 100644 index 00000000..c9e993a5 --- /dev/null +++ b/cli/commander/copyAndDelete/package.json @@ -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" + } +} diff --git a/cli/commander/copyFile/bin/cli.js b/cli/commander/copyFile/bin/cli.js deleted file mode 100755 index 4f655278..00000000 --- a/cli/commander/copyFile/bin/cli.js +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env node -const { program } = require('commander'); - -const copyFile = require('../lib/copyFile'); - -program.version('0.0.1', '-v, --version').usage('[options]'); - -program - .command('copy ') - .usage(' ') - .description('Copy file.') - .action((name, direcotry) => { - copyFile(name, direcotry); - }); - -program.command('*', { noHelp: true }).action(() => { - console.log('Command not found'); - program.help(); -}); - -program.parse(process.argv); diff --git a/cli/commander/copyFile/lib/copyFile.js b/cli/commander/copyFile/lib/copyFile.js deleted file mode 100644 index cfb85522..00000000 --- a/cli/commander/copyFile/lib/copyFile.js +++ /dev/null @@ -1,39 +0,0 @@ -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; diff --git a/cli/commander/copyFile/package.json b/cli/commander/copyFile/package.json deleted file mode 100644 index 17cf98a5..00000000 --- a/cli/commander/copyFile/package.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "name": "copyfile", - "version": "1.0.0", - "description": "Copies files to a specific directory path.", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "", - "license": "ISC", - "dependencies": { - "commander": "^6.1.0" - }, - "bin": { - "copyFile": "./bin/cli.js" - } -} diff --git a/cli/commander/removeFile/README.md b/cli/commander/removeFile/README.md deleted file mode 100644 index 2e804a1e..00000000 --- a/cli/commander/removeFile/README.md +++ /dev/null @@ -1,39 +0,0 @@ -# copyFile - -This is a CLI built with [commander](https://www.npmjs.com/package/commander) that deletes the specified path and its sub-files/folders.. - -## Usage - -Here is the output of running `node bin/cli.js -h` from the `removeFile` directory: - -```bash -Usage: cli.js [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. -removeFile 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. -removeFile rimraf [path] # run the CLI -``` diff --git a/cli/commander/removeFile/bin/cli.js b/cli/commander/removeFile/bin/cli.js deleted file mode 100755 index d7eb334f..00000000 --- a/cli/commander/removeFile/bin/cli.js +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env node -const { program } = require('commander'); - -const rimraf = require('../lib/rimraf'); - -program.version('0.0.1', '-v, --version').usage('[options]'); - -program - .command('rimraf ') - .usage('') - .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); diff --git a/cli/commander/removeFile/lib/rimraf.js b/cli/commander/removeFile/lib/rimraf.js deleted file mode 100644 index 0c1fd75b..00000000 --- a/cli/commander/removeFile/lib/rimraf.js +++ /dev/null @@ -1,35 +0,0 @@ -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; diff --git a/cli/commander/removeFile/package.json b/cli/commander/removeFile/package.json deleted file mode 100644 index adba7b4b..00000000 --- a/cli/commander/removeFile/package.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "name": "removefile", - "version": "1.0.0", - "description": "Deletes the specified path and its sub-files/folders.", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "", - "license": "ISC", - "dependencies": { - "commander": "^6.1.0" - }, - "bin": { - "removeFile": "./bin/cli.js" - } -} From b0b68bf31f3ee8ab4e2cc2ba1a87f854d12a2109 Mon Sep 17 00:00:00 2001 From: hyeongjun231 Date: Mon, 5 Oct 2020 19:47:45 +0900 Subject: [PATCH 3/3] Move exist funtion into separte module for reusing --- cli/commander/copyAndDelete/lib/copyFile.js | 13 +------------ cli/commander/copyAndDelete/lib/rimraf.js | 13 +------------ cli/commander/copyAndDelete/lib/util.js | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 24 deletions(-) create mode 100644 cli/commander/copyAndDelete/lib/util.js diff --git a/cli/commander/copyAndDelete/lib/copyFile.js b/cli/commander/copyAndDelete/lib/copyFile.js index 23e2716e..c6387aa6 100644 --- a/cli/commander/copyAndDelete/lib/copyFile.js +++ b/cli/commander/copyAndDelete/lib/copyFile.js @@ -1,17 +1,6 @@ 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 { exist } = require('./util') const mkdirp = (dir) => { const dirname = path diff --git a/cli/commander/copyAndDelete/lib/rimraf.js b/cli/commander/copyAndDelete/lib/rimraf.js index 38d63700..bf0a6093 100644 --- a/cli/commander/copyAndDelete/lib/rimraf.js +++ b/cli/commander/copyAndDelete/lib/rimraf.js @@ -1,17 +1,6 @@ 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 { exist } = require('./util') const rimraf = (p) => { if (exist(p)) { diff --git a/cli/commander/copyAndDelete/lib/util.js b/cli/commander/copyAndDelete/lib/util.js new file mode 100644 index 00000000..52d57c11 --- /dev/null +++ b/cli/commander/copyAndDelete/lib/util.js @@ -0,0 +1,17 @@ +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. + } +} + +module.exports = { + exist +}