From 070a32a89e107e40e2a05d233e1293d64d188469 Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Thu, 10 Oct 2024 01:12:36 +0200 Subject: [PATCH] improve init script assignment --- frontend/package.json | 10 +- ...AppConnectLightweightResourcePicker.svelte | 32 +-- .../lib/components/FlowPreviewContent.svelte | 3 +- frontend/src/lib/components/custom_ui.ts | 1 + .../lib/components/flows/FlowHistory.svelte | 1 + .../components/flows/FlowHistoryInner.svelte | 30 +- .../lib/components/icons/WindmillIcon.svelte | 2 +- frontend/src/lib/components/icons/store.ts | 2 +- frontend/src/lib/script_helpers.ts | 266 ++++++++++-------- 9 files changed, 192 insertions(+), 155 deletions(-) diff --git a/frontend/package.json b/frontend/package.json index c1e3ea3549..e05dcaa70f 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -300,9 +300,9 @@ "types": "./package/utils.d.ts", "default": "./package/utils.js" }, - "./icons/store": { - "types": "./package/icons/store.d.ts", - "default": "./package/icons/store.js" + "./components/icons/store": { + "types": "./package/components/icons/store.d.ts", + "default": "./package/components/icons/store.js" }, "./script_helpers": { "types": "./package/script_helpers.d.ts", @@ -481,8 +481,8 @@ "script_helpers": [ "./package/script_helpers.d.ts" ], - "icons/store": [ - "./package/icons/store.d.ts" + "components/icons/store": [ + "./package/components/icons/store.d.ts" ] } }, diff --git a/frontend/src/lib/components/AppConnectLightweightResourcePicker.svelte b/frontend/src/lib/components/AppConnectLightweightResourcePicker.svelte index 4e59ea7858..30dd9b3d29 100644 --- a/frontend/src/lib/components/AppConnectLightweightResourcePicker.svelte +++ b/frontend/src/lib/components/AppConnectLightweightResourcePicker.svelte @@ -1,6 +1,4 @@ -{#if customIcon} +{#if customIcon.white || customIcon.normal} {#if white} Windmill Custom icon {:else} diff --git a/frontend/src/lib/components/icons/store.ts b/frontend/src/lib/components/icons/store.ts index cbd03d5c96..960a49e7c8 100644 --- a/frontend/src/lib/components/icons/store.ts +++ b/frontend/src/lib/components/icons/store.ts @@ -1 +1 @@ -export let customIcon: { normal: string; white: string } | undefined = undefined +export let customIcon: { normal?: string; white?: string } = {} diff --git a/frontend/src/lib/script_helpers.ts b/frontend/src/lib/script_helpers.ts index f861a29434..7536b952ef 100644 --- a/frontend/src/lib/script_helpers.ts +++ b/frontend/src/lib/script_helpers.ts @@ -2,7 +2,7 @@ import { type Script } from './gen' import type { SupportedLanguage } from './common' -export let PYTHON_FAILURE_MODULE_CODE = `import os +const PYTHON_FAILURE_MODULE_CODE = `import os def main(message: str, name: str, step_id: str): flow_id = os.environ.get("WM_ROOT_FLOW_JOB_ID") @@ -11,13 +11,13 @@ def main(message: str, name: str, step_id: str): print("step_id", step_id) return { "message": message, "flow_id": flow_id, "step_id": step_id, "recover": False }` -export let PYTHON_INIT_CODE_CLEAR = `# import wmill +const PYTHON_INIT_CODE_CLEAR = `# import wmill def main(x: str): return x` -export let PYTHON_INIT_CODE_TRIGGER = `import wmill +const PYTHON_INIT_CODE_TRIGGER = `import wmill def main(): @@ -32,7 +32,7 @@ def main(): # return range from (state to newState) return [1, 2, 3]` -export let PYTHON_INIT_CODE = `import os +const PYTHON_INIT_CODE = `import os import wmill # You can import any PyPi package. @@ -73,7 +73,7 @@ def main( # return value is converted to JSON return {"splitted": name.split(), "user": user, "state": new_state}` -export let NATIVETS_INIT_CODE = `// Fetch-only script, no imports allowed (except windmill) but benefits from a dedicated highly efficient runtime +const NATIVETS_INIT_CODE = `// Fetch-only script, no imports allowed (except windmill) but benefits from a dedicated highly efficient runtime //import * as wmill from './windmill.ts' export async function main(example_input: number = 3) { @@ -85,7 +85,7 @@ export async function main(example_input: number = 3) { } ` -export let BUNNATIVE_INIT_CODE = `//native +const BUNNATIVE_INIT_CODE = `//native //you can add proxy support using //proxy http(s)://host:port // native scripts are bun scripts that are executed on native workers and can be parallelized @@ -102,7 +102,7 @@ export async function main(example_input: number = 3) { } ` -export let NATIVETS_INIT_CODE_CLEAR = `// Fetch-only script, no imports allowed (except windmill) but benefits from a dedicated highly efficient runtime +const NATIVETS_INIT_CODE_CLEAR = `// Fetch-only script, no imports allowed (except windmill) but benefits from a dedicated highly efficient runtime //import * as wmill from './windmill.ts' export async function main() { @@ -113,7 +113,7 @@ export async function main() { } ` -export let DENO_INIT_CODE = `// Ctrl/CMD+. to cache dependencies on imports hover. +const DENO_INIT_CODE = `// Ctrl/CMD+. to cache dependencies on imports hover. // Deno uses "npm:" prefix to import from npm (https://deno.land/manual@v1.36.3/node/npm_specifiers) // import * as wmill from "npm:windmill-client@${__pkg__.version}" @@ -134,7 +134,7 @@ export async function main( } ` -export let BUN_INIT_CODE = `// there are multiple modes to add as header: //nobundling //native //npm //nodejs +const BUN_INIT_CODE = `// there are multiple modes to add as header: //nobundling //native //npm //nodejs // https://www.windmill.dev/docs/getting_started/scripts_quickstart/typescript#modes // import { toWords } from "number-to-words@1" @@ -165,7 +165,7 @@ export async function main( } ` -export let GO_INIT_CODE = `package inner +const GO_INIT_CODE = `package inner import ( "fmt" @@ -189,7 +189,7 @@ func main(x string, nested struct { } ` -export let GO_FAILURE_MODULE_CODE = `package inner +const GO_FAILURE_MODULE_CODE = `package inner import ( "fmt" @@ -206,21 +206,21 @@ func main(message string, name string) (interface{}, error) { } ` -export let DENO_INIT_CODE_CLEAR = `// import * as wmill from "npm:windmill-client@${__pkg__.version}" +const DENO_INIT_CODE_CLEAR = `// import * as wmill from "npm:windmill-client@${__pkg__.version}" export async function main(x: string) { return x } ` -export let BUN_INIT_CODE_CLEAR = `// import * as wmill from "windmill-client" +const BUN_INIT_CODE_CLEAR = `// import * as wmill from "windmill-client" export async function main(x: string) { return x } ` -export let DENO_FAILURE_MODULE_CODE = ` +const DENO_FAILURE_MODULE_CODE = ` export async function main(message: string, name: string, step_id: string) { const flow_id = Deno.env.get("WM_ROOT_FLOW_JOB_ID") console.log("message", message) @@ -230,7 +230,7 @@ export async function main(message: string, name: string, step_id: string) { } ` -export let BUN_FAILURE_MODULE_CODE = ` +const BUN_FAILURE_MODULE_CODE = ` export async function main(message: string, name: string, step_id: string) { const flow_id = process.env.WM_ROOT_FLOW_JOB_ID console.log("message", message) @@ -240,7 +240,7 @@ export async function main(message: string, name: string, step_id: string) { } ` -export let POSTGRES_INIT_CODE = `-- to pin the database use '-- database f/your/path' +const POSTGRES_INIT_CODE = `-- to pin the database use '-- database f/your/path' -- $1 name1 = default arg -- $2 name2 -- $3 name3 @@ -249,7 +249,7 @@ INSERT INTO demo VALUES (\$1::TEXT, \$2::INT, \$3::TEXT[]) RETURNING *; UPDATE demo SET col2 = \$4::INT WHERE col2 = \$2::INT; ` -export let MYSQL_INIT_CODE = `-- to pin the database use '-- database f/your/path' +const MYSQL_INIT_CODE = `-- to pin the database use '-- database f/your/path' -- :name1 (text) = default arg -- :name2 (int) -- :name3 (int) @@ -257,7 +257,7 @@ INSERT INTO demo VALUES (:name1, :name2); UPDATE demo SET col2 = :name3 WHERE col2 = :name2; ` -export let BIGQUERY_INIT_CODE = `-- to pin the database use '-- database f/your/path' +const BIGQUERY_INIT_CODE = `-- to pin the database use '-- database f/your/path' -- @name1 (string) = default arg -- @name2 (integer) -- @name3 (string[]) @@ -266,7 +266,7 @@ INSERT INTO \`demodb.demo\` VALUES (@name1, @name2, @name3); UPDATE \`demodb.demo\` SET col2 = @name4 WHERE col2 = @name2; ` -export let SNOWFLAKE_INIT_CODE = `-- to pin the database use '-- database f/your/path' +const SNOWFLAKE_INIT_CODE = `-- to pin the database use '-- database f/your/path' -- ? name1 (varchar) = default arg -- ? name2 (int) INSERT INTO demo VALUES (?, ?); @@ -275,7 +275,7 @@ INSERT INTO demo VALUES (?, ?); UPDATE demo SET col2 = ? WHERE col2 = ?; ` -export let MSSQL_INIT_CODE = `-- return_last_result +const MSSQL_INIT_CODE = `-- return_last_result -- to pin the database use '-- database f/your/path' -- @p1 name1 (varchar) = default arg -- @p2 name2 (int) @@ -284,7 +284,7 @@ INSERT INTO demo VALUES (@p1, @p2); UPDATE demo SET col2 = @p3 WHERE col2 = @p2; ` -export let GRAPHQL_INIT_CODE = `query($name4: String, $name2: Int, $name3: [String]) { +const GRAPHQL_INIT_CODE = `query($name4: String, $name2: Int, $name3: [String]) { demo(name1: $name1, name2: $name2, name3: $name3) { name1, name2, @@ -293,7 +293,7 @@ export let GRAPHQL_INIT_CODE = `query($name4: String, $name2: Int, $name3: [Stri } ` -export let PHP_INIT_CODE = `) -> anyhow::Result { } ` -export let FETCH_INIT_CODE = `export async function main( +const FETCH_INIT_CODE = `export async function main( url: string | undefined, method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' = 'GET', body: Object = {}, @@ -379,7 +379,7 @@ export let FETCH_INIT_CODE = `export async function main( }) }` -export let BASH_INIT_CODE = `# shellcheck shell=bash +const BASH_INIT_CODE = `# shellcheck shell=bash # arguments of the form X="$I" are parsed as parameters X of type string msg="$1" dflt="\${2:-default value}" @@ -389,7 +389,7 @@ dflt="\${2:-default value}" echo "Hello $msg" ` -export let DENO_INIT_CODE_TRIGGER = `import * as wmill from "npm:windmill-client@${__pkg__.version}" +const DENO_INIT_CODE_TRIGGER = `import * as wmill from "npm:windmill-client@${__pkg__.version}" export async function main() { @@ -410,7 +410,7 @@ export async function main() { } ` -export let BUN_INIT_CODE_TRIGGER = `import * as wmill from "windmill-client" +const BUN_INIT_CODE_TRIGGER = `import * as wmill from "windmill-client" export async function main() { @@ -431,7 +431,7 @@ export async function main() { } ` -export let GO_INIT_CODE_TRIGGER = `package inner +const GO_INIT_CODE_TRIGGER = `package inner import ( wmill "github.com/windmill-labs/windmill-go-client" @@ -455,7 +455,7 @@ func main() (interface{}, error) { } ` -export let DENO_INIT_CODE_APPROVAL = `import * as wmill from "npm:windmill-client@^1.158.2" +const DENO_INIT_CODE_APPROVAL = `import * as wmill from "npm:windmill-client@^1.158.2" export async function main(approver?: string) { const urls = await wmill.getResumeUrls(approver) @@ -483,7 +483,7 @@ export async function main(approver?: string) { // add a form in Advanced - Suspend // all on approval steps: https://www.windmill.dev/docs/flows/flow_approval` -export let BUN_INIT_CODE_APPROVAL = `import * as wmill from "windmill-client@^1.158.2" +const BUN_INIT_CODE_APPROVAL = `import * as wmill from "windmill-client@^1.158.2" export async function main(approver?: string) { const urls = await wmill.getResumeUrls(approver) @@ -511,7 +511,7 @@ export async function main(approver?: string) { // add a form in Advanced - Suspend // all on approval steps: https://www.windmill.dev/docs/flows/flow_approval` -export let BUN_PREPROCESSOR_MODULE_CODE = ` +const BUN_PREPROCESSOR_MODULE_CODE = ` export async function preprocessor( wm_trigger: { kind: 'http' | 'email' | 'webhook', @@ -532,7 +532,7 @@ export async function preprocessor( } ` -export let DENO_PREPROCESSOR_MODULE_CODE = ` +const DENO_PREPROCESSOR_MODULE_CODE = ` export async function preprocessor( wm_trigger: { kind: 'http' | 'email' | 'wehbook', @@ -553,7 +553,7 @@ export async function preprocessor( } ` -export let PYTHON_INIT_CODE_APPROVAL = `import wmill +const PYTHON_INIT_CODE_APPROVAL = `import wmill def main(): urls = wmill.get_resume_urls() @@ -580,7 +580,7 @@ def main(): # add a form in Advanced - Suspend # all on approval steps: https://www.windmill.dev/docs/flows/flow_approval` -export let PYTHON_PREPROCESSOR_MODULE_CODE = `from typing import TypedDict, Literal +const PYTHON_PREPROCESSOR_MODULE_CODE = `from typing import TypedDict, Literal class Http(TypedDict): route: str # The route path, e.g. "/users/:id" @@ -603,7 +603,7 @@ def preprocessor( } ` -export let DOCKER_INIT_CODE = `# shellcheck shell=bash +const DOCKER_INIT_CODE = `# shellcheck shell=bash # Bash script that calls docker as a client to the host daemon # See documentation: https://www.windmill.dev/docs/advanced/docker msg="\${1:-world}" @@ -616,7 +616,7 @@ docker pull $IMAGE docker run --rm $IMAGE $COMMAND ` -export let POWERSHELL_INIT_CODE = `param($Msg, $Dflt = "default value", [int]$Nb = 3) +const POWERSHELL_INIT_CODE = `param($Msg, $Dflt = "default value", [int]$Nb = 3) # Import-Module MyModule @@ -627,7 +627,7 @@ export let POWERSHELL_INIT_CODE = `param($Msg, $Dflt = "default value", [int]$Nb # the last line of the stdout is the return value Write-Output "Hello $Msg"` -export let ANSIBLE_PLAYBOOK_INIT_CODE = `--- +const ANSIBLE_PLAYBOOK_INIT_CODE = `--- inventory: - resource_type: ansible_inventory # You can pin an inventory to this script by hardcoding the resource path: @@ -671,43 +671,87 @@ dependencies: content: "{{ my_result | to_json }}" dest: result.json ` - -const ALL_INITIAL_CODE = [ - PYTHON_INIT_CODE, - PYTHON_INIT_CODE_TRIGGER, - DENO_INIT_CODE, - POSTGRES_INIT_CODE, - MYSQL_INIT_CODE, - BIGQUERY_INIT_CODE, - SNOWFLAKE_INIT_CODE, - MSSQL_INIT_CODE, - GRAPHQL_INIT_CODE, - DENO_INIT_CODE_TRIGGER, - DENO_INIT_CODE_CLEAR, - PYTHON_INIT_CODE_CLEAR, - PYTHON_FAILURE_MODULE_CODE, - PYTHON_PREPROCESSOR_MODULE_CODE, - DENO_INIT_CODE_APPROVAL, - DENO_FAILURE_MODULE_CODE, - DENO_PREPROCESSOR_MODULE_CODE, - BUN_INIT_CODE, - BUN_INIT_CODE_CLEAR, - BUN_INIT_CODE_APPROVAL, - BUN_FAILURE_MODULE_CODE, - BUN_PREPROCESSOR_MODULE_CODE, - BASH_INIT_CODE, - POWERSHELL_INIT_CODE, - PHP_INIT_CODE, - RUST_INIT_CODE, - ANSIBLE_PLAYBOOK_INIT_CODE -] +export const INITIAL_CODE = { + bun: { + script: BUN_INIT_CODE, + trigger: BUN_INIT_CODE_TRIGGER, + approval: BUN_INIT_CODE_APPROVAL, + failure: BUN_FAILURE_MODULE_CODE, + preprocessor: BUN_PREPROCESSOR_MODULE_CODE + }, + python3: { + script: PYTHON_INIT_CODE, + trigger: PYTHON_INIT_CODE_TRIGGER, + approval: PYTHON_INIT_CODE_APPROVAL, + failure: PYTHON_FAILURE_MODULE_CODE, + preprocessor: PYTHON_PREPROCESSOR_MODULE_CODE + }, + deno: { + script: DENO_INIT_CODE, + trigger: DENO_INIT_CODE_TRIGGER, + approval: DENO_INIT_CODE_APPROVAL, + failure: DENO_FAILURE_MODULE_CODE, + preprocessor: DENO_PREPROCESSOR_MODULE_CODE, + fetch: FETCH_INIT_CODE + }, + go: { + script: GO_INIT_CODE, + trigger: GO_INIT_CODE_TRIGGER, + failure: GO_FAILURE_MODULE_CODE + }, + bash: { + script: BASH_INIT_CODE + }, + powershell: { + script: POWERSHELL_INIT_CODE + }, + nativets: { + script: NATIVETS_INIT_CODE + }, + postgresql: { + script: POSTGRES_INIT_CODE + }, + mysql: { + script: MYSQL_INIT_CODE + }, + bigquery: { + script: BIGQUERY_INIT_CODE + }, + snowflake: { + script: SNOWFLAKE_INIT_CODE + }, + mssql: { + script: MSSQL_INIT_CODE + }, + graphql: { + script: GRAPHQL_INIT_CODE + }, + php: { + script: PHP_INIT_CODE + }, + rust: { + script: RUST_INIT_CODE + }, + ansible: { + script: ANSIBLE_PLAYBOOK_INIT_CODE + }, + docker: { + script: DOCKER_INIT_CODE + }, + bunnative: { + script: BUNNATIVE_INIT_CODE + } +} export function isInitialCode(content: string): boolean { - for (const code of ALL_INITIAL_CODE) { - if (content === code) { - return true - } - } + Object.values(INITIAL_CODE).forEach((lang) => { + Object.values(lang).forEach((code) => { + if (content === code) { + return true + } + }) + }) + return false } @@ -731,93 +775,93 @@ export function initialCode( } if (language === 'deno') { if (kind === 'trigger') { - return DENO_INIT_CODE_TRIGGER + return INITIAL_CODE.deno.trigger } else if (kind === 'script') { if (subkind === 'flow') { - return DENO_INIT_CODE_CLEAR + return INITIAL_CODE.deno.script } else if (subkind === 'pgsql') { - return POSTGRES_INIT_CODE + return INITIAL_CODE.postgresql.script } else if (subkind === 'mysql') { - return MYSQL_INIT_CODE + return INITIAL_CODE.mysql.script } else if (subkind === 'fetch') { - return FETCH_INIT_CODE + return INITIAL_CODE.deno.fetch } else if (subkind === 'preprocessor') { - return DENO_PREPROCESSOR_MODULE_CODE + return INITIAL_CODE.deno.preprocessor } else { - return DENO_INIT_CODE + return INITIAL_CODE.deno.script } } else if (kind === 'failure') { - return DENO_FAILURE_MODULE_CODE + return INITIAL_CODE.deno.failure } else if (kind === 'approval') { - return DENO_INIT_CODE_APPROVAL + return INITIAL_CODE.deno.approval } else { - return DENO_INIT_CODE + return INITIAL_CODE.deno.script } } else if (language === 'python3') { if (kind === 'trigger') { - return PYTHON_INIT_CODE_TRIGGER + return INITIAL_CODE.python3.trigger } else if (kind === 'approval') { - return PYTHON_INIT_CODE_APPROVAL + return INITIAL_CODE.python3.approval } else if (kind === 'failure') { - return PYTHON_FAILURE_MODULE_CODE + return INITIAL_CODE.python3.failure } else if (subkind === 'flow') { - return PYTHON_INIT_CODE_CLEAR + return INITIAL_CODE.python3.script } else if (subkind === 'preprocessor') { - return PYTHON_PREPROCESSOR_MODULE_CODE + return INITIAL_CODE.python3.preprocessor } else { - return PYTHON_INIT_CODE + return INITIAL_CODE.python3.script } } else if (language == 'bash') { if (subkind === 'docker') { - return DOCKER_INIT_CODE + return INITIAL_CODE.docker.script } else { - return BASH_INIT_CODE + return INITIAL_CODE.bash.script } } else if (language == 'powershell') { - return POWERSHELL_INIT_CODE + return INITIAL_CODE.powershell.script } else if (language == 'nativets') { - return NATIVETS_INIT_CODE + return INITIAL_CODE.nativets.script } else if (language == 'postgresql') { - return POSTGRES_INIT_CODE + return INITIAL_CODE.postgresql.script } else if (language == 'mysql') { - return MYSQL_INIT_CODE + return INITIAL_CODE.mysql.script } else if (language == 'bigquery') { - return BIGQUERY_INIT_CODE + return INITIAL_CODE.bigquery.script } else if (language == 'snowflake') { - return SNOWFLAKE_INIT_CODE + return INITIAL_CODE.snowflake.script } else if (language == 'mssql') { - return MSSQL_INIT_CODE + return INITIAL_CODE.mssql.script } else if (language == 'graphql') { - return GRAPHQL_INIT_CODE + return INITIAL_CODE.graphql.script } else if (language == 'php') { - return PHP_INIT_CODE + return INITIAL_CODE.php.script } else if (language == 'rust') { - return RUST_INIT_CODE + return INITIAL_CODE.rust.script } else if (language == 'ansible') { - return ANSIBLE_PLAYBOOK_INIT_CODE + return INITIAL_CODE.ansible.script } else if (language == 'bun' || language == 'bunnative') { if (kind == 'trigger') { - return BUN_INIT_CODE_TRIGGER + return INITIAL_CODE.bun.trigger } else if (language == 'bunnative' || subkind === 'bunnative') { - return BUNNATIVE_INIT_CODE + return INITIAL_CODE.bunnative.script } else if (kind === 'approval') { - return BUN_INIT_CODE_APPROVAL + return INITIAL_CODE.bun.approval } else if (kind === 'failure') { - return BUN_FAILURE_MODULE_CODE + return INITIAL_CODE.bun.failure } else if (subkind === 'preprocessor') { - return BUN_PREPROCESSOR_MODULE_CODE + return INITIAL_CODE.bun.preprocessor } else if (subkind === 'flow') { - return BUN_INIT_CODE_CLEAR + return INITIAL_CODE.bun.script } - return BUN_INIT_CODE + return INITIAL_CODE.bun.script } else { if (kind === 'failure') { - return GO_FAILURE_MODULE_CODE + return INITIAL_CODE.go.failure } else if (kind === 'trigger') { - return GO_INIT_CODE_TRIGGER + return INITIAL_CODE.go.trigger } else { - return GO_INIT_CODE + return INITIAL_CODE.go.script } } }