diff --git a/genkit-tools/cli/config/nextjs.genkit-tools.config.js.template b/genkit-tools/cli/config/nextjs.genkit-tools.config.js.template new file mode 100644 index 0000000000..0b44568054 --- /dev/null +++ b/genkit-tools/cli/config/nextjs.genkit-tools.config.js.template @@ -0,0 +1,6 @@ +module.exports = { + runner: { + mode: 'harness', + files: [$GENKIT_HARNESS_FILES], + }, +}; diff --git a/genkit-tools/cli/src/commands/init.ts b/genkit-tools/cli/src/commands/init.ts index eff440b9b2..6b56568088 100644 --- a/genkit-tools/cli/src/commands/init.ts +++ b/genkit-tools/cli/src/commands/init.ts @@ -150,6 +150,9 @@ const sampleTemplatePaths: Record = { nextjs: '../../config/nextjs.genkit.ts.template', }; +const nextjsToolsConfigTemplatePath = + '../../config/nextjs.genkit-tools.config.js.template'; + /** Supported runtimes for the init command. */ const supportedRuntimes: Runtime[] = ['node']; @@ -250,6 +253,7 @@ export const init = new Command('init') ) { generateSampleFile(platform, modelOptions[model].plugin, plugins); } + generateToolsConfig(platform); } catch (error) { logger.error(error); process.exit(1); @@ -325,6 +329,28 @@ async function updatePackageJson() { fs.writeFileSync(packageJsonPath, JSON.stringify(newPackageJson, null, 2)); } +/** + * Generates an appropriate tools config for the given platform. + * @param platform platform + */ +function generateToolsConfig(platform: Platform) { + if (platform === 'nextjs') { + const templatePath = path.join(__dirname, nextjsToolsConfigTemplatePath); + let template = fs.readFileSync(templatePath, 'utf8'); + if (fs.existsSync('src/app')) { + template = template.replace('$GENKIT_HARNESS_FILES', `'./src/app/*.ts'`); + } else if (fs.existsSync('app')) { + template = template.replace('$GENKIT_HARNESS_FILES', `'./app/*.js'`); + } else { + throw new Error( + 'Unable to resolve source folder (app or src/app) of you next.js app.' + ); + } + const configPath = path.join(process.cwd(), 'genkit-tools.conf.js'); + fs.writeFileSync(configPath, template, 'utf8'); + } +} + /** * Generates a sample index.ts file. * @param platform Deployment platform. diff --git a/genkit-tools/common/src/runner/runner.ts b/genkit-tools/common/src/runner/runner.ts index 57d338cafb..c1ee24be40 100644 --- a/genkit-tools/common/src/runner/runner.ts +++ b/genkit-tools/common/src/runner/runner.ts @@ -110,11 +110,13 @@ export class Runner { } if (this.autoReload) { const config = await findToolsConfig(); - this.buildCommand = config?.builder?.cmd; - if (!this.buildCommand && detectRuntime(process.cwd()) === 'node') { - this.buildCommand = 'npm run build'; + if (config?.runner?.mode !== 'harness') { + this.buildCommand = config?.builder?.cmd; + if (!this.buildCommand && detectRuntime(process.cwd()) === 'node') { + this.buildCommand = 'npm run build'; + } + this.build(); } - this.build(); this.watchForChanges(); } return this.startApp();