[refactor, chore] Including TSLint; fixing most things.

As it currently stands, bringing in TSLint with *all* rules enabled by
default and override each as we go. This means that as we upgrade and
bring in new rules we might start seeing new rules that the code
violates.

Because all the rules are enabled by default, the choice to explicitly
disable/relax rules can be discussed as part of PRs.

This also brought in strict type checking. This means we shouldn't be
relying on truthy/falsey value coercion. It also means that we will
likely need to create some type definition files or port certain files
over. That...will be a bit tedious, but will make the codebase better as
a whole.
This commit is contained in:
Steven Evans 2018-06-14 14:57:44 -04:00
parent 67cee8642b
commit 9c09e44db2
8 changed files with 312 additions and 86 deletions

87
package-lock.json generated

@ -9275,6 +9275,93 @@
"integrity": "sha512-AVP5Xol3WivEr7hnssHDsaM+lVrVXWUvd1cfXTRkTj80b//6g2wIFEH6hZG0muGZRnHGrfttpdzRk3YlBkWjKw==",
"dev": true
},
"tslint": {
"version": "5.10.0",
"resolved": "https://registry.npmjs.org/tslint/-/tslint-5.10.0.tgz",
"integrity": "sha1-EeJrzLiK+gLdDZlWyuPUVAtfVMM=",
"dev": true,
"requires": {
"babel-code-frame": "6.26.0",
"builtin-modules": "1.1.1",
"chalk": "2.4.1",
"commander": "2.14.1",
"diff": "3.5.0",
"glob": "7.1.2",
"js-yaml": "3.12.0",
"minimatch": "3.0.4",
"resolve": "1.5.0",
"semver": "5.5.0",
"tslib": "1.9.2",
"tsutils": "2.27.1"
},
"dependencies": {
"ansi-styles": {
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz",
"integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==",
"dev": true,
"requires": {
"color-convert": "1.9.1"
}
},
"chalk": {
"version": "2.4.1",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.1.tgz",
"integrity": "sha512-ObN6h1v2fTJSmUXoS3nMQ92LbDK9be4TV+6G+omQlGJFdcUX5heKi1LZ1YnRMIgwTLEj3E24bT6tYni50rlCfQ==",
"dev": true,
"requires": {
"ansi-styles": "3.2.1",
"escape-string-regexp": "1.0.5",
"supports-color": "5.4.0"
}
},
"diff": {
"version": "3.5.0",
"resolved": "https://registry.npmjs.org/diff/-/diff-3.5.0.tgz",
"integrity": "sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==",
"dev": true
},
"esprima": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.0.tgz",
"integrity": "sha512-oftTcaMu/EGrEIu904mWteKIv8vMuOgGYo7EhVJJN00R/EED9DCua/xxHRdYnKtcECzVg7xOWhflvJMnqcFZjw==",
"dev": true
},
"glob": {
"version": "7.1.2",
"resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz",
"integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==",
"dev": true,
"requires": {
"fs.realpath": "1.0.0",
"inflight": "1.0.6",
"inherits": "2.0.3",
"minimatch": "3.0.4",
"once": "1.4.0",
"path-is-absolute": "1.0.1"
}
},
"js-yaml": {
"version": "3.12.0",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.12.0.tgz",
"integrity": "sha512-PIt2cnwmPfL4hKNwqeiuz4bKfnzHTBv6HyVgjahA6mPLwPDzjDWrplJBMjHUFxku/N3FlmrbyPclad+I+4mJ3A==",
"dev": true,
"requires": {
"argparse": "1.0.10",
"esprima": "4.0.0"
}
}
}
},
"tsutils": {
"version": "2.27.1",
"resolved": "https://registry.npmjs.org/tsutils/-/tsutils-2.27.1.tgz",
"integrity": "sha512-AE/7uzp32MmaHvNNFES85hhUDHFdFZp6OAiZcd6y4ZKKIg6orJTm8keYWBhIhrJQH3a4LzNKat7ZPXZt5aTf6w==",
"dev": true,
"requires": {
"tslib": "1.9.2"
}
},
"tty-browserify": {
"version": "0.0.0",
"resolved": "https://registry.npmjs.org/tty-browserify/-/tty-browserify-0.0.0.tgz",

@ -63,6 +63,7 @@
"source-map": "^0.7.3",
"style-loader": "^0.21.0",
"ts-loader": "^4.4.1",
"tslint": "^5.10.0",
"typescript": "^2.9.2",
"url-loader": "^1.0.1",
"watchpack": "^1.6.0",
@ -86,6 +87,7 @@
"start:dev": "webpack-dev-server",
"build": "webpack --mode production",
"build:dev": "webpack --mode development",
"lint:typescript": "tslint --project . --exclude **/*.d.ts --format stylish src/**/*.ts utils/**/*.ts",
"watch": "webpack --watch --mode production",
"watch:dev": "webpack --watch --mode development"
},

@ -1,39 +1,43 @@
export const TerminalHelpText =
"Type 'help name' to learn more about the command 'name'<br><br>" +
'alias [-g] [name="value"] Create or display Terminal aliases<br>' +
"analyze Get information about the current machine <br>" +
"buy [-l/program] Purchase a program through the Dark Web<br>" +
"cat [file] Display a .msg, .lit, or .txt file<br>" +
"check [script] [args...] Print a script's logs to Terminal<br>" +
"clear Clear all text on the terminal <br>" +
"cls See 'clear' command <br>" +
"connect [ip/hostname] Connects to a remote server<br>" +
"download [script/text file] Downloads scripts or text files to your computer<br>" +
"free Check the machine's memory (RAM) usage<br>" +
"hack Hack the current machine<br>" +
"help [command] Display this help text, or the help text for a command<br>" +
"home Connect to home computer<br>" +
"hostname Displays the hostname of the machine<br>" +
"ifconfig Displays the IP address of the machine<br>" +
"kill [script] [args...] Stops the specified script on the current server <br>" +
"killall Stops all running scripts on the current machine<br>" +
"ls [| grep pattern] Displays all files on the machine<br>" +
"lscpu Displays the number of CPU cores on the machine<br>" +
"mem [script] [-t] [n] Displays the amount of RAM required to run the script<br>" +
"nano [file] Text editor - Open up and edit a script or text file<br>" +
"ps Display all scripts that are currently running<br>" +
"rm [file] Delete a file from the server<br>" +
"run [name] [-t] [n] [args...] Execute a program or script<br>" +
"scan Prints all immediately-available network connections<br>" +
"scan-analyze [d] [-a] Prints info for all servers up to <i>d</i> nodes away<br>" +
"scp [file] [server] Copies a file to a destination server<br>" +
"sudov Shows whether you have root access on this computer<br>" +
"tail [script] [args...] Displays dynamic logs for the specified script<br>" +
"theme [preset] | bg txt hlgt Change the color scheme of the UI<br>" +
"top Displays all running scripts and their RAM usage<br>" +
'unalias "[alias name]" Deletes the specified alias<br>';
/* tslint:disable:max-line-length completed-docs variable-name*/
export const TerminalHelpText: string =
"Type 'help name' to learn more about the command 'name'<br><br>" +
'alias [-g] [name="value"] Create or display Terminal aliases<br>' +
"analyze Get information about the current machine <br>" +
"buy [-l/program] Purchase a program through the Dark Web<br>" +
"cat [file] Display a .msg, .lit, or .txt file<br>" +
"check [script] [args...] Print a script's logs to Terminal<br>" +
"clear Clear all text on the terminal <br>" +
"cls See 'clear' command <br>" +
"connect [ip/hostname] Connects to a remote server<br>" +
"download [script/text file] Downloads scripts or text files to your computer<br>" +
"free Check the machine's memory (RAM) usage<br>" +
"hack Hack the current machine<br>" +
"help [command] Display this help text, or the help text for a command<br>" +
"home Connect to home computer<br>" +
"hostname Displays the hostname of the machine<br>" +
"ifconfig Displays the IP address of the machine<br>" +
"kill [script] [args...] Stops the specified script on the current server <br>" +
"killall Stops all running scripts on the current machine<br>" +
"ls [| grep pattern] Displays all files on the machine<br>" +
"lscpu Displays the number of CPU cores on the machine<br>" +
"mem [script] [-t] [n] Displays the amount of RAM required to run the script<br>" +
"nano [file] Text editor - Open up and edit a script or text file<br>" +
"ps Display all scripts that are currently running<br>" +
"rm [file] Delete a file from the server<br>" +
"run [name] [-t] [n] [args...] Execute a program or script<br>" +
"scan Prints all immediately-available network connections<br>" +
"scan-analyze [d] [-a] Prints info for all servers up to <i>d</i> nodes away<br>" +
"scp [file] [server] Copies a file to a destination server<br>" +
"sudov Shows whether you have root access on this computer<br>" +
"tail [script] [args...] Displays dynamic logs for the specified script<br>" +
"theme [preset] | bg txt hlgt Change the color scheme of the UI<br>" +
"top Displays all running scripts and their RAM usage<br>" +
'unalias "[alias name]" Deletes the specified alias<br>';
export const HelpTexts = {
interface IMap<T> {
[key: string]: T;
}
export const HelpTexts: IMap<string> = {
alias: 'alias [-g] [name="value"] <br>' +
"Create or display aliases. An alias enables a replacement of a word with another string. " +
"It can be used to abbreviate a commonly used command, or commonly used parts of a command. The NAME " +
@ -211,4 +215,4 @@ export const HelpTexts = {
'unalias "r"<br><br>' +
"It is not necessary to differentiate between global and non-global aliases when using 'unalias'",
}
};

@ -1,71 +1,109 @@
import {dialogBoxCreate} from "../utils/DialogBox";
import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver";
import { dialogBoxCreate } from "../utils/DialogBox";
import { Generic_fromJSON, Generic_toJSON, Reviver } from "../utils/JSONReviver";
/**
* Represents a plain text file that is typically stored on a server.
*/
export class TextFile {
/**
* Initiatizes a TextFile from a JSON save state.
*/
static fromJSON(value: any): TextFile {
return Generic_fromJSON(TextFile, value.data);
}
/**
* The full file name.
*/
fn: string;
/**
* The content of the file.
*/
text: string;
constructor(fn = "", txt = "") {
this.fn = (fn.endsWith(".txt") ? fn : `${fn}.txt`).replace(/\s+/g, '');
constructor(fn: string = "", txt: string = "") {
this.fn = (fn.endsWith(".txt") ? fn : `${fn}.txt`).replace(/\s+/g, "");
this.text = txt;
}
append(txt: string) {
/**
* Concatenates the raw values to the end of current content.
*/
append(txt: string): void {
this.text += txt;
}
write(txt: string) {
this.text = txt;
}
read() {
return this.text;
}
show() {
dialogBoxCreate(`${this.fn}<br /><br />${this.text}`, true);
}
download() {
const filename = this.fn;
const file = new Blob([ this.text ], { type: 'text/plain' });
/**
* Serves the file to the user as a downloadable resource through the browser.
*/
download(): void {
const filename: string = this.fn;
const file: Blob = new Blob([ this.text ], { type: "text/plain" });
/* tslint:disable-next-line:strict-boolean-expressions */
if (window.navigator.msSaveOrOpenBlob) {
// IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
} else {
// Others
const a = document.createElement("a");
const url = URL.createObjectURL(file);
const a: HTMLAnchorElement = document.createElement("a");
const url: string = URL.createObjectURL(file);
a.href = url;
a.download = this.fn;
document.body.appendChild(a);
a.click();
setTimeout(() => {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
setTimeout(
() => {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
},
0);
}
}
toJSON() {
/**
* Retrieve the content of the file.
*/
read(): string {
return this.text;
}
/**
* Shows the content to the user via the game's dialog box.
*/
show(): void {
dialogBoxCreate(`${this.fn}<br /><br />${this.text}`, true);
}
/**
* Serialize the current file to a JSON save state.
*/
toJSON(): any {
return Generic_toJSON("TextFile", this);
}
static fromJSON(value: any) {
return Generic_fromJSON(TextFile, value.data);
/**
* Replaces the current content with the text provided.
*/
write(txt: string): void {
this.text = txt;
}
}
Reviver.constructors.TextFile = TextFile;
export function getTextFile(fn: string, server: any): string | null {
if (!fn.endsWith(".txt")) {
fn += ".txt";
}
/**
* Retrieve the file object for the filename on the specified server.
* @param fn The file name to look for
* @param server The server object to look in
* @returns The file object, or null if it couldn't find it.
*/
export function getTextFile(fn: string, server: any): TextFile | null {
const filename: string = !fn.endsWith(".txt") ? `${fn}.txt` : fn;
for (let i = 0; i < server.textFiles.length; i++) {
if (server.textFiles[i].fn === fn) {
return server.textFiles[i];
for (const file of (server.textFiles as TextFile[])) {
if (file.fn === filename) {
return file;
}
}
@ -74,30 +112,36 @@ export function getTextFile(fn: string, server: any): string | null {
/**
* Creates a TextFile on the target server.
* @param {string} fn The file name to create.
* @param {string} txt The contents of the file.
* @param {*} server The server that the file should be created on.
* @returns {TextFile} The instance of the file.
* @param fn The file name to create.
* @param txt The contents of the file.
* @param server The server that the file should be created on.
* @returns The instance of the file.
*/
export function createTextFile(fn: string, txt: string, server: any): TextFile {
export function createTextFile(fn: string, txt: string, server: any): TextFile | undefined {
if (getTextFile(fn, server) !== null) {
// This should probably be a `throw`...
/* tslint:disable-next-line:no-console */
console.error(`A file named "${fn}" already exists on server ${server.hostname}.`);
return;
return undefined;
}
const file = new TextFile(fn, txt);
const file: TextFile = new TextFile(fn, txt);
server.textFiles.push(file);
return file;
}
function deleteTextFile(fn, server) {
if (!fn.endsWith(".txt")) {
fn += ".txt";
}
for (var i = 0; i < server.textFiles.length; ++i) {
if (server.textFiles[i].fn === fn) {
/* tslint:disable-next-line:no-unused-variable */
function deleteTextFile(fn: string, server: any): boolean {
const filename: string = !fn.endsWith(".txt") ? `${fn}.txt` : fn;
/* tslint:disable-next-line:typedef */
for (let i = 0; i < server.textFiles.length; ++i) {
if (server.textFiles[i].fn === filename) {
server.textFiles.splice(i, 1);
return true;
}
}
return false;
}

@ -2,7 +2,8 @@
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"sourceMap": true
"sourceMap": true,
"strict": true
},
"exclude": [
"node_modules"

76
tslint.json Normal file

@ -0,0 +1,76 @@
{
"defaultSeverity": "error",
"extends": [
"tslint:all"
],
"jsRules": {},
"linterOptions": {
"exclude": [
"node_modules/"
]
},
"rules": {
"completed-docs": [
true,
{
"classes": true,
"enums": true,
"enum-members": true,
"functions": {
"visibilities": [
"exported"
]
},
"interfaces": true,
"methods": {
"privacies": [
"public"
]
},
"namespaces": true,
"properties": true,
"types": true,
"variables": {
"visibilities": [
"exported"
]
}
}
],
"linebreak-style": false,
"member-access": [
true,
"no-public"
],
"no-any": false,
"no-inferrable-types": [
true,
"ignore-params",
"ignore-properties"
],
"no-null-keyword": false,
"no-unsafe-any": false,
"object-literal-key-quotes": [
true,
"as-needed"
],
"only-arrow-functions": [
true,
"allow-declarations",
"allow-named-functions"
],
"typedef": [
true,
"call-signatures",
"arrow-call-signatures",
"parameter",
"arrow-parameter",
"property-declaration",
"variable-declaration",
"member-variable-declaration",
"object-destructuring",
"array-destructuring"
]
},
"rulesDirectory": []
}

2
utils/DialogBox.d.ts vendored Normal file

@ -0,0 +1,2 @@
export function dialogBoxCreate(txt: string, preformatted: boolean): void;
export var dialogBoxOpened: boolean;

10
utils/JSONReviver.d.ts vendored Normal file

@ -0,0 +1,10 @@
interface IReviverValue {
ctor: string;
data: object
}
export function Generic_fromJSON<T>(ctor: new () => T, data: any): T;
export function Generic_toJSON(ctorName: string, obj: object, keys?: string[]): string;
export function Reviver(key, value: IReviverValue);
export namespace Reviver {
export var constructors: any;
}