clean-old.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/env bash
  2. # look for old 0.x cruft, and get rid of it.
  3. # Should already be sitting in the npm folder.
  4. # This doesn't have to be quite as cross-platform as install.sh.
  5. # There are some bash-isms, because maintaining *two*
  6. # fully-portable posix/bourne sh scripts is too much for
  7. # one project with a sane maintainer.
  8. # If readlink isn't available, then this is just too tricky.
  9. # However, greadlink is fine, so Solaris can join the party, too.
  10. readlink="readlink"
  11. which $readlink >/dev/null 2>/dev/null
  12. if [ $? -ne 0 ]; then
  13. readlink="greadlink"
  14. which $readlink >/dev/null 2>/dev/null
  15. if [ $? -ne 0 ]; then
  16. echo "Can't find the readlink or greadlink command. Aborting."
  17. exit 1
  18. fi
  19. fi
  20. if [ "x$npm_config_prefix" != "x" ]; then
  21. PREFIXES=$npm_config_prefix
  22. else
  23. node="$NODE"
  24. if [ "x$node" = "x" ]; then
  25. node=`which node`
  26. fi
  27. if [ "x$node" = "x" ]; then
  28. echo "Can't find node to determine prefix. Aborting."
  29. exit 1
  30. fi
  31. PREFIX=`dirname $node`
  32. PREFIX=`dirname $PREFIX`
  33. echo "cleanup prefix=$PREFIX"
  34. PREFIXES=$PREFIX
  35. altprefix=`"$node" -e process.installPrefix`
  36. if [ "x$altprefix" != "x" ] && [ "x$altprefix" != "x$PREFIX" ]; then
  37. echo "altprefix=$altprefix"
  38. PREFIXES="$PREFIX $altprefix"
  39. fi
  40. fi
  41. # now prefix is where npm would be rooted by default
  42. # go hunting.
  43. packages=
  44. for prefix in $PREFIXES; do
  45. packages="$packages
  46. "`ls "$prefix"/lib/node/.npm 2>/dev/null | grep -v .cache`
  47. done
  48. packages=`echo $packages`
  49. filelist=()
  50. fid=0
  51. for prefix in $PREFIXES; do
  52. # remove any links into the .npm dir, or links to
  53. # version-named shims/symlinks.
  54. for folder in share/man bin lib/node; do
  55. find $prefix/$folder -type l | while read file; do
  56. target=`$readlink $file | grep '/\.npm/'`
  57. if [ "x$target" != "x" ]; then
  58. # found one!
  59. filelist[$fid]="$file"
  60. let 'fid++'
  61. # also remove any symlinks to this file.
  62. base=`basename "$file"`
  63. base=`echo "$base" | awk -F@ '{print $1}'`
  64. if [ "x$base" != "x" ]; then
  65. find "`dirname $file`" -type l -name "$base"'*' \
  66. | while read l; do
  67. target=`$readlink "$l" | grep "$base"`
  68. if [ "x$target" != "x" ]; then
  69. filelist[$fid]="$1"
  70. let 'fid++'
  71. fi
  72. done
  73. fi
  74. fi
  75. done
  76. # Scour for shim files. These are relics of 0.2 npm installs.
  77. # note: grep -r is not portable.
  78. find $prefix/$folder -type f \
  79. | xargs grep -sl '// generated by npm' \
  80. | while read file; do
  81. filelist[$fid]="$file"
  82. let 'fid++'
  83. done
  84. done
  85. # now remove the package modules, and the .npm folder itself.
  86. if [ "x$packages" != "x" ]; then
  87. for pkg in $packages; do
  88. filelist[$fid]="$prefix/lib/node/$pkg"
  89. let 'fid++'
  90. for i in $prefix/lib/node/$pkg\@*; do
  91. filelist[$fid]="$i"
  92. let 'fid++'
  93. done
  94. done
  95. fi
  96. for folder in lib/node/.npm lib/npm share/npm; do
  97. if [ -d $prefix/$folder ]; then
  98. filelist[$fid]="$prefix/$folder"
  99. let 'fid++'
  100. fi
  101. done
  102. done
  103. # now actually clean, but only if there's anything TO clean
  104. if [ "${#filelist[@]}" -gt 0 ]; then
  105. echo ""
  106. echo "This script will find and eliminate any shims, symbolic"
  107. echo "links, and other cruft that was installed by npm 0.x."
  108. echo ""
  109. if [ "x$packages" != "x" ]; then
  110. echo "The following packages appear to have been installed with"
  111. echo "an old version of npm, and will be removed forcibly:"
  112. for pkg in $packages; do
  113. echo " $pkg"
  114. done
  115. echo "Make a note of these. You may want to install them"
  116. echo "with npm 1.0 when this process is completed."
  117. echo ""
  118. fi
  119. OK=
  120. if [ "x$1" = "x-y" ]; then
  121. OK="yes"
  122. fi
  123. while [ "$OK" != "y" ] && [ "$OK" != "yes" ] && [ "$OK" != "no" ]; do
  124. echo "Is this OK?"
  125. echo " enter 'yes' or 'no'"
  126. echo " or 'show' to see a list of files "
  127. read OK
  128. if [ "x$OK" = "xshow" ] || [ "x$OK" = "xs" ]; then
  129. for i in "${filelist[@]}"; do
  130. echo "$i"
  131. done
  132. fi
  133. done
  134. if [ "$OK" = "no" ]; then
  135. echo "Aborting"
  136. exit 1
  137. fi
  138. for i in "${filelist[@]}"; do
  139. rm -rf "$i"
  140. done
  141. fi
  142. echo ""
  143. echo 'All clean!'
  144. exit 0