diff --git a/bin/.gitignore b/bin/.gitignore index c7e0eaf..7b18ba1 100644 --- a/bin/.gitignore +++ b/bin/.gitignore @@ -5,8 +5,8 @@ node_modules/ dist/ # Build -bin/ -bin.zip +cactive-bin/ +cactive-bin.zip cactive-bin-linux cactive-bin-macos cactive-bin-win.exe \ No newline at end of file diff --git a/bin/LICENSE.md b/bin/LICENSE.md new file mode 100644 index 0000000..dc8267b --- /dev/null +++ b/bin/LICENSE.md @@ -0,0 +1,22 @@ +# CactiveNetwork Licence Version 1.0 + +## Last Updated: +14th November 2020 + +## Licensor Information: +Copyright ©️ 2022 CactiveNetwork [admin@luke.mx](mailto://admin@luke.mx) + +### Definitions + +- **"Software"** (or **"Work"**) shall mean the source code, algorithms, processes or related material used in this work. +- **"Licence"** shall mean the terms of conditions for re-production, use and additional distribution. +- **"Licensor"** shall mean the copyright owner, or other entity authorised by the copyright owner granting this Licence. +- **"You"** (or **"Your"**) shall mean an individual exercising permissions granted by this License. + +### Terms of this licence + +Permission is granted, free of charge, to any person obtaining a copy of this "Software", to the rights to, use, copy, modify, merge, publish, distribute, and/or sell copies of the Software without restriction, this holds true as long as the software is not used to intentionally hurt people, and/or groups or communities mentally or physically. + +Unless required by applicable law or agreed to in writing, the Licensor provides this work on an "as-is" basis without warranties or conditions of any kind. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the "Software" without modification. \ No newline at end of file diff --git a/bin/README.md b/bin/README.md new file mode 100644 index 0000000..fce31d6 --- /dev/null +++ b/bin/README.md @@ -0,0 +1,53 @@ +### What is this? +This is CAP or Cactive's Assembled Packages. In short, it is a compiled NodeJS script that allows you to use Cactive's products from any coding language or without one at all! + +### How do I set it up? +First, install the cactive-bin.zip file from the most recent release + +Extract that zip file + +Now in the same directory as the cactive-bin extracted folder, open cmd + +Now execute the correct file and add a -h (help) flag. For example, on Windows: +```bash +.\cactive-bin\cactive-bin-win.exe -h +``` + +### cactive.json File +The Cactive JSON file's location is specified using the -f flag and defaults to "./cactive.json" + +The file contains special commands that hold which module, function, and args to pass. For example: +```json +{ + "retrieve": { + "module": "ip", + "func": "retrieve", + "args": "user" + }, + "google": { + "module": "ip", + "func": "retrieve", + "args": { + "ip": "8.8.8.8" + } + } +} +``` + +Here we define two commands, "retrieve" and "google". The "module" specifies that we will run a command from Cactive's IP Module. "func" specifies that we will run the "retrieve" function from Cactive's IP Module. Args specifies two different things, it specifies for "retrieve", we will let the user decide the args to be passed. For "google", it specifies +the arguments passed to the function will always be {"ip": "8.8.8.8"}. + +To use this file we can run our cactive-bin program and we can do something like this where the text after ">>> " is stringified JSON +```bash +>>> {"command":"google"} +[output here] +>>> {"command":"retrieve","args":{"ip":"8.8.8.8"}} +``` + +The data after ">>> " is just stringified JSON that matches this style: +```json +{ + "command": "command to execute from cactive.json", + "args?": "args to pass to the function defined in cactive.json" +} +``` \ No newline at end of file diff --git a/bin/package.json b/bin/package.json index ed9b730..05b2fd6 100644 --- a/bin/package.json +++ b/bin/package.json @@ -5,7 +5,8 @@ "main": "./src/index.ts", "bin": "./dist/index.js", "scripts": { - "build": "tsc && pkg -C GZip --ouput cactive-bin ." + "build": "tsc && pkg -C GZip --out-path cactive-bin/ .", + "build-nodeploy": "tsc" }, "pkg": { "assets": [ @@ -27,6 +28,7 @@ }, "dependencies": { "phin": "^3.7.0", + "readline": "^1.3.0", "ts-command-line-args": "^2.3.1" } } diff --git a/bin/src/index.ts b/bin/src/index.ts index 12792c0..3220c9b 100644 --- a/bin/src/index.ts +++ b/bin/src/index.ts @@ -1,13 +1,13 @@ import { parse } from "ts-command-line-args"; +import { readFileSync, existsSync } from "fs"; +import { createInterface } from "readline"; import Arguments from "./types/arguments"; +import Cactive from "./types/cactive"; +import Input from "./types/input"; import { self, retrieve } from "./ip"; -function test(a: string, b: string) { - console.log(a, b) -} - const year = new Date().getFullYear(); const valid_modules: string[] = [ "ip", @@ -26,36 +26,68 @@ const modules_functions_mapping: {[k: string]: {[k: string]: (...args: any[]) => }; const args = parse( { - module: { type: String, alias: "m", description: "The Cactive module to use (ip, etc.)" }, - func: { type: String, alias: "f", description: "The function to use from that module (retrieve, etc.)" }, - args: { type: String, optional: true, alias: "a", description: "The args to pass to that function seperated by ',' ('8.8.8.8, hi')" }, + file: { type: String, alias: "f", defaultValue: "cactive.json", description: "The file location of you cactive.json file" }, help: { type: Boolean, optional: true, alias: "h", description: "Prints the usage guide" }, }, { helpArg: "help", headerContentSections: [{ header: "Cactive Bin Config", content: "By using Cactive Bin, you agree to our CactiveNetwork Licence Version 1.0" }], - footerContentSections: [{ header: `Copyright © ${year} Luke Matison, Cactive.`, content: "All Rights Reserved." }], + footerContentSections: [{ header: `Copyright © ${year} Cactive.`, content: "All Rights Reserved." }], }, ); if (!args.help) { - if (!valid_modules.includes(args.module)) throw Error("That is not a valid module!"); - if (!valid_functions[args.module].includes(args.func)) throw Error("That is not a valid function for that module!"); - - // Load Module's Function With Args - let pass_args: string[]; - if (args.args) { - pass_args = args.args.replace(/\[|\]/g, "").split(","); - } else { - pass_args = []; + if (!existsSync(args.file)) throw Error("Your file has to exist!"); + + const rl = createInterface({ + input: process.stdin, + output: process.stdout, + }); + const question = (questionText: string) => new Promise(resolve => rl.question(questionText, resolve)); + + let cactiveFile: Cactive; + + try { + cactiveFile = JSON.parse(readFileSync(args.file, 'utf-8')); + } catch (e) { + throw Error("Your file has to be JSON parsable!"); } - const arg_length = modules_functions_mapping[args.module][args.func].length; - if (pass_args.length < arg_length || pass_args.length > arg_length) throw Error("User passed too little or too many arguments for that function"); + (async() => { + while (true) { + let input: Input; + try { + input = JSON.parse(await question(">>> ")); + } catch (e) { + throw Error("Input must be JSON parsable!"); + } - (async () => { - await modules_functions_mapping[args.module][args.func](...pass_args); - })().catch(e => { - console.log(e); - }); + if (!cactiveFile[input.command]) throw Error(`Command ${input.command} was not found in your cactive.json!`); + + const command = cactiveFile[input.command]; + let args: any[]; + let module = command.module; + + if (!valid_modules.includes(module)) throw Error(`Module ${module} is not a valid module!`); + if (!valid_functions[module].includes(command.func)) throw Error(`Function ${command.func} is not a valid function for module ${command.module}!`); + + if (command.args === "user") { + if (!input.args) throw Error("Arguments are required on a user args command!"); + + args = Object.values(input.args); + } else { + args = Object.values(command.args); + } + + const func = modules_functions_mapping[command.module][command.func]; + + if (args.length < func.length || args.length > func.length) throw Error("You passed too little or too many arguments for that function!"); + + try { + await func(...args); + } catch (e) { + console.log(e); + } + } + })(); } \ No newline at end of file diff --git a/bin/src/types/arguments.ts b/bin/src/types/arguments.ts index 458ab7b..5b622e7 100644 --- a/bin/src/types/arguments.ts +++ b/bin/src/types/arguments.ts @@ -1,6 +1,4 @@ export default interface Arguments { - module: string - func: string - args?: string + file: string help?: boolean } \ No newline at end of file diff --git a/bin/src/types/cactive.ts b/bin/src/types/cactive.ts new file mode 100644 index 0000000..9e45391 --- /dev/null +++ b/bin/src/types/cactive.ts @@ -0,0 +1,9 @@ +type args = "user"; + +export default interface Cactive { + [k: string]: { + module: string + func: string + args: args | {[k: string]: any} + } +} \ No newline at end of file diff --git a/bin/src/types/input.ts b/bin/src/types/input.ts new file mode 100644 index 0000000..5b6cca9 --- /dev/null +++ b/bin/src/types/input.ts @@ -0,0 +1,4 @@ +export default interface Input { + command: string + args?: {[k: string]: any} +} \ No newline at end of file diff --git a/bin/yarn.lock b/bin/yarn.lock index f21dfea..be7396f 100644 --- a/bin/yarn.lock +++ b/bin/yarn.lock @@ -814,6 +814,11 @@ readable-stream@^3.1.1, readable-stream@^3.4.0, readable-stream@^3.6.0: string_decoder "^1.1.1" util-deprecate "^1.0.1" +readline@^1.3.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/readline/-/readline-1.3.0.tgz#c580d77ef2cfc8752b132498060dc9793a7ac01c" + integrity sha512-k2d6ACCkiNYz222Fs/iNze30rRJ1iIicW7JuX/7/cozvih6YCkFZH+J6mAFDVgv0dRBaAyr4jDqC95R2y4IADg== + reduce-flatten@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/reduce-flatten/-/reduce-flatten-2.0.0.tgz#734fd84e65f375d7ca4465c69798c25c9d10ae27"