-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(commands): Allow any command in focused mode
- Loading branch information
Simon Mollweide
committed
Nov 2, 2017
1 parent
1ae2503
commit aa52832
Showing
5 changed files
with
112 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters