clean up cmd line
This commit is contained in:
109
remove.ce
109
remove.ce
@@ -1,24 +1,105 @@
|
||||
// cell remove <alias|path> - Remove a package from dependencies or shop
|
||||
// cell remove <locator> - Remove a package from the shop
|
||||
//
|
||||
// Usage:
|
||||
// cell remove <locator> Remove a package from the shop
|
||||
// cell remove . Remove current directory package from shop
|
||||
//
|
||||
// Options:
|
||||
// --prune Also remove packages no longer needed by any root
|
||||
// --dry-run Show what would be removed
|
||||
|
||||
var shop = use('internal/shop')
|
||||
var pkg = use('package')
|
||||
var link = use('link')
|
||||
var fd = use('fd')
|
||||
|
||||
if (args.length < 1) {
|
||||
log.console("Usage: cell remove <alias|path>")
|
||||
$stop()
|
||||
return
|
||||
}
|
||||
var target_pkg = null
|
||||
var prune = false
|
||||
var dry_run = false
|
||||
|
||||
var pkg = args[0]
|
||||
|
||||
// Resolve relative paths to absolute paths
|
||||
if (pkg == '.' || pkg.startsWith('./') || pkg.startsWith('../') || fd.is_dir(pkg)) {
|
||||
var resolved = fd.realpath(pkg)
|
||||
if (resolved) {
|
||||
pkg = resolved
|
||||
for (var i = 0; i < args.length; i++) {
|
||||
if (args[i] == '--prune') {
|
||||
prune = true
|
||||
} else if (args[i] == '--dry-run') {
|
||||
dry_run = true
|
||||
} else if (args[i] == '--help' || args[i] == '-h') {
|
||||
log.console("Usage: cell remove <locator> [options]")
|
||||
log.console("")
|
||||
log.console("Remove a package from the shop.")
|
||||
log.console("")
|
||||
log.console("Options:")
|
||||
log.console(" --prune Also remove packages no longer needed by any root")
|
||||
log.console(" --dry-run Show what would be removed")
|
||||
$stop()
|
||||
} else if (!args[i].startsWith('-')) {
|
||||
target_pkg = args[i]
|
||||
}
|
||||
}
|
||||
|
||||
shop.remove(pkg)
|
||||
if (!target_pkg) {
|
||||
log.console("Usage: cell remove <locator> [options]")
|
||||
$stop()
|
||||
}
|
||||
|
||||
// Resolve relative paths to absolute paths
|
||||
if (target_pkg == '.' || target_pkg.startsWith('./') || target_pkg.startsWith('../') || fd.is_dir(target_pkg)) {
|
||||
var resolved = fd.realpath(target_pkg)
|
||||
if (resolved) {
|
||||
target_pkg = resolved
|
||||
}
|
||||
}
|
||||
|
||||
var packages_to_remove = [target_pkg]
|
||||
|
||||
if (prune) {
|
||||
// Find packages no longer needed
|
||||
// Get all dependencies of remaining packages
|
||||
var lock = shop.load_lock()
|
||||
var all_packages = shop.list_packages()
|
||||
|
||||
// Build set of all needed packages (excluding target)
|
||||
var needed = {}
|
||||
for (var p of all_packages) {
|
||||
if (p == target_pkg || p == 'core') continue
|
||||
|
||||
// Mark this package and its deps as needed
|
||||
needed[p] = true
|
||||
try {
|
||||
var deps = pkg.gather_dependencies(p)
|
||||
for (var dep of deps) {
|
||||
needed[dep] = true
|
||||
}
|
||||
} catch (e) {
|
||||
// Skip if can't read deps
|
||||
}
|
||||
}
|
||||
|
||||
// Find packages that are NOT needed
|
||||
for (var p of all_packages) {
|
||||
if (p == 'core') continue
|
||||
if (!needed[p] && packages_to_remove.indexOf(p) < 0) {
|
||||
packages_to_remove.push(p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (dry_run) {
|
||||
log.console("Would remove:")
|
||||
for (var p of packages_to_remove) {
|
||||
log.console(" " + p)
|
||||
}
|
||||
} else {
|
||||
for (var p of packages_to_remove) {
|
||||
// Remove any link for this package
|
||||
if (link.is_linked(p)) {
|
||||
link.remove(p)
|
||||
}
|
||||
|
||||
// Remove from shop
|
||||
shop.remove(p)
|
||||
}
|
||||
|
||||
log.console("Removed " + text(packages_to_remove.length) + " package(s).")
|
||||
}
|
||||
|
||||
$stop()
|
||||
|
||||
Reference in New Issue
Block a user