Browse files

Initial commit with a bootstrap file to require the other stuff.

  • Loading branch information...
0 parents commit f8d389f967adf52534720f15833cca4f6ea90d84 @eventualbuddha committed Jul 6, 2012
Showing with 113 additions and 0 deletions.
  1. +105 −0 bootstrap.coffee
  2. +8 −0 vendor/coffee-script.js
105 bootstrap.coffee
@@ -0,0 +1,105 @@
+# This file borrows heavily from CasperJS.
+
+phantomRequire = require
+yunoRequirePath = '.'
+
+fs = ((fs) ->
+ unless fs.hasOwnProperty('basename')
+ fs.basename = (path) -> path.replace(/.*\//, '')
+
+ unless fs.hasOwnProperty('dirname')
+ fs.dirname = (path) ->
+ path.replace(/\\/g, '/').replace(/\/[^\/]*$/, '')
+
+ unless fs.hasOwnProperty('isWindows')
+ fs.isWindows = ->
+ testPath = arguments[0] or @workingDirectory
+ (/^[a-z]{1,2}:/i).test(testPath) or testPath.indexOf("\\\\") is 0
+
+ unless fs.hasOwnProperty('pathJoin')
+ fs.pathJoin = (args...) ->
+ args.join @separator
+
+ return fs
+)(phantomRequire('fs'))
+
+phantom.injectJs(fs.pathJoin(yunoRequirePath, 'vendor', 'coffee-script.js'))
+
+#
+# Retrieves the javascript source code from a given .js or .coffee file.
+#
+# @param String file The path to the file
+# @param Function|null onError An error callback (optional)
+#
+phantom.getScriptCode = (file, onError) ->
+ scriptCode = fs.read(file)
+ if /\.coffee$/i.test(file)
+ scriptCode = CoffeeScript.compile(scriptCode)
+ return scriptCode
+
+yunoRequire = ((require, requireDir) ->
+ phantomBuiltins = ['fs', 'webpage', 'webserver', 'system']
+ requireCache = {}
+
+ (path) ->
+ paths = []
+ fileGuesses = []
+ module = exports: {}
+
+ if phantomBuiltins.indexOf(path) isnt -1
+ return phantomRequire(path)
+
+ switch path[0]
+ when '.'
+ paths.push fs.absolute(path),
+ fs.absolute(fs.pathJoin requireDir, path)
+ when '/'
+ paths.push path
+ else
+ dir = fs.absolute requireDir
+ while dir isnt '' and dir.lastIndexOf(':') and dir.length - 1
+ # nodejs compatibility
+ paths.push fs.pathJoin(dir, 'node_modules', path)
+ dir = fs.dirname(dir)
+ paths.push fs.pathJoin(requireDir, 'lib', path)
+ paths.push fs.pathJoin(requireDir, 'modules', path)
+
+ for testPath in paths
+ fileGuesses.push(
+ testPath,
+ testPath + '.js',
+ testPath + '.coffee',
+ fs.pathJoin(testPath, 'index.js'),
+ fs.pathJoin(testPath, 'index.coffee'),
+ fs.pathJoin(testPath, 'lib', fs.basename(testPath) + '.js'),
+ fs.pathJoin(testPath, 'lib', fs.basename(testPath) + '.coffee')
+ )
+
+ file = null
+ for fileGuess in fileGuesses
+ break if file
+ if fs.isFile fileGuess
+ file = fileGuess
+
+ unless file
+ throw new Error "couldn't find module #{path}"
+
+ if file of requireCache
+ return requireCache[file].exports
+
+ scriptCode = phantom.getScriptCode(file)
+ fn = new Function('__file__', 'require', 'module', 'exports', scriptCode)
+ try
+ fn file, yunoRequire, module, module.exports
+ catch e
+ error = new Error "__mod_error(#{path}):: #{e}"
+ error.file = file
+ throw error
+
+ requireCache[file] = module
+ return module.exports
+)(phantomRequire, '.')
+
+path = phantom.args[0]
+path = "./#{path}" unless path[0] in ['/', '.']
+yunoRequire path
8 vendor/coffee-script.js
8 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.

0 comments on commit f8d389f

Please sign in to comment.