diff --git a/cli/commander/copyAndDelete/README.md b/cli/commander/copyAndDelete/README.md new file mode 100644 index 00000000..8e220f83 --- /dev/null +++ b/cli/commander/copyAndDelete/README.md @@ -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 +``` 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..c6387aa6 --- /dev/null +++ b/cli/commander/copyAndDelete/lib/copyFile.js @@ -0,0 +1,28 @@ +const path = require('path') +const fs = require('fs') +const { exist } = require('./util') + +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..bf0a6093 --- /dev/null +++ b/cli/commander/copyAndDelete/lib/rimraf.js @@ -0,0 +1,24 @@ +const fs = require('fs') +const path = require('path') +const { exist } = require('./util') + +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/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 +} 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" + } +}