Skip to content

Instantly share code, notes, and snippets.

@tautologistics
Created March 29, 2016 12:43
Show Gist options
  • Save tautologistics/8ede855290a4639e9ebf to your computer and use it in GitHub Desktop.
Save tautologistics/8ede855290a4639e9ebf to your computer and use it in GitHub Desktop.
# This will collect module paths and versions for all
# modules loaded in the process. Should be called
# before any other require()s
versionCache = {}
(->
Path = require 'path'
Module = require('module')
Object.keys(Module._extensions).forEach (ext) ->
# Leave the JSON handler alone
return if ext is '.json'
origRequire = Module._extensions[ext]
Module._extensions[ext] = (info, filepath) ->
packageInfo = null
modulepath = Path.dirname filepath
basepath = Path.join __dirname, 'node_modules'
while not packageInfo? and modulepath?
try
packageInfo = require Path.join(modulepath, 'package.json')
catch ex
modulepath = Path.join modulepath, '..'
if modulepath is basepath or modulepath.length <= basepath.length
# We're at the top level or off the reservation, so stop
modulepath = null
if packageInfo?
versionCache[modulepath] = packageInfo.version
console.log 'REQUIRE [' + ext + ']', modulepath, packageInfo?
return origRequire.apply Module, arguments
)()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment