Skip to content

Commit

Permalink
fix(run-npm-scripts): handle clear character
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Mollweide committed May 27, 2018
1 parent 0ca6c86 commit 99b71b4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/runNpmScripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ const { getProgram } = require('../commander');
const { getState } = require('../store');
const render = require('../render');

const parseText = (text) => {
// eslint-disable-next-line
return text.replace(new RegExp('\x1Bc', 'g'), '').split('\n');
};

/**
* @returns {Object} state
**/
Expand All @@ -36,11 +41,17 @@ function runNpmScripts() {
scriptName: program.script,
packagePath,
onRecieve(text) {
state[packageName].log = state[packageName].log.concat(getText(text.split('\n'), 'msg'));
if (text.search('\x1Bc') >= 0) {
state[packageName].log = [];
}
state[packageName].log = state[packageName].log.concat(getText(parseText(text), 'msg'));
render();
},
onError(text) {
state[packageName].log = state[packageName].log.concat(getText(text.split('\n'), 'error'));
if (text.search('\x1Bc') >= 0) {
state[packageName].log = [];
}
state[packageName].log = state[packageName].log.concat(getText(parseText(text), 'error'));
render();
},
onExit() {
Expand Down
26 changes: 26 additions & 0 deletions src/runNpmScripts/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,30 @@ describe('runNpmScripts', () => {
});
expect(runNpmScripts().utils.log).toEqual(['stop: utils']);
});
it('package.terminal.onRecieve \x1Bc -> clear log', () => {
getState.mockImplementation(() => ({}));
getScriptCommands.mockImplementation(() => ({ start: ['/path/to/package/utils', '/path/to/package/ui'] }));
getProgram.mockImplementation(() => ({ script: 'start' }));
isIgnoredPackage.mockImplementation(() => false);
basename.mockImplementation(_path => _path.split('/')[4]);
getText.mockImplementation(text => text);
render.mockImplementation(() => {});
runNpmScript.mockImplementation(({ onRecieve }) => {
onRecieve('\x1Bc');
});
expect(runNpmScripts().utils.log).toEqual(['']);
});
it('package.terminal.onError \x1Bc -> clear log', () => {
getState.mockImplementation(() => ({}));
getScriptCommands.mockImplementation(() => ({ start: ['/path/to/package/utils', '/path/to/package/ui'] }));
getProgram.mockImplementation(() => ({ script: 'start' }));
isIgnoredPackage.mockImplementation(() => false);
basename.mockImplementation(_path => _path.split('/')[4]);
getText.mockImplementation(text => text);
render.mockImplementation(() => {});
runNpmScript.mockImplementation(({ onError }) => {
onError('\x1Bc');
});
expect(runNpmScripts().utils.log).toEqual(['']);
});
});

0 comments on commit 99b71b4

Please sign in to comment.