From 1b12910fb8cf97cc146ace4e5b3f8aef357640f5 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Tue, 21 Mar 2023 13:52:02 +0100 Subject: [PATCH] feat: upload env variables --- package-lock.json | 9 ++ package.json | 1 + .../function-[function]/settings/+page.svelte | 16 ++-- .../function-[function]/settings/store.ts | 8 ++ .../settings/uploadVariables.svelte | 83 +++++++++---------- 5 files changed, 68 insertions(+), 49 deletions(-) create mode 100644 src/routes/console/project-[project]/functions/function-[function]/settings/store.ts diff --git a/package-lock.json b/package-lock.json index 83761e17f..020704b73 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "@sentry/svelte": "^7.36.0", "@sentry/tracing": "^7.36.0", "analytics": "^0.8.1", + "dotenv": "^16.0.3", "echarts": "^5.4.1", "logrocket": "^3.0.1", "pretty-bytes": "^6.1.0", @@ -3613,6 +3614,14 @@ "node": ">=12" } }, + "node_modules/dotenv": { + "version": "16.0.3", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz", + "integrity": "sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==", + "engines": { + "node": ">=12" + } + }, "node_modules/eastasianwidth": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz", diff --git a/package.json b/package.json index 0d0865856..bb39c6b4f 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "@sentry/svelte": "^7.36.0", "@sentry/tracing": "^7.36.0", "analytics": "^0.8.1", + "dotenv": "^16.0.3", "echarts": "^5.4.1", "logrocket": "^3.0.1", "pretty-bytes": "^6.1.0", diff --git a/src/routes/console/project-[project]/functions/function-[function]/settings/+page.svelte b/src/routes/console/project-[project]/functions/function-[function]/settings/+page.svelte index 475de53f1..09a4ea17d 100644 --- a/src/routes/console/project-[project]/functions/function-[function]/settings/+page.svelte +++ b/src/routes/console/project-[project]/functions/function-[function]/settings/+page.svelte @@ -18,7 +18,7 @@ import { onMount } from 'svelte'; import Variable from '../../createVariable.svelte'; import { execute, func } from '../store'; - // import Upload from './uploadVariables.svelte'; + import UploadVariables from './uploadVariables.svelte'; import { Table, TableBody, @@ -36,7 +36,7 @@ const functionId = $page.params.function; let showDelete = false; let selectedVar: Models.Variable = null; - // let showVariablesUpload = false; + let showVariablesUpload = false; let showVariablesModal = false; let showVariablesDropdown = []; let timeout: number = null; @@ -369,10 +369,10 @@ Download .env file - + {#if data.variables.total}
@@ -446,8 +446,9 @@
{:else} - (showVariablesModal = !showVariablesModal)} - >Create a variable to get started + (showVariablesModal = !showVariablesModal)}> + Create a variable to get started + {/if} @@ -505,3 +506,6 @@ on:created={handleVariableCreated} on:updated={handleVariableUpdated} /> {/if} +{#if showVariablesUpload} + +{/if} diff --git a/src/routes/console/project-[project]/functions/function-[function]/settings/store.ts b/src/routes/console/project-[project]/functions/function-[function]/settings/store.ts new file mode 100644 index 000000000..3f8cf0cea --- /dev/null +++ b/src/routes/console/project-[project]/functions/function-[function]/settings/store.ts @@ -0,0 +1,8 @@ +import { page } from '$app/stores'; +import { derived } from 'svelte/store'; +import type { Models } from '@aw-labs/appwrite-console'; + +export const variables = derived( + page, + ($page) => $page.data.variables.variables as Models.Variable[] +); diff --git a/src/routes/console/project-[project]/functions/function-[function]/settings/uploadVariables.svelte b/src/routes/console/project-[project]/functions/function-[function]/settings/uploadVariables.svelte index 617f40b46..d5caa07e7 100644 --- a/src/routes/console/project-[project]/functions/function-[function]/settings/uploadVariables.svelte +++ b/src/routes/console/project-[project]/functions/function-[function]/settings/uploadVariables.svelte @@ -6,69 +6,66 @@ import { Button, InputFile } from '$lib/elements/forms'; import { addNotification } from '$lib/stores/notifications'; import { sdk } from '$lib/stores/sdk'; - import { createEventDispatcher } from 'svelte'; + import { parse } from 'dotenv'; + import { variables } from './store'; export let show = false; - const dispatch = createEventDispatcher(); - const functionId = $page.params.function; + let files: FileList; + let error: string; - const handleSubmit = async () => { - if (files?.length) { - const variables = await parseFile(files[0]); - for (const variable of variables) { - try { - await sdk.forProject.functions.createVariable( - functionId, - variable.key, - variable.value - ); - invalidate(Dependencies.VARIABLES); - addNotification({ - type: 'success', - message: 'Variable uploaded' - }); - } catch (error) { - addNotification({ - type: 'error', - message: error.message - }); - } + async function handleSubmit() { + try { + if (!files?.length) { + throw new Error('No file selected'); } - dispatch('uploaded', variables); - } else { - addNotification({ - type: 'error', - message: 'No file uploaded' - }); - } - }; - async function parseFile(file: File) { - if (file) { - let variables = []; - let text = await file.text(); - text.split('\n').forEach((line) => { - const [key, value] = line.split('='); - variables.push({ key, value }); + const uploaded = parse(await files[0].text()); + + if (!Object.keys(uploaded).length) { + throw new Error('No variables found'); + } + + await Promise.all( + Object.entries(uploaded) + .filter(([, value]) => !!value) + .map(([key, value]) => { + const found = $variables.find((variable) => variable.key === key); + return found + ? sdk.forProject.functions.updateVariable( + functionId, + found.$id, + key, + value + ) + : sdk.forProject.functions.createVariable(functionId, key, value); + }) + ); + + invalidate(Dependencies.VARIABLES); + addNotification({ + type: 'success', + message: 'Variables uploaded' }); - return variables; + show = false; + } catch (e) { + error = e.message; } } - + Upload Variables

Upload multiple variables via a .env file that will be passed to your function at runtime.

- + - +