From ef2056c3f51d5440869e5f24f8b831d1276e2173 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 15 Apr 2013 15:39:54 +0800 Subject: [PATCH] Initial javascript startup code. --- atom.gyp | 3 ++- browser/api/lib/atom.coffee | 1 + browser/atom/atom.coffee | 45 +++++++++++++++++++++++++++----- browser/default_app/main.js | 5 ++++ browser/default_app/package.json | 5 ++++ 5 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 browser/api/lib/atom.coffee create mode 100644 browser/default_app/package.json diff --git a/atom.gyp b/atom.gyp index 7f85dd31aba8..974195570329 100644 --- a/atom.gyp +++ b/atom.gyp @@ -6,6 +6,7 @@ 'app/atom_main.cc', ], 'coffee_sources': [ + 'browser/api/lib/atom.coffee', 'browser/atom/atom.coffee', ], 'lib_sources': [ @@ -84,7 +85,7 @@ ], }, { - 'destination': '<(PRODUCT_DIR)/<(product_name).app/Contents/Resources', + 'destination': '<(PRODUCT_DIR)/<(product_name).app/Contents/Resources/browser', 'files': [ 'browser/default_app', ], diff --git a/browser/api/lib/atom.coffee b/browser/api/lib/atom.coffee new file mode 100644 index 000000000000..f38491e725ed --- /dev/null +++ b/browser/api/lib/atom.coffee @@ -0,0 +1 @@ +module.exports = global.__atom diff --git a/browser/atom/atom.coffee b/browser/atom/atom.coffee index e6744db1325a..56b5367fe8ca 100644 --- a/browser/atom/atom.coffee +++ b/browser/atom/atom.coffee @@ -1,7 +1,40 @@ +fs = require 'fs' +path = require 'path' + +# Provide default Content API implementations. +atom = {} + +atom.browserMainParts = + preMainMessageLoopRun: -> + # This is the start of the whole application, usually we should initialize + # the main window here. + +# Store atom object in global scope, apps can just override methods of it to +# implement various logics. +global.__atom = atom + +# Add Atom.app/Contents/Resources/browser/api/lib to require's search paths, +# which contains javascript of Atom's built-in libraries. +require('module').globalPaths.push path.join(__dirname, '..', 'api', 'lib') + # Don't quit on fatal error. -process.on "uncaughtException", (error) -> - console.error "uncaughtException:" - if error.stack? - console.error error.stack - else - console.error error.name + ": " + error.message +process.on 'uncaughtException', (error) -> + # TODO Show error in GUI. + message = error.stack ? "#{error.name}: #{error.message}" + console.error 'uncaughtException:' + console.error message + +# Now we try to load app's package.json. +packageJson = null + +packagePath = path.join __dirname, '..', '..', 'app' +try + # First we try to load Atom.app/Contents/Resources/app + packageJson = JSON.parse(fs.readFileSync(path.join(packagePath, 'package.json'))) +catch error + # If not found then we load Atom.app/Contents/Resources/browser/default_app + packagePath = path.join __dirname, '..', 'default_app' + packageJson = JSON.parse(fs.readFileSync(path.join(packagePath, 'package.json'))) + +# Finally load app's main.js and transfer control to C++. +require path.join(packagePath, packageJson.main) diff --git a/browser/default_app/main.js b/browser/default_app/main.js index e69de29bb2d1..77c0bc27e797 100644 --- a/browser/default_app/main.js +++ b/browser/default_app/main.js @@ -0,0 +1,5 @@ +var atom = require('atom'); + +atom.browserMainParts.preMainMessageLoopRun = function() { + console.log('Create new window'); +} diff --git a/browser/default_app/package.json b/browser/default_app/package.json new file mode 100644 index 000000000000..f495eeefd748 --- /dev/null +++ b/browser/default_app/package.json @@ -0,0 +1,5 @@ +{ + "name" : "atom", + "version" : "0.1.0", + "main" : "main.js" +}