Skip to content

The open alternative to Salesforce, designed for AI.

Make app scaffolding reusable and fix template typechecking (#27073)

Weiko41e74676ddb2
diff --git a/packages/create-twenty-app/src/constants/template-package-version.ts b/packages/create-twenty-app/src/constants/template-package-version.ts
new file mode 100644
index 00000000000..6310f21f355
--- /dev/null
+++ b/packages/create-twenty-app/src/constants/template-package-version.ts
@@ -0,0 +1,3 @@
+import createTwentyAppPackageJson from '../../package.json';
+
+export const TEMPLATE_PACKAGE_VERSION = createTwentyAppPackageJson.version;
diff --git a/packages/create-twenty-app/src/constants/template/tsconfig.json b/packages/create-twenty-app/src/constants/template/tsconfig.json
index abacf4dcf6d..12aa0c23886 100644
--- a/packages/create-twenty-app/src/constants/template/tsconfig.json
+++ b/packages/create-twenty-app/src/constants/template/tsconfig.json
@@ -33,10 +33,5 @@
"**/*.test.ts",
"**/*.spec.ts",
"**/*.integration-test.ts"
- ],
- "references": [
- {
- "path": "./tsconfig.spec.json"
- }
]
}
diff --git a/packages/create-twenty-app/src/utils/__tests__/app-template.spec.ts b/packages/create-twenty-app/src/utils/__tests__/app-template.spec.ts
index cf592802f13..a62c4e7af5d 100644
--- a/packages/create-twenty-app/src/utils/__tests__/app-template.spec.ts
+++ b/packages/create-twenty-app/src/utils/__tests__/app-template.spec.ts
@@ -3,6 +3,7 @@ import * as fs from 'fs-extra';
import { tmpdir } from 'os';
import createTwentyAppPackageJson from 'package.json';
import { join } from 'path';
+import ts from 'typescript';
jest.mock('fs-extra', () => {
const actual = jest.requireActual('fs-extra');
@@ -116,6 +117,109 @@ describe('copyBaseApplicationProject', () => {
expect(uuidMatches).toHaveLength(2);
});
+ it.each([
+ ['quotes', "Owner's app", "Owner\\'s app"],
+ ['backslashes', 'C:\\apps', 'C:\\\\apps'],
+ ['line breaks', 'First\nSecond\rThird', 'First\\nSecond\\rThird'],
+ ['replacement tokens', '$& $` $$', '$& $` $$'],
+ ])(
+ 'should preserve %s in generated metadata',
+ async (_, value, escapedValue) => {
+ await copyBaseApplicationProject({
+ appName: 'my-test-app',
+ appDisplayName: value,
+ appDescription: value,
+ appDirectory: testAppDirectory,
+ });
+
+ const content = await fs.readFile(
+ join(testAppDirectory, UNIVERSAL_IDENTIFIERS_PATH),
+ 'utf8',
+ );
+
+ expect(content).toContain(`APP_DISPLAY_NAME = '${escapedValue}'`);
+ expect(content).toContain(`APP_DESCRIPTION = '${escapedValue}'`);
+ },
+ );
+
+ it('should copy a caller-provided template directory', async () => {
+ const templateDirectory = join(testAppDirectory, 'custom-template');
+ const customFileName = 'custom-template.txt';
+
+ await fs.ensureDir(templateDirectory);
+ await fs.writeFile(
+ join(templateDirectory, customFileName),
+ 'Custom template',
+ );
+ jest
+ .mocked(fs.copy)
+ .mockImplementationOnce(jest.requireActual<typeof fs>('fs-extra').copy);
+
+ await copyBaseApplicationProject({
+ appName: 'my-test-app',
+ appDisplayName: 'My Test App',
+ appDescription: '',
+ appDirectory: testAppDirectory,
+ templateDirectory,
+ });
+
+ expect(
+ await fs.readFile(join(testAppDirectory, customFileName), 'utf8'),
+ ).toBe('Custom template');
+ });
+
+ it('should pin first-party packages to a caller-provided version', async () => {
+ await copyBaseApplicationProject({
+ appName: 'my-test-app',
+ appDisplayName: 'My Test App',
+ appDescription: '',
+ appDirectory: testAppDirectory,
+ packageVersion: '2.45.0-canary.1',
+ });
+
+ const packageJson = await fs.readJson(
+ join(testAppDirectory, 'package.json'),
+ );
+
+ expect(packageJson.devDependencies).toEqual({
+ 'twenty-client-sdk': '2.45.0-canary.1',
+ 'twenty-sdk': '2.45.0-canary.1',
+ 'twenty-ui': '2.45.0-canary.1',
+ });
+ });
+
+ it('should typecheck app sources before the test project is built', async () => {
+ const templateDirectory = join(__dirname, '../../constants/template');
+ const templateConfig = await fs.readJson(
+ join(templateDirectory, 'tsconfig.json'),
+ );
+
+ await fs.writeJson(join(testAppDirectory, 'tsconfig.json'), templateConfig);
+ await fs.writeJson(join(testAppDirectory, 'tsconfig.spec.json'), {
+ extends: './tsconfig.json',
+ compilerOptions: { composite: true },
+ include: ['src/**/*.ts'],
+ });
+
+ const parsedConfig = ts.parseJsonConfigFileContent(
+ templateConfig,
+ ts.sys,
+ testAppDirectory,
+ { types: [] },
+ );
+ const program = ts.createProgram({
+ rootNames: parsedConfig.fileNames,
+ options: parsedConfig.options,
+ projectReferences: parsedConfig.projectReferences,
+ });
+
+ expect(parsedConfig.errors).toEqual([]);
+ expect(
+ program.getSourceFiles().map((sourceFile) => sourceFile.fileName),
+ ).toContain(join(testAppDirectory, UNIVERSAL_IDENTIFIERS_PATH));
+ expect(ts.getPreEmitDiagnostics(program)).toEqual([]);
+ });
+
it('should generate different UUIDs for each identifier', async () => {
await copyBaseApplicationProject({
appName: 'my-test-app',
diff --git a/packages/create-twenty-app/src/utils/app-template.ts b/packages/create-twenty-app/src/utils/app-template.ts
index 481b132ae48..2bd64ee9c1e 100644
--- a/packages/create-twenty-app/src/utils/app-template.ts
+++ b/packages/create-twenty-app/src/utils/app-template.ts
@@ -2,8 +2,8 @@ import * as fs from 'fs-extra';
import { join } from 'path';
import { v4 } from 'uuid';
-import { TEMPLATE_FIRST_PARTY_PACKAGES } from '@/constants/template-packages';
-import createTwentyAppPackageJson from 'package.json';
+import { TEMPLATE_PACKAGE_VERSION } from '../constants/template-package-version';
+import { TEMPLATE_FIRST_PARTY_PACKAGES } from '../constants/template-packages';
const SRC_FOLDER = 'src';
@@ -12,16 +12,20 @@ export const copyBaseApplicationProject = async ({
appDisplayName,
appDescription,
appDirectory,
+ templateDirectory = join(__dirname, './constants/template'),
+ packageVersion = TEMPLATE_PACKAGE_VERSION,
onProgress,
}: {
appName: string;
appDisplayName: string;
appDescription: string;
appDirectory: string;
+ templateDirectory?: string;
+ packageVersion?: string;
onProgress?: (message: string) => void;
}) => {
onProgress?.('Copying base template');
- await fs.copy(join(__dirname, './constants/template'), appDirectory);
+ await fs.copy(templateDirectory, appDirectory);
onProgress?.('Configuring dotfiles (.gitignore, .github, .yarnrc.yml)');
await renameDotfiles({ appDirectory });
@@ -39,7 +43,7 @@ export const copyBaseApplicationProject = async ({
});
onProgress?.('Updating package.json');
- await updatePackageJson({ appName, appDirectory });
+ await updatePackageJson({ appName, appDirectory, packageVersion });
};
// npm strips dotfiles from published packages, so they're stored without the dot and renamed after copying.
@@ -103,18 +107,31 @@ const generateUniversalIdentifiers = async ({
await fs.writeFile(
universalIdentifiersPath,
universalIdentifiersFileContent
- .replace('DISPLAY-NAME-TO-BE-GENERATED', appDisplayName)
- .replace('DESCRIPTION-TO-BE-GENERATED', appDescription)
+ .replace('DISPLAY-NAME-TO-BE-GENERATED', () =>
+ escapeSingleQuotedString(appDisplayName),
+ )
+ .replace('DESCRIPTION-TO-BE-GENERATED', () =>
+ escapeSingleQuotedString(appDescription),
+ )
.replace(/UUID-TO-BE-GENERATED/g, () => v4()),
);
};
+const escapeSingleQuotedString = (value: string) =>
+ value
+ .replace(/\\/g, '\\\\')
+ .replace(/'/g, "\\'")
+ .replace(/\n/g, '\\n')
+ .replace(/\r/g, '\\r');
+
const updatePackageJson = async ({
appName,
appDirectory,
+ packageVersion,
}: {
appName: string;
appDirectory: string;
+ packageVersion: string;
}) => {
const packageJson = await fs.readJson(join(appDirectory, 'package.json'));
@@ -122,8 +139,7 @@ const updatePackageJson = async ({
packageJson.name = appName;
for (const packageName of TEMPLATE_FIRST_PARTY_PACKAGES) {
- packageJson.devDependencies[packageName] =
- createTwentyAppPackageJson.version;
+ packageJson.devDependencies[packageName] = packageVersion;
}
await fs.writeFile(
diff --git a/packages/create-twenty-app/src/utils/convert-to-label.ts b/packages/create-twenty-app/src/utils/convert-to-label.ts
index e71d9db12ca..7c645273306 100644
--- a/packages/create-twenty-app/src/utils/convert-to-label.ts
+++ b/packages/create-twenty-app/src/utils/convert-to-label.ts
@@ -1,4 +1,4 @@
-import { startCase } from 'lodash';
+import startCase from 'lodash.startcase';
export const convertToLabel = (str: string) => {
const s = startCase(str).toLowerCase();