Skip to content

Instantly share code, notes, and snippets.

@ondrejbartas
Created July 15, 2016 09:14
Show Gist options
  • Save ondrejbartas/c4c764980142c5bfd676756cc86b1411 to your computer and use it in GitHub Desktop.
Save ondrejbartas/c4c764980142c5bfd676756cc86b1411 to your computer and use it in GitHub Desktop.
require 'json'
package = JSON.parse(File.open('./package.json').read)
mainfiles = Dir['./node_modules/*'].map{|f| "#{f}/package.json"}.select{|f| File.exist?(f)}.map{|f| JSON.parse(File.open(f).read)}
organizationfiles = Dir[*Dir['./node_modules/*'].select{|f| !File.exist?("#{f}/package.json")}.map{|x| "#{x}/*"}].map{|f| "#{f}/package.json"}.select{|f| File.exist?(f)}.map{|f| JSON.parse(File.open(f).read)}
files = mainfiles | organizationfiles
out = {
'devDependencies' => {},
'dependencies' => {}
}
devDep = (package['devDependencies'] || {}).keys
files.select{|x| devDep.include?(x['name'])}.each{ |x| out['devDependencies'][x['name']] = x['version']}
dep = (package['dependencies'] || {}).keys
files.select{|x| dep.include?(x['name'])}.each{ |x| out['dependencies'][x['name']] = x['version']}
puts JSON.pretty_generate(package.merge(out))
File.open('./package.json', 'w:UTF-8').write("#{JSON.pretty_generate(package.merge(out))}\n")
puts "Written freezed packages into package.json"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment