Skip to content

Instantly share code, notes, and snippets.

@bagilevi
Last active December 11, 2016 16:37
Show Gist options
  • Select an option

  • Save bagilevi/a35e8d0e124d134756f358f880c85344 to your computer and use it in GitHub Desktop.

Select an option

Save bagilevi/a35e8d0e124d134756f358f880c85344 to your computer and use it in GitHub Desktop.
Remove all node_modules directories within a directory (Ruby)
#!/usr/bin/env ruby
# Remove all node_modules directories within a directory
base_dir = File.expand_path(ARGV[0])
# Boilerplate from: http://stackoverflow.com/questions/1154846/continuously-read-from-stdout-of-external-process-in-ruby#answer-1162850
require 'pty'
cmd = "find #{base_dir} -name node_modules"
begin
PTY.spawn( cmd ) do |stdout, stdin, pid|
begin
stdout.each do |line|
path = line.strip
if path.include?('node_modules') && !path.include?('node_modules/')
puts path
# puts `du -d 0 #{path}`
puts `rm -rf #{path}`
# puts
end
end
rescue Errno::EIO
puts "Errno:EIO error, but this probably just means " +
"that the process has finished giving output"
end
end
rescue PTY::ChildExited
puts "The child process exited!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment