Merge pull request #844 from atom/custom-resource-loader

Add custom resource loader, fixes #197
This commit is contained in:
Cheng Zhao
2013-09-16 18:44:21 -07:00
5 changed files with 59 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
AtomWindow = require 'atom-window'
ApplicationMenu = require 'application-menu'
AtomProtocolHandler = require 'atom-protocol-handler'
BrowserWindow = require 'browser-window'
Menu = require 'menu'
autoUpdater = require 'auto-updater'
@@ -45,6 +46,7 @@ class AtomApplication
windows: null
applicationMenu: null
atomProtocolHandler: null
resourcePath: null
version: null
@@ -56,6 +58,7 @@ class AtomApplication
@windows = []
@applicationMenu = new ApplicationMenu(@version, devMode)
@atomProtocolHandler = new AtomProtocolHandler(@resourcePath)
@listenForArgumentsFromNewProcess()
@setupJavaScriptArguments()

View File

@@ -0,0 +1,27 @@
app = require 'app'
fs = require 'fs'
path = require 'path'
protocol = require 'protocol'
# Private: Handles requests with 'atom' protocol.
#
# It's created by {AtomApplication} upon instantiation, and is used to create a
# custom resource loader by adding the 'atom' custom protocol.
module.exports =
class AtomProtocolHandler
constructor: (@resourcePath) ->
@loadPaths = [
path.join(@resourcePath, 'node_modules')
path.join(app.getHomeDir(), '.atom', 'packages')
]
@registerAtomProtocol()
# Private: Creates the 'atom' custom protocol handler.
registerAtomProtocol: ->
protocol.registerProtocol 'atom', (request) =>
relativePath = path.normalize(request.url.substr(7))
for loadPath in @loadPaths
filePath = path.join(loadPath, relativePath)
break if fs.statSyncNoException(filePath)?
return new protocol.RequestFileJob(filePath)