// link [args] // Commands: // list // delete // clear // [package] // // Examples: // cell link ../cell-steam (Links package defined in ../cell-steam to that path) // cell link my-pkg ../cell-steam (Links my-pkg to ../cell-steam) // cell link my-pkg other-pkg (Links my-pkg to other-pkg) var shop = use('shop') var fd = use('fd') var toml = use('toml') if (args.length < 1) { log.console("Usage: link [args] or link [package] ") log.console("Commands:") log.console(" list List all active links") log.console(" delete Remove a link") log.console(" clear Remove all links") log.console(" Link the package in to ") log.console(" Link to (path or package)") $_.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 == 'delete' || cmd == 'rm') { if (args.length < 2) { log.console("Usage: link delete ") $_.stop() return } var target = args[1] // Try to remove directly if (shop.remove_link(target)) { // Update to restore original if possible log.console("Restoring " + target + "...") shop.update(target) } else { log.console("No link found for " + target) } } else if (cmd == 'clear') { shop.clear_links() log.console("Links cleared. Run 'cell update' to restore packages if needed.") } else { // Linking logic var pkg_name = null var target_path = null // Check for 'add' compatibility var start_idx = 0 if (cmd == 'add') { start_idx = 1 } // Parse arguments // usage: link [pkg] // valid inputs: // link ../foo // link pkg ../foo // link pkg remote-pkg var arg1 = args[start_idx] var arg2 = (args.length > start_idx + 1) ? args[start_idx + 1] : null if (!arg1) { log.console("Error: specific target or package required") $_.stop() return } if (arg2) { // Two arguments: explicit package name and target pkg_name = arg1 target_path = arg2 } else { // One argument: assume it's a path, and we need to infer the package name target_path = arg1 // Resolve path to check for cell.toml var resolved = target_path if (fd.is_dir(resolved)) resolved = fd.realpath(resolved) var toml_path = resolved + '/cell.toml' if (!fd.is_file(toml_path)) { log.console("Error: No cell.toml found at " + resolved + ". Cannot infer package name.") log.console("If you meant to link an alias, provide the target: link ") $_.stop() return } // Read package name try { var content = toml.decode(text(fd.slurp(toml_path))) if (content.package) { pkg_name = content.package // If package name has version, strip it? // Usually package = "name", version = "..." in cell.toml? // Or package = "name" // Standard is just name. } else { log.console("Error: cell.toml at " + resolved + " does not define a 'package' name.") $_.stop() return } } catch (e) { log.console("Error reading cell.toml: " + e) $_.stop() return } // Ensure target path is fully resolved since we inferred it if (fd.is_dir(target_path)) target_path = fd.realpath(target_path) } // Process the link // 1. Resolve target if it is a directory if (target_path != "." && fd.is_dir(target_path)) { target_path = fd.realpath(target_path) } else if (target_path == ".") { target_path = fd.realpath(target_path) } else if (target_path.startsWith('/') || target_path.startsWith('./') || target_path.startsWith('../')) { // It looks like a path but doesn't exist? log.console("Warning: Link target '" + target_path + "' does not exist locally. Linking as alias anyway.") if (target_path.startsWith('./') || target_path.startsWith('../')) { // Resolve relative path roughly? var cwd = fd.realpath('.') // simple concat (fd.realpath typically converts to abspath, but if file missing it might fail or return same) // Assuming user wants strict path if they used relative. // Using fd.realpath on CWD + path is safer if we want absolute. // But we don't have path manipulation lib easily exposed except implicit logic. // Leaving as provided string if not existing dir. } } // 2. Add link shop.add_link(pkg_name, target_path) // 3. Update shop // "Doing this effectively adds another item to the shop" // We trigger update to ensure the shop recognizes the new link shop.update(pkg_name) } $_.stop()