Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/codegen/configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,7 @@ export function realizeConfiguration(

const generatorIds: string[] = [];
for (const [index, generator] of config.generators.entries()) {
const language =
(generator as any).language !== undefined
? (generator as any).language
: config.language ?? 'typescript';
const language = generator.language ?? config.language ?? 'typescript';
if (!generator?.preset) {
continue;
}
Expand Down
11 changes: 8 additions & 3 deletions src/codegen/generators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ export async function runGenerators(context: RunGeneratorContext) {
/**
* Load the configuration and run the generator
*
* @param configFile
* @param configFileOrContext Either a config file path or a pre-realized RunGeneratorContext
*/
export async function generateWithConfig(configFile: string | undefined) {
const context = await realizeGeneratorContext(configFile);
export async function generateWithConfig(
configFileOrContext: string | undefined | RunGeneratorContext
) {
const context =
typeof configFileOrContext === 'string' || configFileOrContext === undefined
? await realizeGeneratorContext(configFileOrContext)
: configFileOrContext;
await runGenerators(context);
}
2 changes: 1 addition & 1 deletion src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default class Generate extends Command {
await this.handleWatchModeStartedTelemetry({context, inputSource});
await this.runWithWatch({configFile: file, watchPath});
} else {
await generateWithConfig(file);
await generateWithConfig(context);
}

await this.handleGeneratorUsageTelemetry({context, inputSource});
Expand Down
8 changes: 8 additions & 0 deletions src/telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ export async function trackEvent(
projectConfig?: ProjectTelemetryConfig
): Promise<void> {
try {
// Debug mode: log the event being tracked
if (process.env.CODEGEN_TELEMETRY_DEBUG === '1') {
Logger.info(
'[Telemetry Debug] Tracking event:',
JSON.stringify(event, null, 2)
);
}

// Show notice on first run (also safe, never throws)
await showTelemetryNoticeIfNeeded();

Expand Down
20 changes: 20 additions & 0 deletions src/telemetry/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,28 @@ export async function sendEvent(
try {
const config = await getTelemetryConfig(projectConfig);

// Debug mode: log config state
if (process.env.CODEGEN_TELEMETRY_DEBUG === '1') {
Logger.info(
'[Telemetry Debug] Config:',
JSON.stringify(
{
enabled: config.enabled,
hasEndpoint: !!config.endpoint,
hasTrackingId: !!config.trackingId,
hasAnonymousId: !!config.anonymousId
},
null,
2
)
);
}

// Don't send if telemetry is disabled
if (!config.enabled) {
if (process.env.CODEGEN_TELEMETRY_DEBUG === '1') {
Logger.info('[Telemetry Debug] Telemetry is disabled, skipping send');
}
return;
}

Expand Down
32 changes: 27 additions & 5 deletions test/codegen/generators/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
//import { generateWithConfig } from "../../../src/codegen/generators";
//import path from "path";
import { generateWithConfig } from "../../../src/codegen/generators";
import { realizeGeneratorContext } from "../../../src/codegen/configurations";
import path from "path";

jest.mock('node:fs/promises');
jest.mock('node:fs');

// Mock the renderer to avoid actual file generation in tests
jest.mock('../../../src/codegen/renderer', () => ({
determineRenderGraph: jest.fn(() => new Map()),
renderGraph: jest.fn()
}));

describe('generateWithConfig', () => {
test('should generate model correctly', async () => {
//await generateWithConfig(path.resolve(__dirname, '../../configs/config-implicit.js'));
expect(true).toEqual(true);
test('should accept a string config path', async () => {
const configPath = path.resolve(__dirname, '../../configs/config.js');
await expect(generateWithConfig(configPath)).resolves.not.toThrow();
});

test('should accept undefined to search for config', async () => {
// This will search for config in current directory
// Should not throw even if no config found due to mocking
await expect(generateWithConfig(undefined)).rejects.toThrow();
});

test('should accept a pre-realized RunGeneratorContext', async () => {
const configPath = path.resolve(__dirname, '../../configs/config.js');
const context = await realizeGeneratorContext(configPath);

// Should not throw when passed a context object
await expect(generateWithConfig(context)).resolves.not.toThrow();
});
});