1
0
Fork 0

Add group functions to core

pull/98/head
Jonathan Clem 2019-08-28 22:35:27 -04:00
parent e35e0e640b
commit 8b9dfa809b
No known key found for this signature in database
GPG Key ID: 48C5B22E9FD6E80F
4 changed files with 72 additions and 1 deletions

View File

@ -78,3 +78,20 @@ catch (err) {
core.error(`Error ${err}, action may still succeed though`); core.error(`Error ${err}, action may still succeed though`);
} }
``` ```
This library can also wrap chunks of output in foldable groups.
```js
const core = require('@actions/core')
// Manually wrap output
core.groupStart('Do some function')
doSomeFunction()
core.groupEnd()
// Wrap an asynchronous function call
const result = await core.group('Do something async', async () => {
const response = await doSomeHTTPRequest()
return response
})
```

View File

@ -155,6 +155,27 @@ describe('@actions/core', () => {
assertWriteCalls([`##[warning]%0D%0Awarning%0A${os.EOL}`]) assertWriteCalls([`##[warning]%0D%0Awarning%0A${os.EOL}`])
}) })
it('startGroup starts a new group', () => {
core.startGroup('my-group')
assertWriteCalls([`##[group]my-group${os.EOL}`])
})
it('endGroup ends new group', () => {
core.endGroup()
assertWriteCalls([`##[endgroup]${os.EOL}`])
})
it('group wraps an async call in a group', async () => {
await core.group('mygroup', async () => {
process.stdout.write('in my group\n')
})
assertWriteCalls([
`##[group]mygroup${os.EOL}`,
'in my group\n',
`##[endgroup]${os.EOL}`
])
})
it('debug sets the correct message', () => { it('debug sets the correct message', () => {
core.debug('Debug') core.debug('Debug')
assertWriteCalls([`##[debug]Debug${os.EOL}`]) assertWriteCalls([`##[debug]Debug${os.EOL}`])

View File

@ -25,7 +25,7 @@ export function issueCommand(
process.stdout.write(cmd.toString() + os.EOL) process.stdout.write(cmd.toString() + os.EOL)
} }
export function issue(name: string, message: string): void { export function issue(name: string, message: string = ''): void {
issueCommand(name, {}, message) issueCommand(name, {}, message)
} }

View File

@ -130,3 +130,36 @@ export function error(message: string): void {
export function warning(message: string): void { export function warning(message: string): void {
issue('warning', message) issue('warning', message)
} }
/**
* Begin an output group.
*
* Output until the next `groupEnd` will be foldable in this group
*
* @param name The name of the output group
*/
export function startGroup(name: string): void {
issue('group', name)
}
/**
* End an output group.
*/
export function endGroup(): void {
issue('endgroup')
}
/**
* Wrap an asynchronous function call in a group.
*
* Returns the same type as the function itself.
*
* @param name The name of the group
* @param fn The function to wrap in the group
*/
export async function group<T>(name: string, fn: () => Promise<T>): Promise<T> {
startGroup(name)
const result = await fn()
endGroup()
return result
}