import fs from 'node:fs/promises'; import os from 'node:os'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { InvalidArgumentError } from 'commander'; import { execa } from 'execa'; import fsExtra from 'fs-extra'; import type { PackageManager } from '../utils/package-manager'; import { selectMatchingDistTag } from './create/command'; import { EDITOR, isValidEditor } from './init/mcp-docs-server-install'; import { areValidComponents, COMPONENTS, isValidLLMProvider, LLMProvider } from './init/utils'; export function getPackageManager(): PackageManager { const userAgent = process.env.npm_config_user_agent || ''; const execPath = process.env.npm_execpath || ''; // Check user agent first if (userAgent.includes('bun')) { return 'bun'; } if (userAgent.includes('yarn')) { return 'yarn'; } if (userAgent.includes('pnpm')) { return 'pnpm'; } if (userAgent.includes('npm')) { return 'npm'; } // Fallback to execpath check if (execPath.includes('bun')) { return 'bun'; } if (execPath.includes('yarn')) { return 'yarn'; } if (execPath.includes('pnpm')) { return 'pnpm'; } if (execPath.includes('npm')) { return 'npm'; } return 'npm'; // Default fallback } /** * Wrap an async commander action so failures print a clean error message * (never a stack trace) and exit non-zero. */ export function wrapAction(fn: (...args: any[]) => Promise): (...args: any[]) => void { return (...args: any[]) => { fn(...args).catch((err: Error) => { console.error(`Error: ${err.message}`); process.exit(1); }); }; } export function parseMcp(value: string) { if (!isValidEditor(value)) { throw new InvalidArgumentError(`Choose a valid value: ${EDITOR.join(', ')}`); } return value; } export function parseSkills(value: string) { // Skills flag accepts comma-separated agent names return value .split(',') .map(s => s.trim()) .filter(Boolean); } export function parseComponents(value: string) { const parsedValue = value.split(','); if (!areValidComponents(parsedValue)) { throw new InvalidArgumentError(`Choose valid components: ${COMPONENTS.join(', ')}`); } return parsedValue; } export function parseLlmProvider(value: string) { if (!isValidLLMProvider(value)) { throw new InvalidArgumentError(`Choose a valid provider: ${LLMProvider.join(', ')}`); } return value; } export function shouldSkipDotenvLoading(): boolean { return process.env.MASTRA_SKIP_DOTENV === 'true' || process.env.MASTRA_SKIP_DOTENV === '1'; } /** * Node flags that make stack traces resolve through the bundle's source maps. * Skipped when NODE_OPTIONS already sets either form of the flag, since a CLI * flag would override a `--no-enable-source-maps` there. Callers place these * before user-supplied args so `--no-enable-source-maps` in those still wins. */ export function getSourceMapNodeArgs(sourcemapEnabled: boolean, nodeOptions: string | undefined): string[] { // Node accepts underscores in option names, e.g. `--no-enable_source_maps`. const setInNodeOptions = nodeOptions?.replaceAll('_', '-').includes('enable-source-maps'); return sourcemapEnabled && !setInNodeOptions ? ['--enable-source-maps'] : []; } /** * Get the version tag (e.g., 'beta', 'latest') for the currently running mastra CLI. * Create passes its known version to avoid resolving package metadata from an installed layout. * Init omits it and preserves the existing best-effort undefined fallback. */ export async function getVersionTag(version?: string): Promise { try { let currentVersion = version; if (!currentVersion) { const pkgPath = fileURLToPath(import.meta.resolve('mastra/package.json')); const json = await fsExtra.readJSON(pkgPath); currentVersion = json.version; } if (!currentVersion) throw new Error('Missing mastra package version'); const { stdout } = await execa('npm', ['dist-tag', 'ls', 'mastra'], { cwd: import.meta.dirname, }); const tag = selectMatchingDistTag(currentVersion, stdout); if (tag) return tag; } catch { // The caller-specific fallback is handled below. } if (version) { console.error('We could not resolve the mastra version tag, falling back to "latest"'); return 'latest'; } return undefined; } /** * Check if the current directory already has git initialized. */ export async function isGitInitialized({ cwd }: { cwd: string }): Promise { try { await execa('git', ['rev-parse', '--is-inside-work-tree'], { cwd, stdio: 'ignore' }); return true; } catch { return false; } } /** * Initialize a git repository in the specified directory. */ export async function gitInit({ cwd }: { cwd: string }) { await execa('git', ['init'], { cwd, stdio: 'ignore' }); await execa('git', ['add', '-A'], { cwd, stdio: 'ignore' }); await execa( 'git', [ 'commit', '-m', 'Initial commit from Mastra', '--author="dane-ai-mastra[bot] "', ], { cwd, stdio: 'ignore' }, ); }