Files
cell/scripts/link.ce
2025-12-06 10:35:06 -06:00

128 lines
3.3 KiB
Plaintext

// link <command> [args]
// Commands:
// add <alias> <path>
// list
// delete <alias>
// clear
var shop = use('shop')
var fd = use('fd')
if (args.length < 1) {
log.console("Usage: link <command> [args]")
log.console("Commands:")
log.console(" add <alias> <path> Link an alias to a local path or package")
log.console(" list List all active links")
log.console(" delete <alias> Remove a link")
log.console(" clear Remove all links")
$_.stop()
return
}
var cmd = args[0]
if (cmd == 'list') {
var links = shop.load_links()
var count = 0
for (var k in links) {
log.console(k + " -> " + links[k])
count++
}
if (count == 0) log.console("No links.")
} else if (cmd == 'add') {
if (args.length < 3) {
log.console("Usage: link add <alias|package> <path>")
$_.stop()
return
}
var target_pkg = args[1]
var target_path = args[2]
// Resolve target path to absolute
if (fd.is_dir(target_path)) {
target_path = fd.realpath(target_path)
} else {
log.console("Warning: Path '" + target_path + "' does not exist or is not a directory.")
// Try to resolve anyway if relative
if (!target_path.startsWith('/')) {
target_path = fd.realpath('.') + '/' + target_path
}
}
var canonical_path = null
// Check if it's an alias in cell.toml
var config = shop.load_config()
if (config.dependencies && config.dependencies[target_pkg]) {
var pkg = config.dependencies[target_pkg]
canonical_path = pkg
} else {
// Assume it's a canonical package name
canonical_path = target_pkg
}
// Strip version
if (canonical_path.includes('@')) {
canonical_path = canonical_path.split('@')[0]
}
if (canonical_path.startsWith('/')) {
canonical_path = canonical_path.substring(1)
}
shop.add_link(canonical_path, target_path)
// Update to apply the link
// If it was an alias, update the alias
if (config.dependencies && config.dependencies[target_pkg]) {
shop.update(target_pkg)
} else {
// If it was a raw package, update it directly
shop.update(canonical_path)
}
} else if (cmd == 'delete') {
if (args.length < 2) {
log.console("Usage: link delete <alias|package>")
$_.stop()
return
}
var target = args[1]
var canonical_path = null
var config = shop.load_config()
if (config.dependencies && config.dependencies[target]) {
var pkg = config.dependencies[target]
canonical_path = pkg
} else {
canonical_path = target
}
if (canonical_path.includes('@')) {
canonical_path = canonical_path.split('@')[0]
}
if (canonical_path.startsWith('/')) {
canonical_path = canonical_path.substring(1)
}
if (shop.remove_link(canonical_path)) {
// Update to restore original (download/unpack)
// If we can map back to an alias, use it, otherwise update canonical
if (config.dependencies && config.dependencies[target]) {
shop.update(target)
} else {
shop.update(canonical_path)
}
} else {
log.console("No link found for " + target)
}
} else if (cmd == 'clear') {
shop.clear_links()
log.console("Links cleared. Run 'cell update' or 'link delete <alias>' to restore packages if needed.")
} else {
log.console("Unknown command: " + cmd)
}
$_.stop()