Skip to content

Commit

Permalink
chore: Merge remote-tracking branch 'origin/develop' into chore/add-f…
Browse files Browse the repository at this point in the history
…unctions-esbuild-config
  • Loading branch information
heldrida committed Oct 31, 2024
2 parents a4714bd + 06037b3 commit ba4b4c9
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-foxes-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fleek-platform/cli": minor
---

Associate function to a site on creation
4 changes: 3 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -357,5 +357,7 @@
"uploadingAssets": "Uploading assets...",
"assetsUploadSuccess": "Assets uploaded successfully",
"uploadAssetsFailed": "Failed to upload function assets to Fleek. Check your network connection and try again.",
"assetsNotSupportedInSgx": "Function assets are not supported in SGX"
"assetsNotSupportedInSgx": "Function assets are not supported in SGX environment",
"functionsSite": "The Site ID where the Function will be deployed",
"siteNotFound": "Site not found!"
}
16 changes: 14 additions & 2 deletions src/commands/functions/create.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import { SiteNotFoundError } from '@fleek-platform/errors';
import { output } from '../../cli';
import type { SdkGuardedFunction } from '../../guards/types';
import { withGuards } from '../../guards/withGuards';
import { t } from '../../utils/translation';
import { getFunctionNameOrPrompt } from './prompts/getFunctionNameOrPrompt';
import { isSiteIdValid } from './utils/isSiteIdValid';

type CreateFunctionArgs = {
name?: string;
siteId?: string;
};

const createAction: SdkGuardedFunction<CreateFunctionArgs> = async ({
args,
sdk,
}) => {
const functionName = await getFunctionNameOrPrompt({ name: args.name });
const { name, siteId } = args;
const functionName = await getFunctionNameOrPrompt({ name });

const newFunction = await sdk.functions().create({ name: functionName });
if (!siteId || !(await isSiteIdValid({ siteId, sdk }))) {
output.error(t(`siteNotFound`));
return;
}

const newFunction = await sdk.functions().create({
name: functionName,
siteId: siteId as string,
});

output.printNewLine();
output.success(t('commonNameCreateSuccess', { name: 'function' }));
Expand Down
8 changes: 6 additions & 2 deletions src/commands/functions/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs from 'node:fs';
import { isValidFolder } from '@fleek-platform/utils-validation';
import cliProgress from 'cli-progress';

import { output } from '../../cli';
Expand Down Expand Up @@ -62,12 +61,17 @@ const deployAction: SdkGuardedFunction<DeployActionArgs> = async ({
functionName: functionToDeploy.name,
});

const updatedEnv = {
FLEEK_URL: functionToDeploy.invokeUrl,
...env,
};

const filePathToUpload = isSGX
? await getWasmCodeFromPath({ filePath })
: await getJsCodeFromPath({
filePath,
bundle,
env,
env: updatedEnv,
assetsCid,
});

Expand Down
5 changes: 3 additions & 2 deletions src/commands/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ export default (program: Command): Command => {
cmd
.command('create')
.option('-n, --name <functionName>', t('functionName'))
.option('--site <siteId>', t('functionsSite'))
.description(t('functionsCreateDescription'))
.action((options: { name?: string }) =>
createActionHandler({ name: options.name }),
.action((options: { name?: string; site?: string }) =>
createActionHandler({ name: options.name, siteId: options.site }),
);

cmd
Expand Down
16 changes: 16 additions & 0 deletions src/commands/functions/utils/isSiteIdValid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { FleekSdk } from '@fleek-platform/sdk/node';

export const isSiteIdValid = async ({
siteId,
sdk,
}: {
siteId: string;
sdk: FleekSdk;
}) => {
try {
await sdk.sites().get({ id: siteId });
return true;
} catch {
return false;
}
};

0 comments on commit ba4b4c9

Please sign in to comment.