Skip to content

Commit

Permalink
Support -p/--port for magic-script run
Browse files Browse the repository at this point in the history
If an valid port is specified use the port.
Otherwise a port hashed from the package
name is used.
  • Loading branch information
Leo Yang committed Apr 17, 2019
1 parent b21840d commit e852f5d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
56 changes: 42 additions & 14 deletions __tests__/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ describe('Test Run', () => {
callback(true);
});
jest.spyOn(console, 'info').mockImplementationOnce((data) => {
expect(data).toBe('Launching:');
expect(data).toBe('Launching: com.abc at port: 56965');
});
jest.spyOn(console, 'error').mockImplementationOnce((data) => {
expect(data).toBe('exec error: error');
});
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb ps');
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb launch com.abc');
expect(command).toBe('mldb launch -v INSPECTOR_PORT=56965 com.abc');
});
callback('error');
});
Expand All @@ -108,15 +108,15 @@ describe('Test Run', () => {
callback(true);
});
jest.spyOn(console, 'info').mockImplementationOnce((data) => {
expect(data).toBe('Launching:');
expect(data).toBe('Launching: com.abc at port: 56965');
});
jest.spyOn(console, 'error').mockImplementationOnce((data) => {
expect(data).toBe('exec error: error');
});
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb ps');
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb launch com.abc');
expect(command).toBe('mldb launch -v INSPECTOR_PORT=56965 com.abc');
callback('error');
});
callback('error');
Expand All @@ -142,15 +142,15 @@ describe('Test Run', () => {
callback(true);
});
jest.spyOn(console, 'info').mockImplementationOnce((data) => {
expect(data).toBe('Launching:');
expect(data).toBe('Launching: com.abc at port: 56965');
});
jest.spyOn(console, 'error').mockImplementationOnce((data) => {
expect(data).toBe('exec error: error');
});
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb ps');
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb launch --auto-net-privs com.abc');
expect(command).toBe('mldb launch --auto-net-privs -v INSPECTOR_PORT=56965 com.abc');
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb ps');
callback(null, '1440 110011 Running com.abc .universe');
Expand Down Expand Up @@ -181,15 +181,15 @@ describe('Test Run', () => {
callback(true);
});
jest.spyOn(console, 'info').mockImplementationOnce((data) => {
expect(data).toBe('Launching:');
expect(data).toBe('Launching: com.abc at port: 56965');
});
jest.spyOn(console, 'error').mockImplementationOnce((data) => {
expect(data).toBe('exec error: error');
});
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb ps');
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb launch --auto-net-privs com.abc');
expect(command).toBe('mldb launch --auto-net-privs -v INSPECTOR_PORT=56965 com.abc');
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb ps');
callback(null, '1440 110011 Running com.abc .universe');
Expand Down Expand Up @@ -224,15 +224,15 @@ describe('Test Run', () => {
callback(true);
});
jest.spyOn(console, 'info').mockImplementationOnce((data) => {
expect(data).toBe('Launching:');
expect(data).toBe('Launching: com.abc at port: 56965');
});
jest.spyOn(console, 'error').mockImplementationOnce((data) => {
expect(data).toBe('exec error: error');
});
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb ps');
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb launch --auto-net-privs com.abc');
expect(command).toBe('mldb launch --auto-net-privs -v INSPECTOR_PORT=56965 com.abc');
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb ps');
callback(null, '1440 110011 Running com.abc .universe');
Expand Down Expand Up @@ -267,15 +267,15 @@ describe('Test Run', () => {
callback(true);
});
jest.spyOn(console, 'info').mockImplementationOnce((data) => {
expect(data).toBe('Launching:');
expect(data).toBe('Launching: com.abc at port: 56965');
});
jest.spyOn(console, 'error').mockImplementationOnce((data) => {
expect(data).toBe('exec error: error');
});
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb ps');
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb launch --auto-net-privs com.abc');
expect(command).toBe('mldb launch --auto-net-privs -v INSPECTOR_PORT=56965 com.abc');
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb ps');
callback(null, '1440 110011 Running com.abc .universe');
Expand All @@ -295,15 +295,15 @@ describe('Test Run', () => {
callback(true);
});
jest.spyOn(console, 'info').mockImplementationOnce((data) => {
expect(data).toBe('Launching:');
expect(data).toBe('Launching: com.abc at port: 56965');
});
jest.spyOn(console, 'error').mockImplementationOnce((data) => {
expect(data).toBe('exec error: error');
});
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb ps');
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb launch com.abc');
expect(command).toBe('mldb launch -v INSPECTOR_PORT=56965 com.abc');
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb ps');
callback(null, '1440 110011 Running com.abc .universe');
Expand All @@ -317,6 +317,34 @@ describe('Test Run', () => {
expect(child_process.exec).toHaveBeenCalled();
});

test('Installed "com.abc" running launch with port specified success no debug', () => {
const mockIsInstalled = jest.spyOn(util, 'isInstalled').mockImplementationOnce((packageName, callback) => {
expect(packageName == 'com.abc').toBeTruthy();
callback(true);
});
jest.spyOn(console, 'info').mockImplementationOnce((data) => {
expect(data).toBe('Launching: com.abc at port: 12345');
});
jest.spyOn(console, 'error').mockImplementationOnce((data) => {
expect(data).toBe('exec error: error');
});
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb ps');
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb launch -v INSPECTOR_PORT=12345 com.abc');
child_process.exec.mockImplementationOnce((command, callback) => {
expect(command).toBe('mldb ps');
callback(null, '1440 110011 Running com.abc .universe');
});
callback(null, 'Success');
});
callback('error');
});
run({ '_': ['run', 'com.abc'], 'debug': false, 'port': 12345 });
expect(mockIsInstalled).toHaveBeenCalled();
expect(child_process.exec).toHaveBeenCalled();
});

test('Installed "com.abc" running terminated', () => {
const mockIsInstalled = jest.spyOn(util, 'isInstalled').mockImplementationOnce((packageName, callback) => {
expect(packageName == 'com.abc').toBeTruthy();
Expand Down
4 changes: 4 additions & 0 deletions bin/magic-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ require('yargs') // eslint-disable-line
boolean: true,
default: true
});
yargs.option('port', {
alias: 'p',
default: 0
});
}, argv => require('../commands/run')(argv))
.option('verbose', {
alias: 'v',
Expand Down
13 changes: 11 additions & 2 deletions commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
// Distributed under MIT License. See LICENSE file in the project root for full license information.
let { exec, spawn } = require('child_process');
let util = require('../lib/util');
let hash = require('hash-index')

let packageName;
let debug;
let port;

function getPortFromPackageName() {
return hash(packageName, 65535 - 1024) + 1024
}

function isRunning (callback) {
exec('mldb ps', (err, stdout, stderr) => {
Expand Down Expand Up @@ -32,8 +38,10 @@ function isRunning (callback) {

function launchFunction (callback) {
let autoPrivilege = debug ? ' --auto-net-privs' : '';
let launchCommand = `mldb launch${autoPrivilege} ${packageName}`;
console.info('Launching:', packageName);
let portNumber = port > 0 && port < 65536 ? port : getPortFromPackageName();
let portArg = '-v INSPECTOR_PORT=' + portNumber;
let launchCommand = `mldb launch${autoPrivilege} ${portArg} ${packageName}`;
console.info(`Launching: ${packageName} at port: ${portNumber}`);
exec(launchCommand, (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
Expand Down Expand Up @@ -92,6 +100,7 @@ function launchCallback (pid) {
module.exports = argv => {
let localArguments = argv._;
debug = argv.debug;
port = argv.port;
if (localArguments.length > 1) {
packageName = localArguments[1];
} else {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"bugs": "https://github.com/magic-script/magic-script-cli/issues",
"dependencies": {
"hash-index": "^3.0.0",
"glob": "^7.1.3",
"which": "^1.3.1",
"yargs": "^12.0.2"
Expand Down

0 comments on commit e852f5d

Please sign in to comment.