Skip to content

The open alternative to Salesforce, designed for AI.

feat(cli): show where app generate-client saves the client (#28087)

Weikoe75cc3150e48
diff --git a/packages/twenty-cli/src/app/client/generate-application-client.ts b/packages/twenty-cli/src/app/client/generate-application-client.ts
index 0e56a668c7f..d881bb1f71e 100644
--- a/packages/twenty-cli/src/app/client/generate-application-client.ts
+++ b/packages/twenty-cli/src/app/client/generate-application-client.ts
@@ -5,6 +5,7 @@ import { isFunction, isNonEmptyString } from '@sniptt/guards';
import { isDefined, isPlainObject } from 'twenty-shared/utils';
import { type ToolingResult } from '@/app/types/tooling-result.type';
+import { getClientSdkPackagePath } from '@/app/get-client-sdk-package-path';
import { readJsonObject } from '@/app/read-json-object';
import { resolveInsideSdk } from '@/app/project/resolve-inside-sdk';
import { validateAppPath } from '@/app/snapshots/validate-app-path';
@@ -31,7 +32,7 @@ export const generateApplicationClient = async ({
throw new Error('schema must be a non-empty GraphQL schema string.');
}
- const packageRoot = join(appPath, 'node_modules', 'twenty-client-sdk');
+ const packageRoot = getClientSdkPackagePath({ appPath });
const packageJson = await readJsonObject(join(packageRoot, 'package.json'));
if (packageJson?.name !== 'twenty-client-sdk') {
diff --git a/packages/twenty-cli/src/app/dev/generate-dev-client.ts b/packages/twenty-cli/src/app/dev/generate-dev-client.ts
index 02c3df23d79..e586e9b7b8e 100644
--- a/packages/twenty-cli/src/app/dev/generate-dev-client.ts
+++ b/packages/twenty-cli/src/app/dev/generate-dev-client.ts
@@ -15,6 +15,7 @@ import {
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';
@@ -23,9 +24,7 @@ export const createDevClientGenerator = ({ appPath }: { appPath: string }) => {
let generatedKey: string | undefined;
let generatedOutputStamp: string | undefined;
const readGeneratedOutputStamp = () =>
- readGeneratedClientStamp(
- join(appPath, 'node_modules', 'twenty-client-sdk'),
- );
+ readGeneratedClientStamp(getClientSdkPackagePath({ appPath }));
return async ({
snapshot,
@@ -64,9 +63,7 @@ export const createDevClientGenerator = ({ appPath }: { appPath: string }) => {
applicationUniversalIdentifier: applied.acknowledgedUniversalIdentifier,
context,
});
- const packageRoot = await realpath(
- join(appPath, 'node_modules', 'twenty-client-sdk'),
- );
+ const packageRoot = await realpath(getClientSdkPackagePath({ appPath }));
const packageJson = await readFile(join(packageRoot, 'package.json'));
const generatorPath = resolveInsideSdk({
diff --git a/packages/twenty-cli/src/app/get-client-generation-skip-reason.ts b/packages/twenty-cli/src/app/get-client-generation-skip-reason.ts
index ac68820376c..d8a64ed41d5 100644
--- a/packages/twenty-cli/src/app/get-client-generation-skip-reason.ts
+++ b/packages/twenty-cli/src/app/get-client-generation-skip-reason.ts
@@ -1,6 +1,6 @@
import { lstat } from 'node:fs/promises';
-import { join } from 'node:path';
+import { getClientSdkPackagePath } from '@/app/get-client-sdk-package-path';
import { hasErrorCode } from '@/utils/has-error-code';
export const getClientGenerationSkipReason = async ({
@@ -9,7 +9,7 @@ export const getClientGenerationSkipReason = async ({
appPath: string;
}) => {
const isClientPackageMissing = await lstat(
- join(appPath, 'node_modules', 'twenty-client-sdk'),
+ getClientSdkPackagePath({ appPath }),
).then(
() => false,
(error: unknown) => hasErrorCode(error, 'ENOENT'),
diff --git a/packages/twenty-cli/src/app/get-client-sdk-package-path.ts b/packages/twenty-cli/src/app/get-client-sdk-package-path.ts
new file mode 100644
index 00000000000..de5cabf1463
--- /dev/null
+++ b/packages/twenty-cli/src/app/get-client-sdk-package-path.ts
@@ -0,0 +1,4 @@
+import { join } from 'node:path';
+
+export const getClientSdkPackagePath = ({ appPath }: { appPath: string }) =>
+ join(appPath, 'node_modules', 'twenty-client-sdk');
diff --git a/packages/twenty-cli/src/commands/app/__tests__/app-generate-client.spec.ts b/packages/twenty-cli/src/commands/app/__tests__/app-generate-client.spec.ts
index f865ff82675..43b3d996a1d 100644
--- a/packages/twenty-cli/src/commands/app/__tests__/app-generate-client.spec.ts
+++ b/packages/twenty-cli/src/commands/app/__tests__/app-generate-client.spec.ts
@@ -3,7 +3,7 @@ import { writeTestSourceSdk } from '@/app/__tests__/utils/write-test-source-sdk'
import { existsSync } from 'node:fs';
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
-import { join } from 'node:path';
+import { basename, dirname, join } from 'node:path';
import { readGraphqlRequest } from '@/__tests__/utils/read-graphql-request';
import {
@@ -121,6 +121,7 @@ describe('app generate-client', () => {
expect(parseSingleJsonLine(stdout).data).toMatchObject({
app: { path: appPath, name: 'generate-app' },
sdk: { version: '9.9.9' },
+ client: { path: join(appPath, 'node_modules', 'twenty-client-sdk') },
application: { displayName: 'Generate App' },
});
expect(server.requests).toHaveLength(1);
@@ -137,16 +138,49 @@ describe('app generate-client', () => {
expectIdentityReadOnce();
});
- it('confirms the generated client in human output', async () => {
+ it('confirms the generated client and its location in cyan in human output', async () => {
+ vi.stubEnv('NO_COLOR', undefined);
+ vi.stubEnv('FORCE_COLOR', '1');
+
const { stdout, exitCode } = await run();
expect(exitCode).toBe(0);
expect(stdout).toContain(
`Generated the typed API client for Generate App from ${server.url}`,
);
+ expect(stdout).toContain(
+ `Saved in \u001b[36m${join(appPath, 'node_modules', 'twenty-client-sdk')}\u001b[39m`,
+ );
expectIdentityReadOnce();
});
+ it('shows the client location relative to the current folder when the app is inside it', async () => {
+ vi.stubEnv('FORCE_COLOR', undefined);
+ vi.stubEnv('NO_COLOR', '1');
+ const workingDirectory = vi.spyOn(process, 'cwd');
+
+ try {
+ workingDirectory.mockReturnValue(appPath);
+ const fromApp = await run();
+ const fromAppJson = await run('--json');
+
+ workingDirectory.mockReturnValue(dirname(appPath));
+ const fromParent = await run();
+
+ expect(fromApp.stdout).toContain(
+ `Saved in ${join('node_modules', 'twenty-client-sdk')}`,
+ );
+ expect(fromParent.stdout).toContain(
+ `Saved in ${join(basename(appPath), 'node_modules', 'twenty-client-sdk')}`,
+ );
+ expect(parseSingleJsonLine(fromAppJson.stdout).data.client).toEqual({
+ path: join(appPath, 'node_modules', 'twenty-client-sdk'),
+ });
+ } finally {
+ workingDirectory.mockRestore();
+ }
+ });
+
it('refuses an app without its own twenty-client-sdk before contacting the workspace', async () => {
await rm(join(appPath, 'node_modules', 'twenty-client-sdk'), {
recursive: true,
diff --git a/packages/twenty-cli/src/commands/app/generate-client/run-app-generate-client-command.ts b/packages/twenty-cli/src/commands/app/generate-client/run-app-generate-client-command.ts
index cead682817d..355e20b7665 100644
--- a/packages/twenty-cli/src/commands/app/generate-client/run-app-generate-client-command.ts
+++ b/packages/twenty-cli/src/commands/app/generate-client/run-app-generate-client-command.ts
@@ -4,6 +4,7 @@ import { createAppNotInstalledError } from '@/app/create-app-not-installed-error
import { formatToolingDiagnostic } from '@/app/format-tooling-diagnostic';
import { generateAppClient } from '@/app/generate-app-client';
import { getClientGenerationSkipReason } from '@/app/get-client-generation-skip-reason';
+import { getClientSdkPackagePath } from '@/app/get-client-sdk-package-path';
import { isApplicationNotFoundError } from '@/app/is-application-not-found-error';
import { resolveAppProject } from '@/app/project/resolve-app-project';
import { resolveSourceSdk } from '@/app/project/resolve-source-sdk';
@@ -13,16 +14,18 @@ import { type CommandRun } from '@/catalog/types/command-run.type';
import { type TargetCommandContext } from '@/catalog/types/target-command-context.type';
import { CliError } from '@/output/cli-error';
import { EXIT_CODE } from '@/output/constants/exit-code.constant';
-import { formatSuccessLine } from '@/output/style';
+import { formatDisplayPath } from '@/output/format-display-path';
+import { colorText, formatSuccessLine } from '@/output/style';
export const runAppGenerateClientCommand: CommandRun<
TargetCommandContext
> = async (context) => {
const { options, output, outputMode, signal, target } = context;
const startedAt = performance.now();
+ const workingDirectory = process.cwd();
const project = await resolveAppProject({
explicitPath: readStringOption(options, 'path'),
- workingDirectory: process.cwd(),
+ workingDirectory,
});
const skipReason = await getClientGenerationSkipReason({
appPath: project.path,
@@ -78,17 +81,26 @@ export const runAppGenerateClientCommand: CommandRun<
throw error;
});
+ const clientPath = getClientSdkPackagePath({ appPath: project.path });
+ const clientDisplayPath = formatDisplayPath({
+ workingDirectory,
+ targetPath: clientPath,
+ });
return {
data: {
app: { path: project.path, name: project.name },
sdk: { version: sdk.version },
+ client: { path: clientPath },
application: identity.application,
diagnostics: [...identity.diagnostics, ...generationDiagnostics],
durationMilliseconds: Math.round(performance.now() - startedAt),
},
- human: formatSuccessLine(
- `Generated the typed API client for ${identity.application.displayName ?? project.name} from ${target.apiUrl}`,
- ),
+ human: [
+ formatSuccessLine(
+ `Generated the typed API client for ${identity.application.displayName ?? project.name} from ${target.apiUrl}`,
+ ),
+ ` Saved in ${colorText('cyan', clientDisplayPath)}`,
+ ].join('\n'),
};
};
diff --git a/packages/twenty-cli/src/commands/app/init/run-app-init-command.ts b/packages/twenty-cli/src/commands/app/init/run-app-init-command.ts
index c8e958c4805..6f5511f2f17 100644
--- a/packages/twenty-cli/src/commands/app/init/run-app-init-command.ts
+++ b/packages/twenty-cli/src/commands/app/init/run-app-init-command.ts
@@ -1,4 +1,4 @@
-import { basename, isAbsolute, relative, resolve } from 'node:path';
+import { basename, resolve } from 'node:path';
import { isNonEmptyString } from '@sniptt/guards';
@@ -16,26 +16,9 @@ import {
import { type CommandRun } from '@/catalog/types/command-run.type';
import { CliError } from '@/output/cli-error';
import { EXIT_CODE } from '@/output/constants/exit-code.constant';
+import { formatDisplayPath } from '@/output/format-display-path';
import { hasConfiguredTarget } from '@/target/has-configured-target';
-const toDisplayPath = ({
- workingDirectory,
- appDirectory,
-}: {
- workingDirectory: string;
- appDirectory: string;
-}) => {
- const relativePath = relative(workingDirectory, appDirectory);
-
- if (relativePath === '') {
- return '.';
- }
-
- return relativePath.startsWith('..') || isAbsolute(relativePath)
- ? appDirectory
- : relativePath;
-};
-
export const runAppInitCommand: CommandRun = async ({
arguments: commandArguments,
options,
@@ -63,7 +46,10 @@ export const runAppInitCommand: CommandRun = async ({
? displayName
: convertToLabel(appName);
const appDescription = readStringOption(options, 'description')?.trim() ?? '';
- const displayPath = toDisplayPath({ workingDirectory, appDirectory });
+ const displayPath = formatDisplayPath({
+ workingDirectory,
+ targetPath: appDirectory,
+ });
const location = displayPath === '.' ? 'the current directory' : displayPath;
output.progress(`Creating ${appName} in ${location}…`);
diff --git a/packages/twenty-cli/src/output/format-display-path.ts b/packages/twenty-cli/src/output/format-display-path.ts
new file mode 100644
index 00000000000..911fc2ba991
--- /dev/null
+++ b/packages/twenty-cli/src/output/format-display-path.ts
@@ -0,0 +1,24 @@
+import { relative } from 'node:path';
+
+import { isInsideDirectory } from '@/utils/is-inside-directory';
+
+export const formatDisplayPath = ({
+ workingDirectory,
+ targetPath,
+}: {
+ workingDirectory: string;
+ targetPath: string;
+}) => {
+ const relativePath = relative(workingDirectory, targetPath);
+
+ if (relativePath === '') {
+ return '.';
+ }
+
+ return isInsideDirectory({
+ filePath: targetPath,
+ directory: workingDirectory,
+ })
+ ? relativePath
+ : targetPath;
+};