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

[ENHANCEMENT] Open android project command #14 #80

Merged
merged 3 commits into from
Oct 23, 2014
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
11 changes: 8 additions & 3 deletions lib/commands/open.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ var path = require('path');
module.exports = {
name: 'cordova:open',
aliases: ['cdv:open'],
description: 'open the ios project with the default application',
description: 'Open the native project with the default application',
works: 'insideProject',

run: function() {
return require('../tasks/open')(this.project)();
availableOptions: [
{ name: 'platform', type: String, default: 'ios' },
{ name: 'application', type: String}
],

run: function(options) {
return require('../tasks/open')(this.project, options.platform, options.application)();
}
};
20 changes: 14 additions & 6 deletions lib/tasks/open.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
'use strict';

var runCommand = require('../utils/run-command');
var path = require('path');
var Promise = require('../ext/promise');
var path = require('path');
var runCommand = require('../utils/run-command');
var getOpenCommand = require('../utils/open');

module.exports = function(project) {
module.exports = function(project, platform, application) {
var projectPath, command;
if (platform === 'ios') {
projectPath = path.join(project.root, 'cordova', 'platforms/ios/*.xcodeproj');
} else if (platform === 'android') {
projectPath = path.join(project.root, 'cordova', 'platforms/android/.project');
} else {
return Promise.reject(new Error('The ' + platform + ' platform is not supported. Please use "ios" or "android"'));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you provide an else here that throws an error informing the user that the platform they specified isn't supported? Rather than throwing, if you reject with an error it should propagate up and be displayed to the user. Please also add a test for that


projectPath = path.join(project.root, 'cordova', 'platforms/ios/*.xcodeproj');
var command = getOpenCommand(projectPath, application);

command = 'open ' + projectPath;
return runCommand(command, 'Opening ios project with the default application');
return runCommand(command, 'Opening ' + platform + ' project with the default application');
};
42 changes: 42 additions & 0 deletions lib/utils/open.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';
/*
This file needs to be removed when issue https://github.com/pwnall/node-open/issues/30
is resolved, original file can be seen here:
https://github.com/pwnall/node-open/blob/0c3ad272bfbc163cce8806e64630c623a9cfd8f4/lib/open.js
*/
module.exports = function(target, appName) {
var opener;

switch (process.platform) {
case 'darwin':
if (appName) {
opener = 'open -a "' + escape(appName) + '"';
} else {
opener = 'open';
}
break;
case 'win32':
// if the first parameter to start is quoted, it uses that as the title
// so we pass a blank title so we can quote the file we are opening
if (appName) {
opener = 'start "" "' + escape(appName) + '"';
} else {
opener = 'start';
}
break;
default:
if (appName) {
opener = escape(appName);
} else {
// use Portlands xdg-open everywhere else
opener = 'xdg-open';
}
break;
}

if (process.env.SUDO_USER) {
opener = 'sudo -u ' + process.env.SUDO_USER + ' ' + opener;
}

return opener + ' "' + target + '"';
}
72 changes: 65 additions & 7 deletions node-tests/unit/tasks/open-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,73 @@ describe('Tasks - Open', function() {
project = newProject();
});

describe('darwin', function() {
it('runs correct command', function() {
var open = proxyquire('../../lib/tasks/open', {
'../utils/run-command': function(command) {
expect(command).to.eql('open project-root/cordova/platforms/ios/*.xcodeproj');
}
it('rejects when the platform isn\'t supported', function() {
var open = proxyquire('../../lib/tasks/open', {});

return open(project, 'fake-platform').catch(function(err) {
expect(err.toString()).to.match(/platform is not supported/);
});
});

describe('runs correct command on each platform', function() {
var platform;
before(function() {
platform = process.platform;
});

after(function() {
process.platform = platform;
});

describe('darwin', function() {
beforeEach(function() {
process.platform = 'darwin';
});

it('ios', function() {
return assertOpenCommand(project, 'ios', 'open "project-root/cordova/platforms/ios/*.xcodeproj"');
});

return open(project);
it('android', function() {
return assertOpenCommand(project, 'android', 'open "project-root/cordova/platforms/android/.project"');
});
});

describe('win32', function() {
beforeEach(function() {
process.platform = 'win32';
});

it('ios', function() {
return assertOpenCommand(project, 'ios', 'start "project-root/cordova/platforms/ios/*.xcodeproj"');
});

it('android', function() {
return assertOpenCommand(project, 'android', 'start "project-root/cordova/platforms/android/.project"');
});
});

describe('other', function() {
beforeEach(function() {
process.platform = 'other';
});

it('ios', function() {
return assertOpenCommand(project, 'ios', 'xdg-open "project-root/cordova/platforms/ios/*.xcodeproj"');
});

it('android', function() {
return assertOpenCommand(project, 'android', 'xdg-open "project-root/cordova/platforms/android/.project"');
});
});
});
});

function assertOpenCommand(project, platform, assertion) {
var open = proxyquire('../../lib/tasks/open', {
'../utils/run-command': function(command) {
expect(command).to.eql(assertion);
}
});
return open(project, platform);
}