Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NW configuration for Ember 1.13.x #38

Merged
merged 1 commit into from
Jul 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions blueprints/node-webkit/files/vendor/node-webkit/shim.js

This file was deleted.

51 changes: 34 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
'use strict';

var fs = require('fs');
var path = require('path');

function injectScript(scriptName) {
var filePath = path.join(__dirname, 'lib', 'resources', scriptName);
return '<script>\n' + fs.readFileSync(filePath, { encoding: 'utf8' }) + '\n</script>';
}

module.exports = {
name: 'ember-node-webkit',

included: function(app) {
this._super.included(app);

app.import('vendor/node-webkit/shim.js', { prepend: true });
app.import({ development: 'vendor/node-webkit/reload.js' });
if (!process.env.EMBER_CLI_NW) { return; }

app.import({ development: 'vendor/node-webkit/reload.js' });
if (process.env.NW_TESTS_DEV) {
app.import({ test: 'vendor/node-webkit/browser-qunit-adapter.js' });
} else {
Expand All @@ -31,11 +37,15 @@ module.exports = {
},

postprocessTree: function(type, tree) {
var funnel = require('broccoli-funnel');
var mergeTrees = require('broccoli-merge-trees');
var replace = require('broccoli-string-replace');
if (!process.env.EMBER_CLI_NW) {
return tree;
}

if (type === 'all' && process.env.EMBER_ENV === 'test') {
var funnel = require('broccoli-funnel');
var mergeTrees = require('broccoli-merge-trees');
var replace = require('broccoli-string-replace');

if (type === 'all' && process.env.EMBER_ENV === 'test' && process.env.NW_TEST) {
// Update the base URL in `tests/index.html`
var index = replace(tree, {
files: ['tests/index.html'],
Expand All @@ -56,16 +66,13 @@ module.exports = {
if (testPageOptions) {
testPkg = replace(testPkg, {
files: ['tests/package.json'],
patterns: [
{
match: /"main":\s*"(index.html\?[^\"]+)"/,
replacement: '"main": "$1&' + testPageOptions + '"'
},
{
match: /"main":\s*"index.html"/,
replacement: '"main": "index.html?' + testPageOptions + '"'
}
]
patterns: [{
match: /"main":\s*"(index.html\?[^\"]+)"/,
replacement: '"main": "$1&' + testPageOptions + '"'
}, {
match: /"main":\s*"index.html"/,
replacement: '"main": "index.html?' + testPageOptions + '"'
}]
});
}

Expand All @@ -76,7 +83,17 @@ module.exports = {
},

contentFor: function(type) {
if (type === 'test-body' && process.env.EMBER_ENV === 'test' && process.env.NW_TEST) {
if (!process.env.EMBER_CLI_NW) { return; }

if (type === 'head') {
return injectScript('shim-head.js');
}

if (type === 'body-footer') {
return injectScript('shim-footer.js');
}

if (type === 'test-body' && process.env.EMBER_ENV === 'test') {
var testemServer = process.env.NW_TESTEM_SERVER_URL;
if (testemServer) {
return '<script src="' + testemServer + '/socket.io/socket.io.js"></script>';
Expand Down
4 changes: 4 additions & 0 deletions lib/commands/nw-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ module.exports = {
{ name: 'config-file', type: String, default: 'config/nw-package.js', aliases: ['f'] }
],

init: function() {
process.env.EMBER_CLI_NW = true;
},

buildApp: function(options) {
var buildTask = new this.tasks.Build({
ui: this.ui,
Expand Down
2 changes: 2 additions & 0 deletions lib/commands/nw-test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ module.exports = {
if (!this.testing) {
process.env.EMBER_CLI_TEST_COMMAND = true;
}

process.env.EMBER_CLI_NW = true;
},

tmp: function() {
Expand Down
4 changes: 4 additions & 0 deletions lib/commands/nw.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ module.exports = {
name: 'nw',
description: 'Builds your app and launches NW.js',

init: function() {
process.env.EMBER_CLI_NW = true;
},

availableOptions: [
{ name: 'environment', type: String, default: 'development', aliases: ['e', { 'dev': 'development' }, { 'prod': 'production' }] },
{ name: 'output-path', type: String, default: 'dist/', aliases: ['o'] }
Expand Down
25 changes: 25 additions & 0 deletions lib/resources/shim-footer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* jshint browser: true */
(function(window) {
if (!window.nwDispatcher) { return; }

// Restore NW.js variables.
global.window = window;
window.process = global.process;

// Redefine a global `require` function that can satisfy both
// Node and AMD module systems.
var requireAMD = window.require;
var requireNode = window.requireNode;

if (requireAMD) {
window.require = function() {
try {
return requireAMD.apply(null, arguments);
} catch (e) {
return requireNode.apply(null, arguments);
}
};
} else {
window.require = requireNode;
}
})(this);
13 changes: 13 additions & 0 deletions lib/resources/shim-head.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* jshint browser: true */
(function(window) {
if (!window.nwDispatcher) { return; }

// Delete some variables that mislead third-party libraries into thinking
// that the running environment is Node.
delete global.window; // can be accessed via `window`
delete window.process; // can be accessed via `global.process`

// Rename node's `require` to avoid conflicts with AMD's `require`.
window.requireNode = window.require;
delete window.require;
})(this);