-
-
Notifications
You must be signed in to change notification settings - Fork 5
feat(cli): Add init command to generate baseline eval config #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sentry-junior
wants to merge
2
commits into
main
Choose a base branch
from
feat/cli-init
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| #!/usr/bin/env node | ||
| import { runInitCommand } from "./cli/init"; | ||
|
|
||
| main().catch((error) => { | ||
| console.error(error instanceof Error ? error.message : String(error)); | ||
| process.exitCode = 1; | ||
| }); | ||
|
|
||
| async function main() { | ||
| const args = process.argv.slice(2); | ||
|
|
||
| if (args[0] === "--help" || args[0] === "-h" || args.length === 0) { | ||
| console.log(usage()); | ||
| return; | ||
| } | ||
|
|
||
| const command = args[0]; | ||
|
|
||
| if (command === "init") { | ||
| await dispatchInit(args.slice(1)); | ||
| return; | ||
| } | ||
|
|
||
| console.error(`Unknown command: ${command}`); | ||
| console.error(usage()); | ||
| process.exitCode = 1; | ||
| } | ||
|
|
||
| async function dispatchInit(args: string[]) { | ||
| const options = parseInitArgs(args); | ||
|
|
||
| if (options.help) { | ||
| console.log(initUsage()); | ||
| return; | ||
| } | ||
|
|
||
| await runInitCommand({ cwd: options.cwd, force: options.force }); | ||
| } | ||
|
|
||
| function parseInitArgs(args: string[]) { | ||
| let force = false; | ||
| let cwd: string | undefined; | ||
| let help = false; | ||
|
|
||
| for (let i = 0; i < args.length; i++) { | ||
| const arg = args[i]; | ||
| switch (arg) { | ||
| case "--force": | ||
| force = true; | ||
| break; | ||
| case "--cwd": { | ||
| const value = args[++i]; | ||
| if (!value) throw new Error("Missing value for --cwd"); | ||
| cwd = value; | ||
| break; | ||
| } | ||
| case "--help": | ||
| case "-h": | ||
| help = true; | ||
| break; | ||
| default: | ||
| throw new Error(`Unknown argument: ${arg}`); | ||
| } | ||
| } | ||
|
|
||
| return { force, cwd, help }; | ||
| } | ||
|
|
||
| function usage() { | ||
| return [ | ||
| "Usage: vitest-evals <command>", | ||
| "", | ||
| "Commands:", | ||
| " init Generate a baseline eval config and add scripts to package.json", | ||
| "", | ||
| "Options:", | ||
| " -h, --help Print help", | ||
| ].join("\n"); | ||
| } | ||
|
|
||
| function initUsage() { | ||
| return [ | ||
| "Usage: vitest-evals init [options]", | ||
| "", | ||
| "Generate vitest.evals.config.ts and add eval scripts to package.json.", | ||
| "", | ||
| "Options:", | ||
| " --force Overwrite existing config and conflicting scripts", | ||
| " --cwd <dir> Target project directory (default: current directory)", | ||
| " -h, --help Print help", | ||
| ].join("\n"); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
| import { tmpdir } from "node:os"; | ||
| import { describe, it, expect, beforeEach, afterEach } from "vitest"; | ||
| import { EVALS_CONFIG_CONTENT, EVALS_SCRIPTS, runInit } from "./init"; | ||
|
|
||
| function makeTmpDir() { | ||
| const dir = join( | ||
| tmpdir(), | ||
| `vitest-evals-init-test-${Date.now()}-${Math.random().toString(36).slice(2)}`, | ||
| ); | ||
| mkdirSync(dir, { recursive: true }); | ||
| return dir; | ||
| } | ||
|
|
||
| function writePkg(dir: string, pkg: Record<string, unknown>) { | ||
| writeFileSync(join(dir, "package.json"), `${JSON.stringify(pkg, null, 2)}\n`); | ||
| } | ||
|
|
||
| function readPkg(dir: string): Record<string, unknown> { | ||
| return JSON.parse(readFileSync(join(dir, "package.json"), "utf8")) as Record< | ||
| string, | ||
| unknown | ||
| >; | ||
| } | ||
|
|
||
| function readConfig(dir: string): string { | ||
| return readFileSync(join(dir, "vitest.evals.config.ts"), "utf8"); | ||
| } | ||
|
|
||
| describe("runInit", () => { | ||
| let dir: string; | ||
|
|
||
| beforeEach(() => { | ||
| dir = makeTmpDir(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| rmSync(dir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it("returns no-package-json when package.json is missing", () => { | ||
| const result = runInit({ cwd: dir }); | ||
| expect(result).toEqual({ status: "no-package-json" }); | ||
| }); | ||
|
|
||
| it("creates config and adds scripts on a fresh project", () => { | ||
| writePkg(dir, { name: "my-app" }); | ||
|
|
||
| const result = runInit({ cwd: dir }); | ||
|
|
||
| expect(result).toEqual({ | ||
| status: "ok", | ||
| wrote: [ | ||
| "vitest.evals.config.ts", | ||
| "package.json scripts.evals", | ||
| "package.json scripts.evals:record", | ||
| ], | ||
| skipped: [], | ||
| }); | ||
|
|
||
| expect(readConfig(dir)).toBe(EVALS_CONFIG_CONTENT); | ||
|
|
||
| const pkg = readPkg(dir); | ||
| const scripts = pkg.scripts as Record<string, string>; | ||
| expect(scripts.evals).toBe(EVALS_SCRIPTS.evals); | ||
| expect(scripts["evals:record"]).toBe(EVALS_SCRIPTS["evals:record"]); | ||
| }); | ||
|
|
||
| it("preserves existing scripts when adding new ones", () => { | ||
| writePkg(dir, { name: "my-app", scripts: { test: "vitest" } }); | ||
|
|
||
| runInit({ cwd: dir }); | ||
|
|
||
| const pkg = readPkg(dir); | ||
| const scripts = pkg.scripts as Record<string, string>; | ||
| expect(scripts.test).toBe("vitest"); | ||
| expect(scripts.evals).toBe(EVALS_SCRIPTS.evals); | ||
| }); | ||
|
|
||
| it("is idempotent on a second run", () => { | ||
| writePkg(dir, { name: "my-app" }); | ||
|
|
||
| runInit({ cwd: dir }); | ||
| const result = runInit({ cwd: dir }); | ||
|
|
||
| expect(result).toEqual({ | ||
| status: "ok", | ||
| wrote: [], | ||
| skipped: [ | ||
| "vitest.evals.config.ts", | ||
| "package.json scripts.evals", | ||
| "package.json scripts.evals:record", | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| it("returns conflict when config exists with different content", () => { | ||
| writePkg(dir, { name: "my-app" }); | ||
| writeFileSync(join(dir, "vitest.evals.config.ts"), "// custom config\n"); | ||
|
|
||
| const result = runInit({ cwd: dir }); | ||
|
|
||
| expect(result).toEqual({ | ||
| status: "conflict", | ||
| conflicts: ["vitest.evals.config.ts"], | ||
| }); | ||
| }); | ||
|
|
||
| it("returns conflict when scripts have different values", () => { | ||
| writePkg(dir, { | ||
| name: "my-app", | ||
| scripts: { evals: "vitest run --config other.config.ts" }, | ||
| }); | ||
|
|
||
| const result = runInit({ cwd: dir }); | ||
|
|
||
| expect(result).toEqual({ | ||
| status: "conflict", | ||
| conflicts: ["package.json scripts.evals"], | ||
| }); | ||
| }); | ||
|
|
||
| it("returns all conflicts when multiple things differ", () => { | ||
| writePkg(dir, { | ||
| name: "my-app", | ||
| scripts: { | ||
| evals: "custom", | ||
| "evals:record": "custom-record", | ||
| }, | ||
| }); | ||
| writeFileSync(join(dir, "vitest.evals.config.ts"), "// different\n"); | ||
|
|
||
| const result = runInit({ cwd: dir }); | ||
| expect(result.status).toBe("conflict"); | ||
| if (result.status === "conflict") { | ||
| expect(result.conflicts).toHaveLength(3); | ||
| } | ||
| }); | ||
|
|
||
| it("overwrites with --force even when conflicts exist", () => { | ||
| writePkg(dir, { | ||
| name: "my-app", | ||
| scripts: { evals: "custom", "evals:record": "custom-record" }, | ||
| }); | ||
| writeFileSync(join(dir, "vitest.evals.config.ts"), "// different\n"); | ||
|
|
||
| const result = runInit({ cwd: dir, force: true }); | ||
|
|
||
| expect(result.status).toBe("ok"); | ||
| expect(readConfig(dir)).toBe(EVALS_CONFIG_CONTENT); | ||
|
|
||
| const pkg = readPkg(dir); | ||
| const scripts = pkg.scripts as Record<string, string>; | ||
| expect(scripts.evals).toBe(EVALS_SCRIPTS.evals); | ||
| expect(scripts["evals:record"]).toBe(EVALS_SCRIPTS["evals:record"]); | ||
| }); | ||
|
|
||
| it("--force preserves unrelated package.json fields and scripts", () => { | ||
| writePkg(dir, { | ||
| name: "my-app", | ||
| version: "1.2.3", | ||
| scripts: { test: "vitest", evals: "old-evals" }, | ||
| dependencies: { lodash: "^4.17.21" }, | ||
| }); | ||
|
|
||
| runInit({ cwd: dir, force: true }); | ||
|
|
||
| const pkg = readPkg(dir); | ||
| expect(pkg.name).toBe("my-app"); | ||
| expect(pkg.version).toBe("1.2.3"); | ||
| expect((pkg.dependencies as Record<string, string>).lodash).toBe( | ||
| "^4.17.21", | ||
| ); | ||
| expect((pkg.scripts as Record<string, string>).test).toBe("vitest"); | ||
| }); | ||
|
|
||
| it("throws on invalid package.json JSON", () => { | ||
| writeFileSync(join(dir, "package.json"), "not valid json"); | ||
| expect(() => runInit({ cwd: dir })).toThrow(/invalid JSON/); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The argument parser for
--cwdconsumes the next token as its value, even if it's another flag like--force, causing the flag to be ignored.Severity: MEDIUM
Suggested Fix
Before assigning the next argument to
cwd, add a check to ensure the value does not start with-or--. If it does, it's another flag, and you should throw an error for a missing--cwdvalue or handle it as an invalid input.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.