Skip to content

Commit

Permalink
Merge pull request brzpegasus#7 from brzpegasus/nw-dependency
Browse files Browse the repository at this point in the history
Update NW.js dependency
  • Loading branch information
brzpegasus committed Mar 15, 2015
2 parents cba0384 + 95810fe commit 8d2cf5b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 36 deletions.
17 changes: 10 additions & 7 deletions blueprints/node-webkit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ module.exports = {
},

afterInstall: function(options) {
var ui = this.ui;

return this.updatePackageJson(options).catch(function(error) {
ui.writeLine(chalk.red('Error updating package.json.'));
ui.writeError(error);
});
var _this = this;
var dependencies = this.project.dependencies();

return this.addNwConfig(options)
.then(function() {
if (!dependencies.nw) {
return _this.addPackageToProject('nw');
}
});
},

updatePackageJson: function(options) {
addNwConfig: function(options) {
var ui = this.ui;
var project = this.project;

Expand Down
53 changes: 24 additions & 29 deletions lib/commands/nw.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

var path = require('path');
var spawn = require('child_process').spawn;
var chalk = require('chalk');
var RSVP = require('rsvp');
var path = require('path');
var spawn = require('child_process').spawn;
var chalk = require('chalk');
var RSVP = require('rsvp');

var Promise = RSVP.Promise;

Expand Down Expand Up @@ -38,29 +38,30 @@ module.exports = {
return watcher;
},

nwStart: function() {
runNW: function() {
var ui = this.ui;
var outputStream = ui.outputStream;
var project = this.project;

return new Promise(function(resolve, reject) {
ui.writeLine(chalk.green('Starting nw.js...'));

var nwCommand = process.env.NW_PATH;
if (!nwCommand) {
if (process.platform === 'darwin') {
nwCommand = '/Applications/node-webkit.app/Contents/MacOS/node-webkit';
} else {
nwCommand = 'nw';
}
}
var findNW = require('../helpers/find-nw');
var nwCommand = findNW(project);

var child = spawn(nwCommand, ['.']);
child.stdout.pipe(outputStream, { end: false });
child.stderr.pipe(outputStream, { end: false });
var child = spawn(nwCommand, ['.'], { cwd: project.root, stdio: 'inherit' });

child.on('error', function(error) {
ui.writeLine(chalk.red('Error running nw.js.'));
reject(error);
if (error.code === 'ENOENT') {
ui.writeLine('');
ui.writeLine(chalk.red("Error running the following command: " + nwCommand));
ui.writeLine('');
ui.writeLine(chalk.yellow("Either re-run the blueprint with 'ember g node-webkit' to add NW.js as an NPM dependency in your project,"));
ui.writeLine(chalk.yellow("or set an environment variable named 'NW_PATH' pointing to your NW.js binary."));
reject();
} else {
ui.writeLine(chalk.red('Error running nw.js.'));
reject(error);
}
});

child.on('exit', function() {
Expand All @@ -73,15 +74,9 @@ module.exports = {
run: function(options) {
var _this = this;

var done = function() {
return RSVP.all([
// Start nw.js
_this.nwStart(),
// Keep watching until failure or signal to exit
new Promise(function() {})
]);
};

return this.buildWatch(options).then(done);
return this.buildWatch(options)
.then(function() {
return _this.runNW();
});
}
};
39 changes: 39 additions & 0 deletions lib/helpers/find-nw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

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

function getLocalNW(nwLocalPath) {
var nw;
var platform = os.platform();

if (platform === 'darwin') {
if (fs.existsSync(path.join(nwLocalPath, 'Contents'))) {
nw = path.join(nwLocalPath, 'Contents', 'MacOS', 'nwjs');
} else {
nw = path.join(nwLocalPath, 'nwjs.app', 'Contents', 'MacOS', 'nwjs');
}
} else if (platform === 'win32') {
nw = path.join(nwLocalPath, 'nw.exe');
} else {
nw = path.join(nwLocalPath, 'nw');
}

return nw;
}

module.exports = function(project) {
var nw;

var nwLocalPath = path.resolve(project.root, 'node_modules/nw/nwjs');
if (fs.existsSync(nwLocalPath)) {
nw = getLocalNW(nwLocalPath);
} else if (process.env.NW_PATH) {
nw = process.env.NW_PATH;
} else {
nw = 'nw';
}

return nw;
};

0 comments on commit 8d2cf5b

Please sign in to comment.