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