From 49022b94dad14f19b26bc529d3da45f0f31696c4 Mon Sep 17 00:00:00 2001 From: apollorion Date: Fri, 18 Feb 2022 21:26:48 -0500 Subject: [PATCH] store logic in flight schema --- README.MD | 43 +++++++--------- dev.sh | 16 ++++++ extension/background.ts | 110 +++++++++++++++++++++------------------- extension/flights.ts | 4 +- extension/helpers.ts | 2 +- 5 files changed, 96 insertions(+), 79 deletions(-) create mode 100755 dev.sh diff --git a/README.MD b/README.MD index 4a4063b..7ea008d 100644 --- a/README.MD +++ b/README.MD @@ -39,27 +39,26 @@ etc, etc, etc. All flights are defined in `extension/flights.ts`. ## Logical Flights -Logical flights are, I guess you could call, "smart links". +Logical flights are, I guess you could call, "smart links". They arent hardcoded into this repository and use logic to determine where you want to go. -Some logical flights (`gh` for example) may require some initial configuration (see below). +You can set defaults for anything within a logical flight. -### Github - `gh` -The `gh` logical flight works 2 ways. -#### Default Org (config required) -If you visit a specific github org often, you can set the org name so that during the flight you only have to specify the repo name. +Look at the `gh` logical flight as an example. Its logic is as follows: `https://github.com/${gh-org}/${repo}`. -Say you visit the `Apollorion` github org a lot. +If no defaults are specified, the extension will assume you will pass in at least 2 parameters to the flight. +`fly hg Apollorion manifests.io` is calculated positionally in this case. `Apollorion` will replace `${gh-org}` and `manifests.io` will replace `${repo}`. +Which will fly to `https://github.com/Apollorion/manifests.io`. -You can run `fly set gh-org Apollorion` and now the extension will assume the `Apollorion` org if an org isnt directly specified. -Flights like `fly gh manifests.io` will now take you to `https://github.com/Apollorion/manifests.io`. -#### Specifying Org -If you specify an org during your flight, it will use that org instead of the default one. +You can, optionally, set a default for any parameter in a logical flight. +`fly set gh-org Apollorion` will set the default for `gh-org` to `Apollorion`. +Now, when you fly the `gh` flight, it will first replace `${gh-org}` with `Apollorion` and then any remaining parameters will be replaced positionally. +So you could do `fly gh manifests.io` and it will fly to `https://github.com/Apollorion/manifests.io`. -Flights like `fly gh Apollorion manifests.io` will take you to `https://github.com/Apollorion/manifests.io`. - -### AWS - `aws` -The `aws` logical flight will take you to the default console home (`https://console.aws.amazon.com/console/home`) if your flight is just `fly aws`. -Alternatively, you can go to a specific region via `fly aws us-east-1`. +Note: ANY unset parameters will be replaced positionally, in order. +If you have a logical flight that is defined as `https://my.com/{$thing1}/{$thing2}/{$thing3}`, and you only set the default for `thing1`. +The extension will expect you to pass in 2 parameters at flight time, otherwise a default flight will be used or an error will be thrown. +`fly my.com test1 test2` will fly to `https://my.com/{thing1 default}/test1/test2/`. +`fly my.com test` will either go to a defined default flight or throw an error. # Custom flight repos You can host a custom flight repo and point the extension to it. This will allow you to define standard and logical flights without contributing to this project. @@ -87,16 +86,12 @@ interface CustomFlightRepo { To use the custom flight repo, just run `fly set repo $url`. You can unset this repo at anytime via `fly unset repo`. # Devving this repo -I'd like to make devving a little less painful somehow, but right now what you need to do is: -1. cd extension -2. yarn install -3. yarn build -4. test things -5. repeat steps 3, 4 infinitely - -I know things like nodemon exist, but it doesnt play well with chrome extensions and I havent figured out why quite yet. + +Startup the dev server via `./dev.sh`. You need to `load unpacked` the extension in the `extension/dist` dir after building at least once. +I havent found a work around yet, but whenever you make a change you need to refresh the extension in `chrome://extensions`. + ## Logical Flights Say a users query is `fly gh Apollorion manifests.io` diff --git a/dev.sh b/dev.sh new file mode 100755 index 0000000..bad9e98 --- /dev/null +++ b/dev.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +function rebuild(){ + echo "Rebuilding" + yarn build + export FLY_RELEASE_VERSION="0.0.1" + envsubst < ./extension/manifest.json > ./dist/manifest.json + cp ./extension/img.png ./dist/img.png + echo "Happy Coding!" +} +export -f rebuild + +yarn install +rebuild + +fswatch -o ./extension | xargs -t -n1 -P2 bash -c 'rebuild "$@"' _ \ No newline at end of file diff --git a/extension/background.ts b/extension/background.ts index 8f8be1a..5e2b35b 100644 --- a/extension/background.ts +++ b/extension/background.ts @@ -25,50 +25,11 @@ async function main() { } else { const repoFlightResponse = await repoFlightResponsePromise; const flight = await getFlightFromQuery(query, repoFlightResponse); - if(flight.type == FlightType.LOGICAL){ - switch(flight.identifier){ - case "gh": - await goToGithub(flight); - break; - default: - await handleFlight(flight); - break; - } - } else { - await handleFlight(flight); - } + await handleFlight(flight); } - }); } -async function goToGithub(flight: Flight){ - if(flight.values !== undefined && flight.values.length === 2) { - return await handleFlight(flight); - } - - try { - let org = await getLocalStorage("gh-org"); - let values: string[] = []; - if(flight.values !== undefined){ - values = flight.values; - } - const newFlight: Flight = { - type: flight.type, - identifier: flight.identifier, - override: flight.override, - values: [org, ...values] - } - await handleFlight(newFlight); - } catch { - const newFlight: Flight = { - type: FlightType.STANDARD, - identifier: "config-github/org-not-set" - }; - await handleFlight(newFlight); - } -} - async function handleStandardFlight(flight: Flight){ const repoFlightResponse = await repoFlightResponsePromise; let newFlights = getNewFlightResponseSynchronous(repoFlightResponse); @@ -93,23 +54,68 @@ async function handleFlight(flight: Flight){ if(flight.type === FlightType.LOGICAL && flight.values !== undefined) { if (flight.identifier in newLogicalFlights) { let flightDetails = newLogicalFlights[flight.identifier]; + let logic = flightDetails["logic"]; + + // Regex that match everything inside ${} + let regex = /\${([^}]*)}/g; + let matches = logic.match(regex); + + if(matches === null) { + console.log("Matches is null"); + + // If the length of matches and the flight values are the same + // The user wants to supply all the values + } else if (matches.length === flight.values.length) { + let i = 0; + for(let match of matches){ + const value = flight.values[i]; + if(value !== undefined) { + logic = logic.replace(match, value); + i++; + } + } - if(flight.values.length === 0){ - if(flight.override !== undefined) { - return await redirect("override flight", flight.override); - } else { - return await redirect("Incorrect Length", "https://github.com/Apollorion/fly/blob/main/help/logical-logicalFlights.md#incorrect-length"); + } else { + for(let match of matches){ + try { + const matchNoBrackets = match.replace("${", "").replace("}", ""); + const value = await getLocalStorage(matchNoBrackets); + if(value !== undefined) { + logic = logic.replace(match, value); + + // Remove the item from the array, so we can replace using the values of the flight later + const index = matches.indexOf(match); + if (index > -1) { + matches.splice(index, 1); // 2nd parameter means remove one item only + } + } + } catch { + console.log("Could not find value for: " + match); + } } - } - let link = flightDetails["logic"]; - let i = 1; - for (let item of flight.values) { - link = link.replace(`$${i}`, item); - i++; + // If there are still matches left, we will treat them as positional variables + let i = 0; + for(let match of matches){ + const value = flight.values[i]; + if(value !== undefined) { + logic = logic.replace(match, value); + i++; + } + } } - return await redirect(`Following Link`, link); + if(logic.includes("${") && logic.includes("}")){ + if(flight.values.length === 0){ + if(flight.override !== undefined) { + return await redirect("override flight", flight.override); + } else { + return await redirect("Incorrect Length", "https://github.com/Apollorion/fly/blob/main/help/logical-logicalFlights.md#incorrect-length"); + } + } + } else { + return await redirect("Following Link", logic); + } } else { return await redirect("Logic Not Found", "https://github.com/Apollorion/fly/blob/main/help/logical-logicalFlights.md#logic-not-found"); diff --git a/extension/flights.ts b/extension/flights.ts index f654402..9bc2c39 100644 --- a/extension/flights.ts +++ b/extension/flights.ts @@ -3,10 +3,10 @@ import {LogicalFlightDefinition, StandardFlightDefinition} from "./types.js"; export const logicalFlights: LogicalFlightDefinition = { "aws": { "override": "https://console.aws.amazon.com/console/home", - "logic": "https://console.aws.amazon.com/console/home?region=$1" + "logic": "https://console.aws.amazon.com/console/home?region=${region}", }, "gh": { - "logic": "https://github.com/$1/$2" + "logic": "https://github.com/${gh-org}/${repo}", } }; diff --git a/extension/helpers.ts b/extension/helpers.ts index 61d509c..c498290 100644 --- a/extension/helpers.ts +++ b/extension/helpers.ts @@ -122,7 +122,7 @@ export async function getLocalStorage(key: string): Promise{ } export async function redirect(message: string, link: string){ - const dev = false; + const dev = true; console.log(message, link); if(!dev){ console.log("following link", link);