UNPKG

@every-env/cli

Version:

Multi-agent orchestrator for AI-powered development workflows

101 lines 3.3 kB
import { exec } from 'child_process'; import { promisify } from 'util'; import { readFile } from 'fs/promises'; import * as path from 'path'; const execAsync = promisify(exec); export class VariableResolver { async resolve(config, pattern, agent, context) { const baseVars = await this.getBaseVariables(); const gitVars = await this.getGitVariables(); const outputVars = this.getOutputVariables(context.outputPath); const matchVars = this.getMatchVariables(context); // Merge all variables with proper precedence return { projectName: '', timestamp: '', date: '', output: '', outputDir: '', outputName: '', ...baseVars, ...gitVars, ...outputVars, ...matchVars, ...(config.variables || config.docs.variables || {}), ...(pattern.variables || {}), ...(agent.variables || {}), }; } async getBaseVariables() { const now = new Date(); // Try to get project name from package.json let projectName = 'Project'; try { const pkg = JSON.parse(await readFile('package.json', 'utf-8')); projectName = pkg.name || projectName; } catch { // Fallback to directory name projectName = process.cwd().split('/').pop() || projectName; } return { projectName, timestamp: now.toISOString(), date: now.toLocaleDateString(), }; } async getGitVariables() { const vars = {}; try { const { stdout: branch } = await execAsync('git rev-parse --abbrev-ref HEAD'); vars.gitBranch = branch.trim(); } catch { // Not in git repo or git not available } try { const { stdout: commit } = await execAsync('git rev-parse HEAD'); vars.gitCommit = commit.trim(); } catch { // No commits yet } try { const { stdout: author } = await execAsync('git config user.name'); vars.gitAuthor = author.trim(); } catch { // Git user not configured } return vars; } getOutputVariables(outputPath) { const { dir, name } = path.parse(outputPath); return { output: outputPath, outputDir: dir, outputName: name, }; } getMatchVariables(context) { const vars = {}; if (context.matchItem !== undefined) { // Handle different match types if (typeof context.matchItem === 'string') { vars.name = context.matchItem; } else if (typeof context.matchItem === 'object') { vars.match = context.matchItem; vars.name = context.matchItem.name || context.matchItem.match; } } if (context.matchIndex !== undefined) { vars.index = context.matchIndex; } if (context.matchTotal !== undefined) { vars.total = context.matchTotal; } return vars; } } //# sourceMappingURL=variable-resolver.js.map