diff --git a/src/runNpmScripts/index.js b/src/runNpmScripts/index.js index 0139167..973c63f 100644 --- a/src/runNpmScripts/index.js +++ b/src/runNpmScripts/index.js @@ -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 **/ @@ -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() { diff --git a/src/runNpmScripts/index.spec.js b/src/runNpmScripts/index.spec.js index 8e1e637..a8b0d25 100644 --- a/src/runNpmScripts/index.spec.js +++ b/src/runNpmScripts/index.spec.js @@ -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(['']); + }); });