Skip to content

Commit

Permalink
Return project name with runtime info (#1330)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDoyle authored and hugoaguirre committed Nov 25, 2024
1 parent b1e5ab8 commit fa6f88f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
8 changes: 7 additions & 1 deletion genkit-tools/common/src/manager/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ import {
import * as apis from '../types/apis';
import { TraceData } from '../types/trace';
import { logger } from '../utils/logger';
import { checkServerHealth, findRuntimesDir, retriable } from '../utils/utils';
import {
checkServerHealth,
findRuntimesDir,
projectNameFromGenkitFilePath,
retriable,
} from '../utils/utils';
import {
GenkitToolsError,
RuntimeEvent,
Expand Down Expand Up @@ -312,6 +317,7 @@ export class RuntimeManager {
async () => {
const content = await fs.readFile(filePath, 'utf-8');
const runtimeInfo = JSON.parse(content) as RuntimeInfo;
runtimeInfo.projectName = projectNameFromGenkitFilePath(filePath);
return { content, runtimeInfo };
},
{ maxRetries: 10, delayMs: 500 }
Expand Down
2 changes: 2 additions & 0 deletions genkit-tools/common/src/manager/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export interface RuntimeInfo {
reflectionServerUrl: string;
/** Timestamp when the runtime was started. */
timestamp: string;
/** Display name for the project, typically basename of the root folder */
projectName?: string;
}

export enum RuntimeEvent {
Expand Down
23 changes: 23 additions & 0 deletions genkit-tools/common/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,29 @@ export async function findServersDir(projectRoot?: string): Promise<string> {
return path.join(root, '.genkit', 'servers');
}

/**
* Extract project name (i.e. basename of the project root folder) from the
* path to a runtime file.
*
* e.g.) /path/to/<basename>/.genkit/runtimes/123.json returns <basename>.
*
* @param filePath path to a runtime file
* @returns project name
*/
export function projectNameFromGenkitFilePath(filePath: string): string {
const parts = filePath.split('/');
const basePath = parts
.slice(
0,
Math.max(
parts.findIndex((value) => value === '.genkit'),
0
)
)
.join('/');
return basePath === '' ? 'unknown' : path.basename(basePath);
}

/**
* Detects what runtime is used in the current directory.
* @returns Runtime of the project directory.
Expand Down
48 changes: 48 additions & 0 deletions genkit-tools/common/tests/utils/utils_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { describe, expect, it } from '@jest/globals';
import { projectNameFromGenkitFilePath } from '../../src/utils';

describe('utils', () => {
describe('projectNameFromGenkitFilePath', () => {
it('returns unknown for empty string', () => {
expect(projectNameFromGenkitFilePath('')).toEqual('unknown');
});

it('returns unknown for an invalid path', () => {
expect(projectNameFromGenkitFilePath('/path/to/nowhere')).toEqual(
'unknown'
);
});

it('returns project name from a typical runtime file path', () => {
expect(
projectNameFromGenkitFilePath(
'/path/to/test-project/.genkit/runtimes/123.json'
)
).toEqual('test-project');
});

it('returns project name from any path that contains a .genkit dir', () => {
expect(
projectNameFromGenkitFilePath(
'/path/to/test-project/.genkit/unexpected/but/valid/location'
)
).toEqual('test-project');
});
});
});

0 comments on commit fa6f88f

Please sign in to comment.