Skip to content

Commit

Permalink
Write tools config in genkit init. (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
apascal07 authored May 10, 2024
1 parent 0ed8e6b commit 9cb10ef
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
runner: {
mode: 'harness',
files: [$GENKIT_HARNESS_FILES],
},
};
26 changes: 26 additions & 0 deletions genkit-tools/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ const sampleTemplatePaths: Record<Platform, string> = {
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'];

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 6 additions & 4 deletions genkit-tools/common/src/runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 9cb10ef

Please sign in to comment.