Skip to content

The open alternative to Salesforce, designed for AI.

3.6 KB
Raw Blame
import { randomUUID } from 'node:crypto';
import { cp, readFile, rename, writeFile } from 'node:fs/promises';
import { join } from 'node:path';

import semver from 'semver';

import { APP_TEMPLATE_PACKAGE_VERSION } from '@/app/constants/app-template-package-version.constant';
import { APP_TEMPLATE_FIRST_PARTY_PACKAGES } from '@/app/constants/app-template-packages.constant';
import { getAppTemplateDirectory } from '@/app/get-app-template-directory';
import { printTypescriptValue } from '@/app/pull/print-typescript-value';
import { CLI_VERSION } from '@/constants/cli-version.constant';

export const renderAppTemplate = async ({
  appDirectory,
  appName,
  appDisplayName,
  appDescription,
  shouldIncludeExample = true,
}: {
  appDirectory: string;
  appName: string;
  appDisplayName: string;
  appDescription: string;
  shouldIncludeExample?: boolean;
}) => {
  const templateDirectory = getAppTemplateDirectory();
  const exampleDirectory = join(templateDirectory, 'example');

  await cp(templateDirectory, appDirectory, {
    recursive: true,
    filter: (source) => source !== exampleDirectory,
  });

  if (shouldIncludeExample) {
    await cp(join(exampleDirectory, 'files'), appDirectory, {
      recursive: true,
    });
  }

  await writeFile(join(appDirectory, 'yarn.lock'), '', { flag: 'a' });

  // npm excludes these dotfiles when packing the template.
  for (const [source, destination] of [
    ['gitignore', '.gitignore'],
    ['yarnrc.yml', '.yarnrc.yml'],
  ]) {
    await rename(join(appDirectory, source), join(appDirectory, destination));
  }

  if (shouldIncludeExample) {
    await rename(join(appDirectory, 'github'), join(appDirectory, '.github'));

    await cp(join(appDirectory, 'AGENTS.md'), join(appDirectory, 'CLAUDE.md'));

    for (const workflowName of ['ci.yml', 'cd.yml', 'publish.yml']) {
      const workflowPath = join(
        appDirectory,
        '.github',
        'workflows',
        workflowName,
      );
      const workflow = (await readFile(workflowPath, 'utf8')).replaceAll(
        'CLI-VERSION-TO-BE-GENERATED',
        CLI_VERSION,
      );

      await writeFile(workflowPath, workflow);
    }

    const identifiersPath = join(
      appDirectory,
      'src/constants/universal-identifiers.ts',
    );
    const identifiers = (await readFile(identifiersPath, 'utf8'))
      .replace(/'DISPLAY-NAME-TO-BE-GENERATED'/g, () =>
        printTypescriptValue({ value: appDisplayName }),
      )
      .replace(/'DESCRIPTION-TO-BE-GENERATED'/g, () =>
        printTypescriptValue({ value: appDescription }),
      )
      .replace(/UUID-TO-BE-GENERATED/g, () => randomUUID());

    await writeFile(identifiersPath, identifiers);
  }

  const packageJsonPath = join(appDirectory, 'package.json');
  const packageJson: {
    name: string;
    engines: Record<string, string>;
    scripts: Record<string, string>;
    devDependencies: Record<string, string>;
  } = JSON.parse(await readFile(packageJsonPath, 'utf8'));

  packageJson.name = appName;
  // Patch releases of the CLI can ship without a matching server release.
  packageJson.engines.twenty = `>=${semver.major(APP_TEMPLATE_PACKAGE_VERSION)}.${semver.minor(APP_TEMPLATE_PACKAGE_VERSION)}.0`;

  for (const packageName of APP_TEMPLATE_FIRST_PARTY_PACKAGES) {
    packageJson.devDependencies[packageName] = APP_TEMPLATE_PACKAGE_VERSION;
  }

  if (shouldIncludeExample) {
    const exampleScripts: Record<string, string> = JSON.parse(
      await readFile(join(exampleDirectory, 'scripts.json'), 'utf8'),
    );

    packageJson.scripts = { ...packageJson.scripts, ...exampleScripts };
  }

  await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2));
};