Skip to content

The open alternative to Salesforce, designed for AI.

7.8 KB
Raw Blame
import { join } from 'node:path';
import { isDefined } from 'twenty-shared/utils';

import { getToolingErrorContext } from '@/app/get-tooling-error-context';
import { assertPullPaths } from '@/app/pull/assert-pull-paths';
import { collectIdentifiers } from '@/app/pull/collect-identifiers';
import { getOverwrittenLocalChanges } from '@/app/pull/get-overwritten-local-changes';
import { prepareAppPull } from '@/app/pull/prepare-app-pull';
import { preparePullEntities } from '@/app/pull/prepare-pull-entities';
import { preserveNestedPullEntities } from '@/app/pull/preserve-nested-pull-entities';
import { reconcilePullBaseManifest } from '@/app/pull/reconcile-pull-base-manifest';
import { createPullBaseWrite } from '@/app/pull/create-pull-base-write';
import { readTargetBoundPullBase } from '@/app/pull/read-target-bound-pull-base';
import { resolveAppSystemPermissionFlag } from '@/app/project/resolve-app-system-permission-flag';
import { type PullAppOptions, type PullAppResult } from '@/app/pull/types';
import { type ToolingResult } from '@/app/types/tooling-result.type';
import { CliError } from '@/output/cli-error';
import { toPullManifest } from '@/app/pull/to-pull-manifest';
import { assertPullSdkExports } from '@/app/pull/assert-pull-sdk-exports';
import { applyPullWrites } from '@/app/pull/apply-pull-writes';
import { buildManifestEntityLabelByUniversalIdentifier } from '@/app/pull/build-manifest-entity-label-by-universal-identifier';
import { planPullWrites } from '@/app/pull/plan-pull-writes';
import { planDependencyWrites } from '@/app/pull/plan-dependency-writes';
import { planTranslationWrites } from '@/app/pull/plan-translation-writes';
import { scanProjectSourceFiles } from '@/app/source/scan-project-source-files';
import { updateSourceFingerprints } from '@/app/pull/update-source-fingerprints';

export const pullApplication = async (
  options: PullAppOptions,
): Promise<ToolingResult<PullAppResult>> => {
  const { appPath, signal } = options;
  try {
    const { applicationExport, target } = await prepareAppPull(options);
    const manifest = toPullManifest(applicationExport.manifest);
    const base = await readTargetBoundPullBase({
      appPath,
      target,
      applicationUniversalIdentifier:
        applicationExport.application.universalIdentifier,
    });
    const baseManifest = isDefined(base.manifest)
      ? toPullManifest(base.manifest)
      : null;
    const scannedFiles = await scanProjectSourceFiles({
      appPath,
      includeConfig: true,
      signal,
    });
    const unreconciledBaseIdentifiers = new Set(
      base.unreconciledUniversalIdentifiers,
    );
    const {
      entities,
      baseEntities,
      skipped: skippedEntities,
    } = preparePullEntities({
      manifest,
      baseManifest,
      scannedFiles,
      unreconciledUniversalIdentifiers: unreconciledBaseIdentifiers,
      systemPermissionFlag: resolveAppSystemPermissionFlag(appPath),
    });

    signal?.throwIfAborted();

    const workspaceUniversalIdentifiers = new Set(
      applicationExport.coverage.map((entry) => entry.universalIdentifier),
    );
    const plan = planPullWrites({
      entities,
      baseEntities,
      scannedFiles,
      workspaceUniversalIdentifiers,
      unreconciledUniversalIdentifiers: unreconciledBaseIdentifiers,
    });
    const protectedIdentifiers = new Set<string>();

    collectIdentifiers({ value: manifest, identifiers: protectedIdentifiers });
    collectIdentifiers({
      value: applicationExport.coverage,
      identifiers: protectedIdentifiers,
    });

    const frontComponentSourcePaths = scannedFiles
      .filter((file) => file.entityKey === 'frontComponents')
      .map((file) => join(appPath, file.relativePath));
    const translationPlan = await planTranslationWrites({
      appPath,
      manifest,
      baseManifest,
      frontComponentSourcePaths,
    });
    const safePlan = preserveNestedPullEntities({
      entities,
      baseEntities,
      protectedIdentifiers,
      writes: plan.writes,
      deletions: plan.deletions.filter(
        (deletion) =>
          !protectedIdentifiers.has(deletion.universalIdentifier.toLowerCase()),
      ),
      scannedFiles,
    });
    const dependencyPlan = await planDependencyWrites({
      appPath,
      files: applicationExport.files,
      sourceFingerprints: base.sourceFingerprints,
      dependencyFingerprints: base.dependencyFingerprints,
    });
    const writes = [
      ...safePlan.writes,
      ...translationPlan.writes,
      ...dependencyPlan.writes,
    ];
    const deletions = [
      ...safePlan.deletions,
      ...translationPlan.deletions.filter(
        (deletion) =>
          isDefined(baseManifest?.translations) &&
          deletion.universalIdentifier in baseManifest.translations,
      ),
    ];
    const skipped = [...skippedEntities, ...safePlan.skipped];
    const unreconciledUniversalIdentifiers = new Set(
      skipped.map((entry) => entry.universalIdentifier.toLowerCase()),
    );
    const finalWrite = createPullBaseWrite({
      target,
      manifest: reconcilePullBaseManifest({
        manifest: applicationExport.manifest,
        baseManifest: base.manifest,
        unreconciledUniversalIdentifiers,
        protectedIdentifiers,
      }),
      unreconciledUniversalIdentifiers: [...unreconciledUniversalIdentifiers],
      dependencyFingerprints: {
        ...base.dependencyFingerprints,
        ...dependencyPlan.fingerprints,
      },
      sourceFingerprints: updateSourceFingerprints({
        sourceFingerprints: {
          ...base.sourceFingerprints,
          ...dependencyPlan.unchangedSourceFingerprints,
        },
        writes,
        deletions,
      }),
    });

    await assertPullPaths({
      appPath,
      relativePaths: [
        ...writes.map((write) => write.relativePath),
        ...deletions.map((deletion) => deletion.relativePath),
        finalWrite.relativePath,
      ],
    });

    const overwrittenLocalChanges = await getOverwrittenLocalChanges({
      appPath,
      baseManifest,
      writes,
      deletions,
      frontComponentSourcePaths,
      baseEntities,
      sourceFingerprints: base.sourceFingerprints,
    });
    const entityLabelByUniversalIdentifier =
      buildManifestEntityLabelByUniversalIdentifier(manifest);

    assertPullSdkExports({ appPath, writes });
    signal?.throwIfAborted();
    await applyPullWrites({ appPath, writes, deletions, finalWrite, signal });

    return {
      success: true,
      data: {
        application: {
          universalIdentifier:
            applicationExport.application.universalIdentifier,
          displayName: applicationExport.application.displayName,
        },
        base: { status: base.status },
        writes: writes.map(
          ({
            content: _content,
            requiredSdkExports: _requiredSdkExports,
            ...write
          }) => write,
        ),
        deletions,
        overwrittenLocalChanges,
        unchangedCount: plan.unchanged.length + dependencyPlan.unchangedCount,
        localOnlyRelativePaths: plan.localOnlyRelativePaths,
        unreadableRelativePaths: scannedFiles
          .filter((file) => !file.isReadable)
          .map((file) => file.relativePath),
        skipped,
        compiledTranslationEntryCountByLocale:
          translationPlan.compiledEntryCountByLocale,
        entityLabelByUniversalIdentifier,
      },
      diagnostics: [],
    };
  } catch (error) {
    return {
      success: false,
      error: {
        code:
          signal?.aborted && Object.is(error, signal.reason)
            ? 'CANCELLED'
            : error instanceof CliError
              ? error.code
              : 'PULL_FAILED',
        message: error instanceof Error ? error.message : String(error),
        ...getToolingErrorContext(error),
        details: {
          outcome: 'unchanged',
          ...(error instanceof CliError ? error.details : {}),
        },
      },
      diagnostics: [],
    };
  }
};