Skip to content

Commit 9cb10ef

Browse files
authored
Write tools config in genkit init. (#117)
1 parent 0ed8e6b commit 9cb10ef

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
runner: {
3+
mode: 'harness',
4+
files: [$GENKIT_HARNESS_FILES],
5+
},
6+
};

genkit-tools/cli/src/commands/init.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ const sampleTemplatePaths: Record<Platform, string> = {
150150
nextjs: '../../config/nextjs.genkit.ts.template',
151151
};
152152

153+
const nextjsToolsConfigTemplatePath =
154+
'../../config/nextjs.genkit-tools.config.js.template';
155+
153156
/** Supported runtimes for the init command. */
154157
const supportedRuntimes: Runtime[] = ['node'];
155158

@@ -250,6 +253,7 @@ export const init = new Command('init')
250253
) {
251254
generateSampleFile(platform, modelOptions[model].plugin, plugins);
252255
}
256+
generateToolsConfig(platform);
253257
} catch (error) {
254258
logger.error(error);
255259
process.exit(1);
@@ -325,6 +329,28 @@ async function updatePackageJson() {
325329
fs.writeFileSync(packageJsonPath, JSON.stringify(newPackageJson, null, 2));
326330
}
327331

332+
/**
333+
* Generates an appropriate tools config for the given platform.
334+
* @param platform platform
335+
*/
336+
function generateToolsConfig(platform: Platform) {
337+
if (platform === 'nextjs') {
338+
const templatePath = path.join(__dirname, nextjsToolsConfigTemplatePath);
339+
let template = fs.readFileSync(templatePath, 'utf8');
340+
if (fs.existsSync('src/app')) {
341+
template = template.replace('$GENKIT_HARNESS_FILES', `'./src/app/*.ts'`);
342+
} else if (fs.existsSync('app')) {
343+
template = template.replace('$GENKIT_HARNESS_FILES', `'./app/*.js'`);
344+
} else {
345+
throw new Error(
346+
'Unable to resolve source folder (app or src/app) of you next.js app.'
347+
);
348+
}
349+
const configPath = path.join(process.cwd(), 'genkit-tools.conf.js');
350+
fs.writeFileSync(configPath, template, 'utf8');
351+
}
352+
}
353+
328354
/**
329355
* Generates a sample index.ts file.
330356
* @param platform Deployment platform.

genkit-tools/common/src/runner/runner.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,13 @@ export class Runner {
110110
}
111111
if (this.autoReload) {
112112
const config = await findToolsConfig();
113-
this.buildCommand = config?.builder?.cmd;
114-
if (!this.buildCommand && detectRuntime(process.cwd()) === 'node') {
115-
this.buildCommand = 'npm run build';
113+
if (config?.runner?.mode !== 'harness') {
114+
this.buildCommand = config?.builder?.cmd;
115+
if (!this.buildCommand && detectRuntime(process.cwd()) === 'node') {
116+
this.buildCommand = 'npm run build';
117+
}
118+
this.build();
116119
}
117-
this.build();
118120
this.watchForChanges();
119121
}
120122
return this.startApp();

0 commit comments

Comments
 (0)