Skip to content

Instantly share code, notes, and snippets.

@nrser
Created February 28, 2017 18:34
Show Gist options
  • Save nrser/9bc033cc59fa59f5ef3f2b5242892736 to your computer and use it in GitHub Desktop.
Save nrser/9bc033cc59fa59f5ef3f2b5242892736 to your computer and use it in GitHub Desktop.
knit.rb - recursively `yarn link` together all `file:<path>` deps in package.json manifests.
#!/usr/bin/env ruby
# recursively `yarn link` together all `file:<path>` deps in package.json
# manifests.
#
# USAGE
#
# knit.rb PACKAGE_DIRECTORY
#
# AUTHOR nrser
# LICENSE MIT
#
require 'json'
DEPS_KEYS = ['dependencies', 'devDependencies', 'optionalDependencies']
$indent = 0
def log *msgs
msgs.map {|msg|
puts (" " * $indent) + if msg.is_a? String
msg
else
msg.inspect
end
}
puts (" " * $indent)
end
def indent &block
$indent += 1
rtn = block.call
$indent -= 1
rtn
end
def link package_dir
Dir.chdir package_dir do
log "link(#{ package_dir.inspect })", "in dir #{ Dir.pwd }"
package_json = JSON.load File.read('package.json')
DEPS_KEYS.each {|deps_key|
if package_json.key? deps_key
package_json[deps_key].each {|name, version|
if version.start_with? 'file:'
dir = version[('file:'.length)..-1]
log "processing #{ name }: #{ version }", "dir: #{ dir }"
indent { link dir }
Dir.chdir dir do
log "in dir #{ Dir.pwd }", "> yarn link"
`yarn link`
end
log "in dir #{ Dir.pwd }", "> yarn link '#{ name }'"
`yarn link '#{ name }'`
end
}
end
}
end
end
if __FILE__ == $0
if ARGV[0]
link ARGV[0]
else
puts "USAGE: knit.rb PACKAGE_DIRECTORY"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment