// cell vendor - Copy all dependencies into modules/ for hermetic builds

var io = use('io')
var shop = use('shop')

if (!io.exists('.cell/shop.toml')) {
  log.error("No shop.toml found. Run 'cell init' first.")
  $_.stop()
  return
}

var config = shop.load_config()
if (!config || !config.dependencies) {
  log.console("No dependencies to vendor")
  $_.stop()
  return
}

log.console("Vendoring dependencies...")

for (var alias in config.dependencies) {
  var locator = config.dependencies[alias]
  var parsed = shop.parse_locator(locator)
  
  if (!parsed) {
    log.error("Invalid locator: " + locator)
    continue
  }
  
  var module_dir = '.cell/modules/' + alias + '@' + parsed.version
  
  if (config.replace && config.replace[locator]) {
    // Already using local path
    log.console(alias + " - using local path: " + config.replace[locator])
  } else if (!io.exists(module_dir)) {
    log.console(alias + " - not found at " + module_dir)
    log.console("  Run 'cell get " + locator + "' to fetch it")
  } else {
    log.console(alias + " - already vendored at " + module_dir)
  }
}

log.console("")
log.console("All dependencies are vendored in .cell/modules/")
log.console("This ensures hermetic, reproducible builds.")

$_.stop()