import chalk from 'chalk' import type { MetabaseClient } from '../client.js' import { Resolver } from '../resolver.js' import { loadResources } from '../loader.js' import { loadState, saveState } from '../state.js' import { buildPlan, applyActions } from '../reconciler.js' import { printPlan } from './plan.js' export async function apply(client: MetabaseClient, dryRun: boolean, resourceDir: string = '.') { console.log('Loading resources...') const resources = await loadResources(resourceDir) if (resources.length === 0) { console.log(chalk.yellow('No resources found. Expected collections/, questions/, dashboards/ subdirectories.')) return } console.log(`Found ${resources.length} resource(s)`) console.log('Initializing resolver...') const resolver = new Resolver() await resolver.init(client) const state = await loadState() const actions = buildPlan(resources, state) printPlan(actions) if (dryRun) { console.log(chalk.cyan('Dry run — no changes applied.')) return } if (actions.length === 0) return console.log(chalk.bold('Applying changes...')) const newState = await applyActions(client, resolver, actions, state) await saveState(newState) console.log(chalk.green(`\nDone! Applied ${actions.length} change(s).`)) }