Added rm.js
This commit is contained in:
		
							
								
								
									
										26
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								README.md
									
									
									
									
									
								
							@@ -18,6 +18,7 @@ A collection of scripts & information pertaining to the [open source](https://gi
 | 
			
		||||
    - [hacknet-manager.js](#hacknet-managerjs)
 | 
			
		||||
    - [miner.js](#minerjs)
 | 
			
		||||
    - [network-graph.js](#network-graphjs)
 | 
			
		||||
    - [rm.js](#rmjs)
 | 
			
		||||
    - [rootkit.js](#rootkitjs)
 | 
			
		||||
    - [server-manager.js](#server-managerjs)
 | 
			
		||||
    - [update.js](#updatejs)
 | 
			
		||||
@@ -304,6 +305,31 @@ Options:
 | 
			
		||||
	-h, --help              Display this help message
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
### [rm.js](./scripts/rm.js)
 | 
			
		||||
**RAM:** 2.85 GB
 | 
			
		||||
 | 
			
		||||
[BitBurner-Connector](https://plugins.jetbrains.com/plugin/18338-bitburner-connector) would occasionally push my IDE
 | 
			
		||||
files to the game, so I created this simple script to recursively search & delete files from a directory to save me 
 | 
			
		||||
from having to delete files one-by-one.
 | 
			
		||||
```
 | 
			
		||||
[home ~/scripts]> run /scripts/rm.js --help
 | 
			
		||||
Running script with 1 thread(s), pid 1 and args: ["--help"].
 | 
			
		||||
/scripts/rm.js: 
 | 
			
		||||
 | 
			
		||||
Recursively delete files inside a directory
 | 
			
		||||
 | 
			
		||||
Usage:	run rm.js [OPTIONS] PATH [SERVER]
 | 
			
		||||
	run rm.js --help 
 | 
			
		||||
 | 
			
		||||
	PATH                    Path to recursively search
 | 
			
		||||
	SERVER                  Run on remote server
 | 
			
		||||
 | 
			
		||||
Options:
 | 
			
		||||
	-f, --force             Remove game files (.exe, .lit, .msg)
 | 
			
		||||
	-r, --recursive         Delete everything inside directory
 | 
			
		||||
	-h, --help              Display this help message
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
### [rootkit.js](./scripts/rootkit.js)
 | 
			
		||||
**RAM:** 4.65 GB
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										39
									
								
								scripts/rm.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								scripts/rm.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
			
		||||
import {ArgParser} from '/scripts/lib/arg-parser';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Recursively delete files inside a path. Equivalent to the Unix "rm -r".
 | 
			
		||||
 * @param {NS} ns - BitBurner API
 | 
			
		||||
 */
 | 
			
		||||
export function main(ns) {
 | 
			
		||||
    // Setup
 | 
			
		||||
    ns.disableLog('ALL');
 | 
			
		||||
    const argParser = new ArgParser('rm.js', 'Recursively delete files inside a directory', [
 | 
			
		||||
        {name: 'path', desc: 'Path to recursively search'},
 | 
			
		||||
        {name: 'server', desc: 'Run on remote server', optional: true, default: ns.getHostname()},
 | 
			
		||||
        {name: 'force', desc: 'Remove game files (.exe, .lit, .msg)', flags: ['-f', '--force'], default: false},
 | 
			
		||||
        {name: 'recursive', desc: 'Delete everything inside directory', flags: ['-r', '--recursive'], default: true}
 | 
			
		||||
    ]);
 | 
			
		||||
    const args = argParser.parse(ns.args);
 | 
			
		||||
 | 
			
		||||
    // Help
 | 
			
		||||
    if(args['help'] || args['_error'].length)
 | 
			
		||||
        return ns.tprint(argParser.help(args['help'] ? null : args['_error'][0], args['_command']));
 | 
			
		||||
 | 
			
		||||
    // Run
 | 
			
		||||
    ns.ls(args['server'], args['path'])
 | 
			
		||||
        .filter(f => new RegExp(/\.(exe|lit|msg)$/g).test(f) ? args['force'] : true)
 | 
			
		||||
        .forEach(f => ns.rm(f, args['server']));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * BitBurner autocomplete.
 | 
			
		||||
 *
 | 
			
		||||
 * @param {{servers: string[], txts: string[], scripts: string[], flags: string[]}} data - Contextual information
 | 
			
		||||
 * @returns {string[]} - Pool of autocomplete options
 | 
			
		||||
 */
 | 
			
		||||
export function autocomplete(data) {
 | 
			
		||||
    return [...data.txts, ...data.scripts]
 | 
			
		||||
        .map(file => file.split('/').slice(0, -1).join('/'))
 | 
			
		||||
        .filter((path, i, arr) => arr.indexOf(path) == i)
 | 
			
		||||
        .concat(data.txts, data.scripts);
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user