Skip to content

Commit

Permalink
v1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
selfagency committed Sep 19, 2022
1 parent 303390d commit bc20750
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ Required inputs:

Optional inputs:

- `args`: A comma-separated list of arguments to run the command with
- `hide-warnings`: Whether to report warnings in the GitHub Actions logs
- `args`: A comma-separated list of arguments with which to run the command
- `hide-warnings`: Hide warnings in the GitHub Actions logs (defaults to `false`)
- `fail`: Fail the step if an error is detected (defaults to `true`)
- `file`: The file to in which to record the output

Outputs:

- `output`: The command output (stdout + stderr)
- `output`: The full command output (stdout + stderr)
- `stdout`: The result of stdout
- `stderr`: The result of stderr
- `exit-code`: The command's exit code
- `duration`: The time the command took to run, in seconds

```
Expand All @@ -28,10 +32,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Capture output
uses: 'selfagency/[email protected].1'
uses: 'selfagency/[email protected].2'
with:
cmd: yarn
args: run,build,--verbose
hide-warnings: true
fail: false
file: ${{ github.workspace }}/output.txt
```
20 changes: 16 additions & 4 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3222,10 +3222,14 @@ var import_perf_hooks = require("perf_hooks");
var import_promises = require("fs/promises");
var import_core = __toESM(require_core(), 1);
var import_exec = __toESM(require_exec(), 1);
var errorOut = (data, hideWarning) => {
var errorOut = (data, hideWarning = false, fail = true) => {
var _a, _b, _c;
if (((_a = data == null ? void 0 : data.toLowerCase()) == null ? void 0 : _a.includes("error")) && !((_b = data == null ? void 0 : data.toLowerCase()) == null ? void 0 : _b.includes("warn")) && !(data == null ? void 0 : data.includes("ESLint must be installed")) && !(data == null ? void 0 : data.startsWith("error Command failed."))) {
import_core.default.setFailed(data);
if (fail) {
import_core.default.setFailed(data);
} else {
import_core.default.error(data);
}
} else if (!hideWarning && ((_c = data == null ? void 0 : data.toLowerCase()) == null ? void 0 : _c.includes("warn"))) {
import_core.default.warning(data);
} else {
Expand All @@ -3239,21 +3243,27 @@ var errorOut = (data, hideWarning) => {
const args = (_a = import_core.default.getInput("args")) == null ? void 0 : _a.split(" ");
const hideWarning = import_core.default.getInput("hide-warning") === "true";
const file = import_core.default.getInput("file");
const fail = import_core.default.getInput("fail") === "true";
let output = "";
let stdout = "";
let stderr = "";
const start = import_perf_hooks.performance.now();
import_core.default.debug("Running command: " + op + " " + args.join(" "));
await import_exec.default.exec(op, args, {
const exitCode = await import_exec.default.exec(op, args, {
listeners: {
stdout: (data) => {
stdout += data.toString();
output += data.toString();
import_core.default.debug(data.toString());
},
stderr: (data) => {
stderr += data.toString();
output += data.toString();
errorOut(data.toString(), hideWarning);
errorOut(data.toString(), hideWarning, fail);
}
}
});
import_core.default.setOutput("exit-code", exitCode);
const end = import_perf_hooks.performance.now();
import_core.default.setOutput("duration", ((end - start) / 1e3).toFixed(2));
output = output.trim();
Expand All @@ -3263,6 +3273,8 @@ Final output:
*************
${output}`);
import_core.default.setOutput("output", output);
import_core.default.setOutput("stdout", stdout);
import_core.default.setOutput("stderr", stderr);
if (file) {
await (0, import_promises.writeFile)(file, output);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "capture-output",
"version": "1.0.1",
"version": "1.0.2",
"description": "Capture the output (and duration) of a command as a GitHub Actions `output` or file",
"repository": {
"type": "git",
Expand Down
23 changes: 19 additions & 4 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import { writeFile } from 'fs/promises';
import core from '@actions/core';
import exec from '@actions/exec';

const errorOut = (data: string, hideWarning?: boolean) => {
const errorOut = (data: string, hideWarning = false, fail = true) => {
if (
data?.toLowerCase()?.includes('error') &&
!data?.toLowerCase()?.includes('warn') &&
!data?.includes('ESLint must be installed') &&
!data?.startsWith('error Command failed.')
) {
core.setFailed(data);
if (fail) {
core.setFailed(data);
} else {
core.error(data);
}
} else if (!hideWarning && data?.toLowerCase()?.includes('warn')) {
core.warning(data);
} else {
Expand All @@ -24,31 +28,42 @@ const errorOut = (data: string, hideWarning?: boolean) => {
const args: string[] = core.getInput('args')?.split(' ');
const hideWarning: boolean = core.getInput('hide-warning') === 'true';
const file: string = core.getInput('file');
const fail: boolean = core.getInput('fail') === 'true';

let output = '';
let stdout = '';
let stderr = '';

const start = performance.now();

core.debug('Running command: ' + op + ' ' + args.join(' '));
await exec.exec(op, args, {
const exitCode: number = await exec.exec(op, args, {
listeners: {
stdout: (data: Buffer) => {
stdout += data.toString();
output += data.toString();
core.debug(data.toString());
},
stderr: (data: Buffer) => {
stderr += data.toString();
output += data.toString();
errorOut(data.toString(), hideWarning);
errorOut(data.toString(), hideWarning, fail);
}
}
});

core.setOutput('exit-code', exitCode);

const end = performance.now();
core.setOutput('duration', ((end - start) / 1000).toFixed(2));

output = output.trim();
core.debug(`\n*************\nFinal output:\n*************\n${output}`);
core.setOutput('output', output);

core.setOutput('stdout', stdout);
core.setOutput('stderr', stderr);

if (file) {
await writeFile(file, output);
}
Expand Down

0 comments on commit bc20750

Please sign in to comment.