-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
136 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Changelog | ||
|
||
## 1.0.0 - [2020-08-26] | ||
|
||
- first release |
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 @@ | ||
# deno_thanos | ||
|
||
[![tag](https://img.shields.io/github/release/justjavac/deno_thanos)](https://github.com/justjavac/deno_thanos/releases) | ||
[![Build Status](https://github.com/justjavac/deno_thanos/workflows/ci/badge.svg?branch=master)](https://github.com/justjavac/deno_thanos/actions) | ||
[![license](https://img.shields.io/github/license/justjavac/deno_thanos)](https://github.com/justjavac/deno_thanos/blob/master/LICENSE) | ||
|
||
This command could delete a half of your files randomly. | ||
|
||
## Install | ||
|
||
```bash | ||
deno install --allow-read --allow-write --unstable --name thanos https://deno.land/x/thanos/mod.ts | ||
``` | ||
|
||
## Usage | ||
|
||
```bash | ||
thanos --help | ||
``` | ||
|
||
OR, use without install: | ||
|
||
```bash | ||
cd node_modules | ||
deno run --allow-read --allow-write --unstable https://deno.land/x/thanos/mod.ts --help | ||
``` | ||
|
||
## License | ||
|
||
[deno_thanos](https://github.com/justjavac/deno_thanos) is released under the MIT License. See the bundled [LICENSE](./LICENSE) file for details. |
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,101 @@ | ||
#!/usr/bin/env deno run --allow-read --allow-write | ||
|
||
import { parse } from "https://deno.land/[email protected]/flags/mod.ts"; | ||
import { walk } from "https://deno.land/[email protected]/fs/mod.ts"; | ||
import { encode } from "https://deno.land/[email protected]/encoding/utf8.ts"; | ||
import { yellow, cyan } from "https://deno.land/[email protected]/fmt/colors.ts"; | ||
|
||
import ProgressBar from "https://deno.land/x/[email protected]/mod.ts"; | ||
import shuffle from "https://deno.land/x/shuffle/mod.ts"; | ||
|
||
const args = parse(Deno.args, { | ||
boolean: true, | ||
alias: { | ||
force: "f", | ||
help: "h", | ||
}, | ||
}); | ||
|
||
const helpMsg = `This command could delete list half your files randomly. | ||
USAGE: | ||
thanos [FLAGS] | ||
FLAGS: | ||
-h, --help Prints help information | ||
-f, --force Force delete | ||
`; | ||
|
||
if (args.help) { | ||
print(helpMsg); | ||
Deno.exit(0); | ||
} | ||
|
||
print("Thanos is comming"); | ||
await printDots(4, 2000); | ||
|
||
print(`Thanos is scanning your directory ${cyan(Deno.cwd())} `); | ||
await printDots(6, 3000); | ||
|
||
async function getFilesNames(): Promise<string[]> { | ||
const filePaths: string[] = []; | ||
for await (const entry of walk(".")) { | ||
if (entry.isFile) { | ||
filePaths.push(entry.path); | ||
} | ||
} | ||
return filePaths; | ||
} | ||
|
||
const files = await getFilesNames(); | ||
const deleteCount = Math.floor(files.length / 2); | ||
|
||
console.log(`Thanos has found ${yellow(String(files.length))} files`); | ||
await delay(300); | ||
console.log(`Thanos will delete ${yellow(String(deleteCount))} files`); | ||
await delay(300); | ||
|
||
if (!args.force) { | ||
console.log(`Good luck! Thanos is not run with flag '${cyan("--force")}'.`); | ||
console.log( | ||
`If you really want Thanos to delete half of the files, please run '${ | ||
cyan("thanos --force") | ||
}'`, | ||
); | ||
Deno.exit(0); | ||
} | ||
|
||
const progress = new ProgressBar({ | ||
title: "deleting:", | ||
total: deleteCount, | ||
}); | ||
|
||
const filesWillDeleted = shuffle(files).slice(0, deleteCount); | ||
|
||
for (let i = 0; i < deleteCount; i++) { | ||
await Deno.remove(filesWillDeleted[i]); | ||
progress.render(i); | ||
} | ||
|
||
progress.render(deleteCount); | ||
print("\n"); | ||
|
||
function print(str: string): void { | ||
Deno.stdout.writeSync(encode(str)); | ||
} | ||
|
||
async function printDots(dots: number, ms = 1000): Promise<void> { | ||
for (let i = 0; i < dots; i++) { | ||
print("."); | ||
await delay(ms / dots); | ||
} | ||
print("\n"); | ||
} | ||
|
||
function delay(ms: number): Promise<void> { | ||
return new Promise((r) => { | ||
setTimeout(() => { | ||
r(); | ||
}, ms); | ||
}); | ||
} |