Skip to content

Commit

Permalink
Add verbose option and -v optional perameter
Browse files Browse the repository at this point in the history
  • Loading branch information
noahweasley committed Aug 9, 2023
1 parent 975e7f2 commit 507f5c1
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 35 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@ npm install clean-sweep -g
### Usage (as a script)

```bash
clean-sweep <path/to/directory>
clean-sweep <path/to/directory> -v
# OR
csw <path/to/directory>
csw <path/to/directory> -v

```

**The first argument is the path to the directory in which the operation would be performed**
- The first argument is the path to the directory in which the operation would be performed, and is required!
- The remaining are optional, -v parameter prints out the operation of the sweep function to console if specifies

### Example

```bash
clean-sweep ./music
clean-sweep ./music -v
# OR
csw ./music
csw ./music -v
```

### Installation (in code)
Expand All @@ -42,7 +45,7 @@ const cleanSweep = require("clean-sweep");
const fileDirectory = "<replace file directory here>";

try {
cleanSweep.sweep(fileDirectory);
cleanSweep.sweep(fileDirectory, { verbose: true });
} catch (err) {
if (err.code == "ENOENT") {
console.error("Directory does not exist");
Expand Down
26 changes: 23 additions & 3 deletions bin/exec-sweep.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
#!/usr/bin/env node
const cleanSweep = require("../lib/sweep");
let fileDirectory = process.argv[2];
const VALID_PARAMS = ["-v"];
let params = process.argv.slice(3, process.argv.length);

if (!fileDirectory) {
return console.error(
"Please specify the target file directory!\n\nUsage: clean-sweep <directory> | csw <directory>"
"Please specify the target file directory!" +
"\n\n" +
"Usage: clean-sweep <directory> -v | csw <directory> -v" +
"\n\n" +
"Parameters:" +
" \n\n" +
" -v: Prints operation to console"
);
} else if (!params.every((param) => VALID_PARAMS.includes(param))) {
return console.error(
"Invalid parameter!" +
"\n\n" +
"Valid parameters:" +
" \n\n" +
" -v: Prints operation to console"
);
} else {
try {
cleanSweep.sweep(fileDirectory);
if (params.includes("-v")) {
cleanSweep.sweep(fileDirectory, { verbose: true });
} else {
cleanSweep.sweep(fileDirectory);
}
} catch (err) {
if (err.code == "ENOENT") {
console.error("Directory does not exist");
console.error(`Directory "${fileDirectory}" does not exist`);
} else {
console.error(err.message);
}
Expand Down
4 changes: 2 additions & 2 deletions example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const cleanSweep = require("../lib/sweep");
const fileDirectory = "./";

try {
cleanSweep.sweep(fileDirectory);
cleanSweep.sweep(fileDirectory, { verbose: true });
} catch (err) {
if (err.code == "ENOENT") {
console.error("Directory does not exist");
} else {
console.error(err.message);
}
}
}
21 changes: 0 additions & 21 deletions lib/files.js

This file was deleted.

31 changes: 27 additions & 4 deletions lib/sweep.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const fs = require("fs");
const path = require("path");
const { getFilesRecursive } = require("./files");

/**
* When executed, deletes all empty files and folders in a target directory
*
* @param {string} dir the directory to clean sweep
* @param {JSON} params the additional optional parameters. Valid params are: [ -v ].
* @throws error if couldn't complete operation
*/
module.exports.sweep = function (dir) {
module.exports.sweep = function (dir, params) {
if (!dir) {
throw new Error("Please specify the target file directory!");
} else {
Expand All @@ -17,11 +17,34 @@ module.exports.sweep = function (dir) {

return getFilesRecursive(dir, (file) => {
const fileSize = fs.statSync(file).size;
console.log(`${file} - ${fileSize} bytes (skipping)`);
if (params && params.verbose) {
console.log(`${file} - ${fileSize} bytes (skipping)`);
}
if (fileSize <= 0) {
// force delete all files and folders
fs.unlinkSync(file);
console.log(`\nDeleted ${path.basename(file)} successfully\n`);
if (params && params.verbose) {
console.log(`\nDeleted ${path.basename(file)} successfully\n`);
}
}
});
};

/**
* Gets all files in the directory, passing them in a callback
*
* @param {string} dir the directory to retrieve files from
* @param {Function} callbackfn action to be performed on each file in directory
*/
function getFilesRecursive(dir, callbackfn) {
const files = fs.readdirSync(dir, { recursive: true });

files.forEach((file) => {
const absoluteFile = path.join(dir, file);
const isFile = fs.statSync(absoluteFile).isFile();

isFile
? callbackfn(absoluteFile)
: getFilesRecursive(absoluteFile, callbackfn);
});
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clean-sweep",
"version": "0.0.3",
"version": "0.0.4",
"description": "Deletes all empty files and folders in a target directory",
"main": "./lib/sweep.js",
"bin": {
Expand Down

0 comments on commit 507f5c1

Please sign in to comment.