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
29 changes: 19 additions & 10 deletions genkit-tools/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ interface InitOptions {
model: ModelProvider;
// Path to local Genkit dist archive.
distArchive: string;
// Non-interactive mode.
nonInteractive: boolean;
}

interface ImportOptions {
Expand Down Expand Up @@ -166,6 +168,10 @@ export const init = new Command('init')
'-m, --model <model>',
'Model provider (googleai, vertexai, ollama, or none)'
)
.option(
'--non-interactive',
'Run init in non-interactive mode (experimental)'
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about doing --interactive with a default setting of "true"?

Might help with some of the double-negatives later on.

!nonInteractive

and

!process.argv.includes('--non-interactive')

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General recommendation is to avoid boolean flags with default value of true...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok 😄 I could see why (--non-interactive vs --interactive=false) it's just a readability thing in the code itself to consider from my perspective.

alternatively, could try and come up w/ something "positive" like --skip-prompts, but let's not spin our wheels on it.

.option(
'-d, --dist-archive <distArchive>',
'Path to local Genkit dist archive'
Expand Down Expand Up @@ -243,13 +249,14 @@ export const init = new Command('init')
if (!fs.existsSync('src')) {
fs.mkdirSync('src');
}
await updateTsConfig();
await updatePackageJson();
await updateTsConfig(options.nonInteractive);
await updatePackageJson(options.nonInteractive);
if (
await confirm({
options.nonInteractive ||
(await confirm({
message: 'Would you like to generate a sample flow?',
default: true,
})
}))
) {
generateSampleFile(platform, modelOptions[model].plugin, plugins);
}
Expand All @@ -275,7 +282,7 @@ export const init = new Command('init')
/**
* Updates package.json with Genkit-expected fields.
*/
async function updatePackageJson() {
async function updatePackageJson(nonInteractive: boolean) {
const packageJsonPath = path.join(process.cwd(), 'package.json');
// package.json should exist before reaching this point.
if (!fs.existsSync(packageJsonPath)) {
Expand All @@ -284,9 +291,11 @@ async function updatePackageJson() {
const existingPackageJson = JSON.parse(
fs.readFileSync(packageJsonPath, 'utf8')
);
const choice = await promptWriteMode(
'Would you like to update your package.json with suggested settings?'
);
const choice = nonInteractive
? 'overwrite'
: await promptWriteMode(
'Would you like to update your package.json with suggested settings?'
);
const packageJson = {
main: 'lib/index.js',
scripts: {
Expand Down Expand Up @@ -458,15 +467,15 @@ async function promptWriteMode(
/**
* Updates tsconfig.json with required flags for Genkit.
*/
async function updateTsConfig() {
async function updateTsConfig(nonInteractive: boolean) {
try {
const tsConfigPath = path.join(process.cwd(), 'tsconfig.json');
let existingTsConfig = undefined;
if (fs.existsSync(tsConfigPath)) {
existingTsConfig = JSON.parse(fs.readFileSync(tsConfigPath, 'utf-8'));
}
let choice: WriteMode = 'overwrite';
if (existingTsConfig) {
if (!nonInteractive && existingTsConfig) {
choice = await promptWriteMode(
'Would you like to update your tsconfig.json with suggested settings?'
);
Expand Down
5 changes: 4 additions & 1 deletion genkit-tools/common/src/utils/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,10 @@ function isValidateOnly(): boolean {
// are set. Once we have opt-out and we're ready for public preview this will
// get updated.
function isAnalyticsEnabled(): boolean {
return !getUserSettings()[ANALYTICS_OPT_OUT_CONFIG_TAG];
return (
!process.argv.includes('--non-interactive') &&
!getUserSettings()[ANALYTICS_OPT_OUT_CONFIG_TAG]
);
}

async function recordInternal(
Expand Down