Skip to content

The open alternative to Salesforce, designed for AI.

4.9 KB
Raw Blame
import { createHash } from 'node:crypto';
import { readFile, realpath } from 'node:fs/promises';
import { join } from 'node:path';
import { createRequire } from 'node:module';
import { resolveInsideSdk } from '@/app/project/resolve-inside-sdk';

import { isDefined } from 'twenty-shared/utils';

import { type AppApplyResult } from '@/app/deployment/apply-app-build';
import { createPostSyncFailure } from '@/app/deployment/create-post-sync-failure';
import { readGeneratedClientStamp } from '@/app/dev/read-generated-client-stamp';
import { type BuiltDevSnapshot } from '@/app/dev/build-dev-snapshot';
import {
  fetchAppClientSchema,
  generateAppClientFromSchema,
} from '@/app/generate-app-client';
import { getClientGenerationSkipReason } from '@/app/get-client-generation-skip-reason';
import { getClientSdkPackagePath } from '@/app/get-client-sdk-package-path';
import { type TargetCommandContext } from '@/catalog/types/target-command-context.type';
import { withPhaseDetails } from '@/app/deployment/with-phase-details';
import { toCliError } from '@/output/to-cli-error';

export const createDevClientGenerator = ({ appPath }: { appPath: string }) => {
  let generatedKey: string | undefined;
  let generatedOutputStamp: string | undefined;
  const readGeneratedOutputStamp = () =>
    readGeneratedClientStamp(getClientSdkPackagePath({ appPath }));

  return async ({
    snapshot,
    applied,
    context,
    withBuildsPaused,
    invalidate,
  }: {
    snapshot: BuiltDevSnapshot;
    applied: AppApplyResult;
    context: TargetCommandContext;
    withBuildsPaused: <TResult>(
      run: () => Promise<TResult>,
    ) => Promise<TResult>;
    invalidate: () => void;
  }) => {
    const skipReason = await getClientGenerationSkipReason({
      appPath,
    });

    if (isDefined(skipReason)) {
      context.output.warn({
        code: 'CLIENT_NOT_GENERATED',
        message: skipReason,
      });

      return 'skipped';
    }

    let schema: string;
    let generationKey: string;
    let currentGeneratedOutputStamp: string;

    try {
      schema = await fetchAppClientSchema({
        applicationUniversalIdentifier: applied.acknowledgedUniversalIdentifier,
        context,
      });
      const packageRoot = await realpath(getClientSdkPackagePath({ appPath }));
      const packageJson = await readFile(join(packageRoot, 'package.json'));

      const generatorPath = resolveInsideSdk({
        resolveFromApp: createRequire(join(packageRoot, 'package.json'))
          .resolve,
        specifier: 'twenty-client-sdk/generate',
        sdkPath: packageRoot,
      });

      if (!isDefined(generatorPath)) {
        throw new Error(
          'The installed twenty-client-sdk has no compatible generate entry point.',
        );
      }

      generationKey = createHash('sha256')
        .update(schema)
        .update(packageRoot)
        .update(packageJson)
        .update(await readFile(generatorPath))
        .digest('hex');
      currentGeneratedOutputStamp = await readGeneratedOutputStamp();
    } catch (error) {
      if (context.signal.aborted) {
        throw withPhaseDetails({
          error: toCliError(error, context.signal),
          phase: 'clientGeneration',
          outcome: 'applied',
          completedPhases: applied.completedPhases,
          hint: 'The app was synced. Client generation had not started, so the previous typed API client was kept.',
        });
      }

      context.output.warn({
        code: 'CLIENT_NOT_GENERATED',
        message: `The app was synced, but its client inputs could not be fetched: ${toCliError(error).message} The previous client was kept.`,
      });

      return 'skipped';
    }

    if (
      generationKey === generatedKey &&
      currentGeneratedOutputStamp === generatedOutputStamp
    ) {
      return 'unchanged';
    }

    let hasStartedGeneration = false;

    try {
      await withBuildsPaused(async () => {
        context.signal.throwIfAborted();
        hasStartedGeneration = true;
        await generateAppClientFromSchema({
          appPath,
          schema,
          sdk: snapshot.tooling,
          context,
        });
        generatedKey = generationKey;
        generatedOutputStamp = await readGeneratedOutputStamp();
        applied.completedPhases.push('clientGeneration');
        invalidate();
      });
    } catch (error) {
      if (!hasStartedGeneration) {
        throw withPhaseDetails({
          error: toCliError(error, context.signal),
          phase: 'clientGeneration',
          outcome: 'applied',
          completedPhases: applied.completedPhases,
          hint: 'The app was synced. Client generation had not started, so the previous typed API client was kept.',
        });
      }

      throw createPostSyncFailure({
        error,
        phase: 'clientGeneration',
        applicationName: snapshot.build.application.displayName,
        apiUrl: context.target.apiUrl,
        completedPhases: applied.completedPhases,
        signal: context.signal,
      });
    }

    return 'generated';
  };
};