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

add commander example, copy and remove file #27

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions cli/commander/copyFile/README.md
Original file line number Diff line number Diff line change
@@ -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
```
21 changes: 21 additions & 0 deletions cli/commander/copyFile/bin/cli.js
Original file line number Diff line number Diff line change
@@ -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 <name> <directory>')
.usage('<name> <directory>')
.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);
39 changes: 39 additions & 0 deletions cli/commander/copyFile/lib/copyFile.js
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;
17 changes: 17 additions & 0 deletions cli/commander/copyFile/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
39 changes: 39 additions & 0 deletions cli/commander/removeFile/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# copyFile
hyeongjun231 marked this conversation as resolved.
Show resolved Hide resolved

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
```
21 changes: 21 additions & 0 deletions cli/commander/removeFile/bin/cli.js
Original file line number Diff line number Diff line change
@@ -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 <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);
35 changes: 35 additions & 0 deletions cli/commander/removeFile/lib/rimraf.js
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;
17 changes: 17 additions & 0 deletions cli/commander/removeFile/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}