-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rashid Ksirov
committed
Mar 28, 2020
1 parent
5c1f7e6
commit 32b5762
Showing
6 changed files
with
116 additions
and
12 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
|
||
const {takeRightWhile, isUndefined} = require('lodash'); | ||
|
||
const normalizeArg = (arg) => typeof arg === 'object' ? JSON.stringify(arg) : `"${arg}"`; | ||
|
||
const getCommandWithArgs = ({name, args = []}) => `${name}(${args.map(normalizeArg).join(', ')})`; | ||
|
||
const getFileFromStack = ({stack}) => { | ||
if (!stack || typeof stack !== 'string') { | ||
return ''; | ||
} | ||
|
||
const openingParenthesisPos = stack.lastIndexOf('('); | ||
if (openingParenthesisPos !== -1) { | ||
const closingParenthesisPos = stack.lastIndexOf(')'); | ||
|
||
return stack.substring(openingParenthesisPos + 1, closingParenthesisPos); | ||
} | ||
|
||
return stack; | ||
}; | ||
|
||
const getCommandWithArgsAndFile = (record) => `${getCommandWithArgs(record)}: ${getFileFromStack(record)}`; | ||
|
||
const getCommandHistory = (allHistory, runnableFile) => { | ||
if (isUndefined(allHistory)) { | ||
return; | ||
} | ||
|
||
try { | ||
const commands = allHistory | ||
.filter(({stack}) => stack.includes(runnableFile)) | ||
.map(getCommandWithArgs) | ||
.map((s) => `\t${s}\n`); | ||
|
||
const lastSubHistory = takeRightWhile(allHistory, ({stack}) => !stack.includes(runnableFile)); | ||
|
||
const lastSubCommands = lastSubHistory | ||
.map(getCommandWithArgsAndFile) | ||
.map((s) => `\t\t${s}\n`); | ||
|
||
return [...commands, ...lastSubCommands]; | ||
} catch (e) { | ||
return `failed to get command history: ${e.message}`; | ||
} | ||
}; | ||
|
||
module.exports = { | ||
getCommandHistory | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
|
||
const {getCommandHistory} = require('lib/history-utils'); | ||
|
||
describe('history-utils', () => { | ||
describe('getCommandHistory', () => { | ||
it('should return commands executed in test file and all sub commands of the last command', async () => { | ||
const allHistory = [ | ||
{name: 'foo', args: ['foo-arg'], stack: 'foo("foo-arg") (/path/to/test/file:10:4)'}, | ||
{name: 'baz', args: ['baz-arg'], stack: 'baz("baz-arg") (/path/to/baz/file:20:4)'}, | ||
{name: 'bar', args: ['bar-arg'], stack: 'bar("bar-arg") (/path/to/test/file:11:4)'}, | ||
{name: 'qux', args: ['qux-arg'], stack: 'qux("qux-arg") (/path/to/qux/file:21:4)'} | ||
]; | ||
|
||
const history = await getCommandHistory(allHistory, '/path/to/test/file'); | ||
|
||
assert.deepEqual(history, [ | ||
'\tfoo("foo-arg")\n', | ||
'\tbar("bar-arg")\n', | ||
'\t\tqux("qux-arg"): /path/to/qux/file:21:4\n' | ||
]); | ||
}); | ||
|
||
it('should return undefined if all history is not given', async () => { | ||
const history = await getCommandHistory(undefined, '/path/to/test/file'); | ||
|
||
assert.isUndefined(history); | ||
}); | ||
|
||
it('should return failure message in case of exception', async () => { | ||
const history = await getCommandHistory([{}], '/path/to/test/file'); | ||
|
||
assert.match(history, /failed to get command history: .*/); | ||
}); | ||
}); | ||
}); |
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