Skip to content

Commit

Permalink
Merge 7902235 into 8ca8720
Browse files Browse the repository at this point in the history
  • Loading branch information
technote-space committed Sep 25, 2019
2 parents 8ca8720 + 7902235 commit 3afd194
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 69 deletions.
16 changes: 10 additions & 6 deletions README.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ logger.displayStdout('stdout1\nstdout2');
logger.displayStderr('stderr1\nstderr2');
logger.log();
logger.info('output info');
logger.endProcess();

// ##[group]Process name
// ::group::Process name
// [command]command
// >> stdout1
// >> stdout2
// ##[warning] >> stderr1
// ##[warning] >> stderr2
// ::warning:: >> stderr1
// ::warning:: >> stderr2
//
// > output info
// ::endgroup::
```

### Command
Expand All @@ -65,15 +67,17 @@ async function run() {
logger.log();
logger.startProcess('Options');
await command.execAsync({command: 'ls', altCommand: 'alt', quiet: true, suppressError: true, suppressOutput: true});
logger.endProcess();

// ##[group]Simple use
// ::group::Simple use
// [command]ls
// >> README.md
// >> src
//
// ##[endgroup]
// ##[group]Options
// ::endgroup::
// ::group::Options
// [command]alt
// ::endgroup::
}

run();
Expand Down
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ logger.displayStdout('stdout1\nstdout2');
logger.displayStderr('stderr1\nstderr2');
logger.log();
logger.info('output info');
logger.endProcess();

// ##[group]Process name
// ::group::Process name
// [command]command
// >> stdout1
// >> stdout2
// ##[warning] >> stderr1
// ##[warning] >> stderr2
// ::warning:: >> stderr1
// ::warning:: >> stderr2
//
// > output info
// ::endgroup::
```

### Command
Expand All @@ -65,15 +67,17 @@ async function run() {
logger.log();
logger.startProcess('Options');
await command.execAsync({command: 'ls', altCommand: 'alt', quiet: true, suppressError: true, suppressOutput: true});
logger.endProcess();

// ##[group]Simple use
// ::group::Simple use
// [command]ls
// >> README.md
// >> src
//
// ##[endgroup]
// ##[group]Options
// ::endgroup::
// ::group::Options
// [command]alt
// ::endgroup::
}

run();
Expand Down
44 changes: 22 additions & 22 deletions __tests__/api-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,16 @@ describe('ApiHelper', () => {

expect(await helper.commit(path.resolve(__dirname, 'fixtures'), 'test commit message', ['build1.json', 'build2.json'], octokit, context)).toBeTruthy();
stdoutCalledWith(mockStdout, [
'##[group]Start push to branch [test]',
'##[endgroup]',
'##[group]Creating blobs...',
'##[endgroup]',
'##[group]Creating tree...',
'##[endgroup]',
'##[group]Creating commit... [cd8274d15fa3ae2ab983129fb037999f264ba9a7]',
'##[endgroup]',
'##[group]Updating ref... [heads%2Ftest] [7638417db6d59f3c431d3e1f261cc637155684cd]',
'##[endgroup]',
'::group::Start push to branch [test]',
'::endgroup::',
'::group::Creating blobs...',
'::endgroup::',
'::group::Creating tree...',
'::endgroup::',
'::group::Creating commit... [cd8274d15fa3ae2ab983129fb037999f264ba9a7]',
'::endgroup::',
'::group::Updating ref... [heads%2Ftest] [7638417db6d59f3c431d3e1f261cc637155684cd]',
'::endgroup::',
]);
expect(process.env.GITHUB_SHA).toBe('7638417db6d59f3c431d3e1f261cc637155684cd');
});
Expand Down Expand Up @@ -351,7 +351,7 @@ describe('ApiHelper with params', () => {
await helper.updateRef(response, octokit, context);

stdoutCalledWith(mockStdout, [
'##[warning]Branch [test-branch] is protected.',
'::warning::Branch [test-branch] is protected.',
]);
});

Expand All @@ -369,7 +369,7 @@ describe('ApiHelper with params', () => {
await helper.updateRef(response, octokit, context);

stdoutCalledWith(mockStdout, [
'##[warning]Branch [test-branch] is protected.',
'::warning::Branch [test-branch] is protected.',
]);
});

Expand Down Expand Up @@ -419,16 +419,16 @@ describe('ApiHelper with params', () => {
expect(fn1).not.toBeCalled();
expect(fn2).toBeCalledTimes(1);
stdoutCalledWith(mockStdout, [
'##[group]Start push to branch [test-branch]',
'##[endgroup]',
'##[group]Creating blobs...',
'##[endgroup]',
'##[group]Creating tree...',
'##[endgroup]',
'##[group]Creating commit... [cd8274d15fa3ae2ab983129fb037999f264ba9a7]',
'##[endgroup]',
'##[group]Updating ref... [test-ref] [7638417db6d59f3c431d3e1f261cc637155684cd]',
'##[endgroup]',
'::group::Start push to branch [test-branch]',
'::endgroup::',
'::group::Creating blobs...',
'::endgroup::',
'::group::Creating tree...',
'::endgroup::',
'::group::Creating commit... [cd8274d15fa3ae2ab983129fb037999f264ba9a7]',
'::endgroup::',
'::group::Updating ref... [test-ref] [7638417db6d59f3c431d3e1f261cc637155684cd]',
'::endgroup::',
]);
});
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Command', () => {
stdoutCalledWith(mockStdout, [
'[command]alt',
' >> stdout',
'##[warning] >> stderr',
'::warning:: >> stderr',
]);
});

Expand Down
28 changes: 14 additions & 14 deletions __tests__/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ describe('Logger', () => {
it('should output log', () => {
const mockStdout = spyOnStdout();

logger.info('test');
logger.log('test');

stdoutCalledWith(mockStdout, ['> test']);
stdoutCalledWith(mockStdout, ['test']);
});
});

Expand All @@ -37,7 +37,7 @@ describe('Logger', () => {

logger.debug('test');

stdoutCalledWith(mockStdout, ['##[debug]test']);
stdoutCalledWith(mockStdout, ['::debug::test']);
});
});

Expand All @@ -47,7 +47,7 @@ describe('Logger', () => {

logger.error('test');

stdoutCalledWith(mockStdout, ['##[error]test']);
stdoutCalledWith(mockStdout, ['::error::test']);
});
});

Expand All @@ -57,7 +57,7 @@ describe('Logger', () => {

logger.warn('test');

stdoutCalledWith(mockStdout, ['##[warning]test']);
stdoutCalledWith(mockStdout, ['::warning::test']);
});
});

Expand Down Expand Up @@ -91,8 +91,8 @@ describe('Logger', () => {
logger.displayStderr('test1\ntest2\n');

stdoutCalledWith(mockStdout, [
'##[warning] >> test1',
'##[warning] >> test2',
'::warning:: >> test1',
'::warning:: >> test2',
]);
});
});
Expand All @@ -103,7 +103,7 @@ describe('Logger', () => {

logger.startProcess('test');

stdoutCalledWith(mockStdout, ['##[group]test']);
stdoutCalledWith(mockStdout, ['::group::test']);
});
});
});
Expand Down Expand Up @@ -160,9 +160,9 @@ describe('Logger with string array', () => {
logger.displayStderr(['test1\ntest2\n', 'test3']);

stdoutCalledWith(mockStdout, [
'##[warning] >> test1',
'##[warning] >> test2',
'##[warning] >> test3',
'::warning:: >> test1',
'::warning:: >> test2',
'::warning:: >> test3',
]);
});
});
Expand Down Expand Up @@ -195,7 +195,7 @@ describe('Logger with replacer', () => {

logger.startProcess('message with args %s %d: <replace target>', '<replace target>', 2);

stdoutCalledWith(mockStdout, ['##[group]message with args <replaced> 2: <replaced>']);
stdoutCalledWith(mockStdout, ['::group::message with args <replaced> 2: <replaced>']);
});
});
});
Expand All @@ -212,8 +212,8 @@ describe('Logger with mixed', () => {
logger.debug(['test1: %s %d: <replace target>', 'test2: %s %d: <replace target>'], '<replace target>', 2);

stdoutCalledWith(mockStdout, [
'##[debug]test1: <replaced> 2: <replaced>',
'##[debug]test2: <replaced> 2: <replaced>',
'::debug::test1: <replaced> 2: <replaced>',
'::debug::test2: <replaced> 2: <replaced>',
]);
});
});
Expand Down
38 changes: 18 additions & 20 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EOL } from 'os';
import { sprintf } from 'sprintf-js';
import { info, debug, error, warning, startGroup, endGroup } from '@actions/core';

/**
* Logger
Expand All @@ -22,57 +22,55 @@ export default class Logger {
*/
private splitMessage = (message: string): string[] => message.replace(/\r?\n$/, '').split(/\r?\n/);

/**
* @param {string} message message
* @param {any[]} args args
*/
public log = (message: string, ...args: any[]): void => { // eslint-disable-line @typescript-eslint/no-explicit-any
process.stdout.write(sprintf(this.replacer(message), ...args.map(arg => 'string' === typeof arg ? this.replacer(arg) : arg)) + EOL);
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private getOutputString = (message: string, ...args: any[]): string => sprintf(this.replacer(message), ...args.map(arg => 'string' === typeof arg ? this.replacer(arg) : arg));

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private multiLineOutput = (replacer: (string) => string, message: string | string[], ...args: any[]): void => {
private multiLineOutput = (output: (string) => void, replacer: null | ((string) => string), message: string | string[], ...args: any[]): void => {
if ('string' !== typeof message) {
message.forEach(message => {
this.multiLineOutput(replacer, message, ...args);
this.multiLineOutput(output, replacer, message, ...args);
});
return;
}

this.splitMessage(message).forEach(message => this.log(replacer(message), ...args));
this.splitMessage(message).forEach(message => output(this.getOutputString(replacer ? replacer(message) : message, ...args)));
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public info = (message: string | string[], ...args: any[]): void => this.multiLineOutput(message => `> ${message}`, message, ...args);
public log = (message: string | string[], ...args: any[]): void => this.multiLineOutput(info, null, message, ...args);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public info = (message: string | string[], ...args: any[]): void => this.multiLineOutput(info, message => `> ${message}`, message, ...args);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public debug = (message: string | string[], ...args: any[]): void => this.multiLineOutput(message => `##[debug]${message}`, message, ...args);
public debug = (message: string | string[], ...args: any[]): void => this.multiLineOutput(debug, null, message, ...args);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public error = (message: string | string[], ...args: any[]): void => this.multiLineOutput(message => `##[error]${message}`, message, ...args);
public error = (message: string | string[], ...args: any[]): void => this.multiLineOutput(error, null, message, ...args);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public warn = (message: string | string[], ...args: any[]): void => this.multiLineOutput(message => `##[warning]${message}`, message, ...args);
public warn = (message: string | string[], ...args: any[]): void => this.multiLineOutput(warning, null, message, ...args);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public displayCommand = (message: string | string[], ...args: any[]): void => this.multiLineOutput(message => `[command]${message}`, message, ...args);
public displayCommand = (message: string | string[], ...args: any[]): void => this.multiLineOutput(info, message => `[command]${message}`, message, ...args);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public displayStdout = (message: string | string[]): void => this.multiLineOutput(message => ` >> ${message}`, message);
public displayStdout = (message: string | string[]): void => this.multiLineOutput(info, message => ` >> ${message}`, message);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public displayStderr = (message: string | string[]): void => this.multiLineOutput(message => `##[warning] >> ${message}`, message);
public displayStderr = (message: string | string[]): void => this.multiLineOutput(warning, message => ` >> ${message}`, message);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public startProcess = (message: string, ...args: any[]): void => {
this.endProcess();
this.log(`##[group]${message}`, ...args);
startGroup(this.getOutputString(message, ...args));
Logger.isRequiredEndGroup = true;
};

public endProcess = (): void => {
if (Logger.isRequiredEndGroup) {
this.log('##[endgroup]');
endGroup();
Logger.isRequiredEndGroup = false;
}
};
Expand Down

0 comments on commit 3afd194

Please sign in to comment.