Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🎸 Associate function to a site on creation #59

Merged
merged 14 commits into from
Oct 31, 2024
3 changes: 2 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -357,5 +357,6 @@
"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",
gabrielmpinto marked this conversation as resolved.
Show resolved Hide resolved
"functionsSite": "The ID of the site you want to deploy your function to"
gabrielmpinto marked this conversation as resolved.
Show resolved Hide resolved
}
14 changes: 12 additions & 2 deletions src/commands/functions/create.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
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 }))) {
gabrielmpinto marked this conversation as resolved.
Show resolved Hide resolved
throw new SiteNotFoundError({ site: { id: siteId } });
heldrida marked this conversation as resolved.
Show resolved Hide resolved
}

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

output.printNewLine();
output.success(t('commonNameCreateSuccess', { name: 'function' }));
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
14 changes: 14 additions & 0 deletions src/commands/functions/utils/isSiteIdValid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { FleekSdk } from '@fleek-platform/sdk/node';

export const isSiteIdValid = ({
siteId,
sdk,
}: { siteId: string; sdk: FleekSdk }) => {
if (!siteId) return true;
gabrielmpinto marked this conversation as resolved.
Show resolved Hide resolved

return sdk
.sites()
.get({ id: siteId })
.then(() => true)
.catch(() => false);
};