Skip to content

Commit

Permalink
feat(style-terminal-panel-title): Display state of child process in p…
Browse files Browse the repository at this point in the history
…anel title
  • Loading branch information
Simon Mollweide committed Nov 2, 2017
1 parent aa52832 commit 3c0ffa4
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 18 deletions.
14 changes: 10 additions & 4 deletions src/getTerminalPanel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const { program } = require('../commander');
const themeSymbolMap = {
test: {
title: text => text,
titleStopped: text => text,
msg: text => text,
error: text => text,
titleSpace: ' ',
Expand All @@ -25,6 +26,7 @@ const themeSymbolMap = {
},
default: {
title: text => chalk.blue.bgWhite.bold(text),
titleStopped: text => chalk.red.bgWhite.bold(text),
msg: text => text,
error: text => chalk.red(text),
titleSpace: chalk.bgWhite(' '),
Expand All @@ -38,6 +40,7 @@ const themeSymbolMap = {
},
massive: {
title: text => chalk.blue.bgWhite.bold(text),
titleStopped: text => chalk.red.bgWhite.bold(text),
msg: text => text,
error: text => chalk.red(text),
titleSpace: chalk.bgWhite(' '),
Expand All @@ -51,6 +54,7 @@ const themeSymbolMap = {
},
minimal: {
title: text => chalk.blue.bgWhite.bold(text),
titleStopped: text => chalk.red.bgWhite.bold(text),
msg: text => text,
error: text => chalk.red(text),
titleSpace: chalk.bgWhite(' '),
Expand Down Expand Up @@ -114,10 +118,11 @@ function getLine({ filled, start, content, end, width }) {
/**
* @param {string} title - the title which will be displayed in start line
* @param {number} width - the line width
* @param {boolean} isRunning - flag for the child process is running or not
* @returns {string} - returns an start line
**/
function getStartLine(title, width) {
title = getThemeSymbol('title')(title);
function getStartLine(title, width, isRunning) {
title = isRunning ? getThemeSymbol('title')(title) : getThemeSymbol('titleStopped')(title);
const separator = getThemeSymbol('separatorTop');
const titleSpace = getThemeSymbol('titleSpace');
const topLeft = getThemeSymbol('topLeft');
Expand Down Expand Up @@ -158,9 +163,10 @@ function getEndLine(width) {
* @param {number} height - the panel height
* @param {string} title - the title which will be displayed in start line
* @param {Array<string>} lines - array with the content of the panel
* @param {boolean} isRunning - flag for is running or not
* @returns {string} - returns an bordered panel
**/
function getTerminalPanel(width, height, title, lines) {
function getTerminalPanel({ width, height, title, lines, isRunning = false }) {
width = parseInt(width, 10);
const out = [];
const rLines = lines.concat([]);
Expand All @@ -173,7 +179,7 @@ function getTerminalPanel(width, height, title, lines) {
shortenLines = rLines.concat([]);
}

out.push(getStartLine(title, width));
out.push(getStartLine(title, width, isRunning));
out.push(getEmptyLine(width));
while (availableLines > 0) {
if (shortenLines[renderedLines]) {
Expand Down
14 changes: 12 additions & 2 deletions src/getTerminalPanel/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,25 @@ describe('getTerminalPanel', () => {
describe('getTerminalPanel', () => {
it('default', () => {
const terminalPanel = JSON.stringify({
terminalPanel: getTerminalPanel(30, 10, 'title', ['npm run start', 'server started']),
terminalPanel: getTerminalPanel({
width: 30,
height: 10,
title: 'title',
lines: ['npm run start', 'server started'],
}),
});
// fs.writeFileSync('./src/getTerminalPanel/__snapshots__/get-terminal-panel-1.json', terminalPanel);
const expected = fs.readFileSync('./src/getTerminalPanel/__snapshots__/get-terminal-panel-1.json', 'utf8');
expect(terminalPanel).toEqual(expected);
});
it('more lines then available', () => {
const terminalPanel = JSON.stringify({
terminalPanel: getTerminalPanel(30, 5, 'title', ['npm run start', 'server started', 'a', 'b', 'c']),
terminalPanel: getTerminalPanel({
width: 30,
height: 5,
title: 'title',
lines: ['npm run start', 'server started', 'a', 'b', 'c'],
}),
});
// fs.writeFileSync('./src/getTerminalPanel/__snapshots__/get-terminal-panel-2.json', terminalPanel);
const expected = fs.readFileSync('./src/getTerminalPanel/__snapshots__/get-terminal-panel-2.json', 'utf8');
Expand Down
3 changes: 0 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
// TODOS
// - terminal title
// - don't ignore main script
// - execute command on childProcess
// -> lerna-terminal~$ cmd utils p (jest)
// -> focused lerna-terminal~$ p (jest)
// - generic usage

const { runCommander } = require('./commander');
Expand Down
13 changes: 7 additions & 6 deletions src/renderAllPanels/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ function renderAllPanels({ _state, _log, _renderClear, _renderCmdPrefix, _dimens
if (!currentState[packageName].log) {
return;
}
renderArr[counterRow][counterColumn] = getTerminalPanel(
panelWidth,
panelHeight,
packageName,
currentState[packageName].log
);
renderArr[counterRow][counterColumn] = getTerminalPanel({
width: panelWidth,
height: panelHeight,
title: packageName,
lines: currentState[packageName].log,
isRunning: currentState[packageName].terminal.isRunning,
});
counterColumn += 1;
if (counterColumn >= boardColumns) {
counterColumn = 0;
Expand Down
12 changes: 12 additions & 0 deletions src/renderAllPanels/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@ const { renderAllPanels } = require('./index');
const state = {
utils: {
log: ['started'],
terminal: {
isRunning: true,
},
},
ui: {
log: ['row'],
terminal: {
isRunning: true,
},
},
ui2: {
log: ['row'],
terminal: {
isRunning: true,
},
},
ui3: {
log: ['row'],
terminal: {
isRunning: true,
},
},
dateTime: {},
};
Expand Down
12 changes: 9 additions & 3 deletions src/runNpmScript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ function runNpmScript(
const run = cmd => {
const cmdArr = cmd.split(' ');

const childProcessObj = { isRunning: true };

const childProcess = _spawn(cmdArr[0], cmdArr.slice(1, cmdArr.length), {
shell: true,
cwd: packagePath,
Expand All @@ -34,10 +36,14 @@ function runNpmScript(
childProcess.stderr.on('data', data => {
onError(data.toString().replace(/\n$/, ''));
});
childProcess.on('exit', onExit);
childProcess.on('exit', () => {
childProcessObj.isRunning = false;
onExit();
});

return {
return Object.assign(childProcessObj, {
stop() {
childProcessObj.isRunning = false;
childProcess.kill('SIGINT');
},
start() {
Expand All @@ -48,7 +54,7 @@ function runNpmScript(
this.stop();
return run(newCmd);
},
};
});
};

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

0 comments on commit 3c0ffa4

Please sign in to comment.