Skip to content

Commit

Permalink
feat(commands): Allow any command in focused mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Mollweide committed Nov 2, 2017
1 parent 1ae2503 commit aa52832
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 17 deletions.
46 changes: 43 additions & 3 deletions src/cmdNative/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,57 @@
/* eslint complexity: 0*/

'use strict';
const resolve = require('../resolve');

const { state } = require('../store');
const { state, uiState } = require('../store');

const isValidChildProcessExecute = (_state, packageName) => {
return (
_state[packageName] &&
_state[packageName].terminal &&
typeof _state[packageName].terminal.execute === 'function'
);
};

const notifications = {
invalidCmd: () => ({
type: 'error',
message: `The entered command is invalid, please enter help for more informations.`,
delay: 2000,
}),
invalidChildProcessExecute: () => ({
type: 'error',
message: `Child process not found, please enter help for more informations.`,
delay: 2000,
}),
};

/**
* @param {string} cmd - the entered command
* @param {string} packageName - the package name
* @param {Function} render - the callback which should be a render function
* @param {Object} ui - dependency injection
* @returns {void}
**/
function cmdNative(packageName, render) {
function cmdNative(cmd, packageName, render, { _state, _uiState }) {
if (typeof cmd !== 'string' || cmd === '') {
_uiState.notifications.push(notifications.invalidCmd());
return;
}

cmd = cmd.trim();

if (typeof packageName !== 'string' || packageName === '') {
return;
}
if (!isValidChildProcessExecute(_state, packageName)) {
_uiState.notifications.push(notifications.invalidChildProcessExecute());
return;
}

_state[packageName].terminal.execute(cmd);
render();
}

module.exports = resolve(cmdNative, { state });
module.exports = resolve(cmdNative, { state, uiState });
module.exports.cmdNative = cmdNative;
47 changes: 43 additions & 4 deletions src/cmdNative/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,50 @@
const dcopy = require('deep-copy');
const resolve = require('../resolve');
const { cmdNative } = require('./index');

const state = {};
const state = {
test: {
terminal: {
execute() {},
},
},
testInvalid: {},
};
const uiState = { notifications: [] };

describe('cmdNative', () => {
it('uiState.focus should be empty', done => {
const _cmdNative = resolve(cmdNative, { state });
expect(_cmdNative(undefined, done)).toBe(undefined);
it('invalid cmd because of missing cmd', () => {
const _uiState = dcopy(uiState);
const _cmdNative = resolve(cmdNative, { state, uiState: _uiState });
_cmdNative(undefined, 'packageName', () => {});
expect(_uiState.notifications.length).toBe(1);
});
it('invalid because of missing packageName', () => {
const _uiState = dcopy(uiState);
const _cmdNative = resolve(cmdNative, { state, uiState: _uiState });
_cmdNative('npm run start', undefined, () => {});
expect(_uiState.notifications.length).toBe(0);
});
it('invalid because of missing execute function', () => {
const _uiState = dcopy(uiState);
const _state = dcopy(state);
const _cmdNative = resolve(cmdNative, { state: _state, uiState: _uiState });
_cmdNative('npm run start', 'testInvalid', () => {});
expect(_uiState.notifications.length).toBe(1);
});
it('execute cmd', done => {
const _uiState = dcopy(uiState);
const _state = dcopy(state);
_state.test.terminal.execute = () => {
done();
};
const _cmdNative = resolve(cmdNative, { state: _state, uiState: _uiState });
_cmdNative('npm run start', 'test', () => {});
});
it('execute cmd -> rerender', done => {
const _uiState = dcopy(uiState);
const _state = dcopy(state);
const _cmdNative = resolve(cmdNative, { state: _state, uiState: _uiState });
_cmdNative('npm run start', 'test', done);
});
});
4 changes: 2 additions & 2 deletions src/executeCmd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const cmdClear = require('../cmdClear');
const cmdFocus = require('../cmdFocus');
const cmdHelp = require('../cmdHelp');
const cmdExit = require('../cmdExit');
const cmdNative = require('../cmdExit');
const cmdNative = require('../cmdNative');
const cmdInvalid = require('../cmdInvalid');
const isValidPackageName = require('../isValidPackageName');

Expand Down Expand Up @@ -77,7 +77,7 @@ function executeCmd(
if (focusedPackageName) {
// run native cmd on child_process for example 'npm run test' in case of a child_process is focused
if (!_isValidCmd) {
_cmdNative(focusedPackageName, _render);
_cmdNative(cmd, focusedPackageName, _render);
return;
}

Expand Down
22 changes: 14 additions & 8 deletions src/runNpmScript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,38 @@ function runNpmScript(
{ scriptName, packagePath, onExit = () => {}, onRecieve = () => {}, onError = () => {} },
{ _spawn }
) {
const run = () => {
const cmd = _spawn('npm', ['run', scriptName], {
const run = cmd => {
const cmdArr = cmd.split(' ');

const childProcess = _spawn(cmdArr[0], cmdArr.slice(1, cmdArr.length), {
shell: true,
cwd: packagePath,
});

cmd.stdout.on('data', data => {
childProcess.stdout.on('data', data => {
onRecieve(data.toString().replace(/\n$/, ''));
});
cmd.stderr.on('data', data => {
childProcess.stderr.on('data', data => {
onError(data.toString().replace(/\n$/, ''));
});
cmd.on('exit', onExit);
childProcess.on('exit', onExit);

return {
stop() {
cmd.kill('SIGINT');
childProcess.kill('SIGINT');
},
start() {
this.stop();
return run();
return run(`npm run ${scriptName}`);
},
execute(newCmd) {
this.stop();
return run(newCmd);
},
};
};

return run();
return run(`npm run ${scriptName}`);
}

module.exports = resolve(runNpmScript, { spawn });
10 changes: 10 additions & 0 deletions src/runNpmScript/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,14 @@ describe('runNpmScript', () => {
});
expect(typeof _runNpmScript(_options).start()).toBe('object');
});
it('execute() -> return run()', () => {
const _options = Object.assign({}, options);
const _spawnReturn = Object.assign({}, spawnReturn);
const _runNpmScript = resolve(runNpmScript, {
spawn() {
return _spawnReturn;
},
});
expect(typeof _runNpmScript(_options).execute('npm run test')).toBe('object');
});
});

0 comments on commit aa52832

Please sign in to comment.