Skip to content
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

feat(cli): Add shortest init command #297

Merged
merged 22 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
64 changes: 64 additions & 0 deletions packages/shortest/src/cli/bin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
#!/usr/bin/env node

import { execSync } from "child_process";
import { appendFileSync, existsSync, writeFileSync } from "fs";
import { join } from "path";
import pc from "picocolors";
import { getConfig } from "..";
import { GitHubTool } from "../browser/integrations/github";
import { TestRunner } from "../core/runner";
import {
detectPackageManager,
detectProjectType,
getConfigTemplate,
getEnvTemplate,
} from "../utils/initialize";

process.removeAllListeners("warning");
process.on("warning", (warning) => {
Expand Down Expand Up @@ -108,9 +117,64 @@ function isValidArg(arg: string): boolean {
return false;
}

async function initCommand() {
console.log(pc.blue("Setting up Shortest..."));

const packageManager = detectPackageManager();
const projectType = detectProjectType();
try {
if (
!existsSync(join(process.cwd(), "node_modules", "@antiwork/shortest"))
) {
console.log("Installing @antiwork/shortest...");
const installCmd = {
npm: "npm install --save-dev @antiwork/shortest",
pnpm: "pnpm add -D @antiwork/shortest",
yarn: "yarn add -D @antiwork/shortest",
}[packageManager];

execSync(installCmd, { stdio: "inherit" });
console.log(pc.green("✔ Dependencies installed"));
}

const configPath = join(process.cwd(), "shortest.config.ts");
if (!existsSync(configPath)) {
writeFileSync(configPath, getConfigTemplate(projectType));
console.log(pc.green("✔ Configuration file created"));
}

const gitignorePath = join(process.cwd(), ".gitignore");
if (!existsSync(gitignorePath)) {
writeFileSync(gitignorePath, ".shortest/\n");
} else if (!existsSync(".shortest/")) {
appendFileSync(gitignorePath, "\n.shortest/\n");
}
console.log(pc.green("✔ .gitignore updated"));

const envPath = join(process.cwd(), ".env.local");
if (!existsSync(envPath)) {
writeFileSync(envPath, getEnvTemplate());
console.log(pc.green("✔ Environment file generated"));
}

console.log(pc.green("\nInitialization complete! Next steps:"));
console.log("1. Add your ANTHROPIC_API_KEY to .env.local");
console.log("2. Create your first test file: my-test.test.ts");
console.log("3. Run tests with: shortest my-test.test.ts");
} catch (error) {
console.error(pc.red("Initialization failed:"), error);
process.exit(1);
}
}

async function main() {
const args = process.argv.slice(2);

if (args[0] === "init") {
initCommand().catch(console.error);
process.exit(0);
}

if (args.includes("--help") || args.includes("-h")) {
showHelp();
process.exit(0);
Expand Down
29 changes: 29 additions & 0 deletions packages/shortest/src/utils/initialize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { existsSync } from "fs";

export function detectPackageManager(): "npm" | "pnpm" | "yarn" {
if (existsSync("pnpm-lock.yaml")) return "pnpm";
if (existsSync("yarn.lock")) return "yarn";
return "npm";
}

export function getConfigTemplate(): string {
return `import type { ShortestConfig } from "@antiwork/shortest";

export default {
headless: false,
baseUrl: "http://localhost:3000",
testPattern: "**/*.test.ts",
anthropicKey: process.env.ANTHROPIC_API_KEY,
} satisfies ShortestConfig;
`;
}

export function getEnvTemplate(): string {
return `# Shortest Environment Variables
ANTHROPIC_API_KEY=

# Optional Configuration
# MAILOSAUR_API_KEY=
# MAILOSAUR_SERVER_ID=
`;
}
Loading