Skip to content

Commit

Permalink
Fix outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
selfagency committed Oct 25, 2022
1 parent 6f29704 commit 7abe962
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 47 deletions.
49 changes: 27 additions & 22 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3249,18 +3249,19 @@ var errorOut = (data, hideWarning = false) => {
};
(async () => {
var _a;
const op = import_core.default.getInput("cmd");
const args = (_a = import_core.default.getInput("args")) == null ? void 0 : _a.replace(/'/g, "").split(",");
const hideWarning = import_core.default.getInput("hide-warnings") === "true";
const file = import_core.default.getInput("file");
const fail = import_core.default.getInput("fail") === "true";
let output = "";
let stdout = "";
let stderr = "";
let exitCode = 0;
const start = import_perf_hooks.performance.now();
try {
const op = import_core.default.getInput("cmd");
const args = (_a = import_core.default.getInput("args")) == null ? void 0 : _a.replace(/'/g, "").split(",");
const hideWarning = import_core.default.getInput("hide-warnings") === "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(" "));
const exitCode = await import_exec.default.exec(op, args, {
exitCode = await import_exec.default.exec(op, args, {
listeners: {
stdout: (data) => {
stdout += data.toString();
Expand All @@ -3274,29 +3275,33 @@ var errorOut = (data, hideWarning = false) => {
}
}
});
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();
import_core.default.debug(`
} catch (err) {
import_core.default.setFailed(`Unexpected error: ${err.message}`);
}
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();
import_core.default.debug(`
*************
Final output:
*************
${output}`);
import_core.default.setOutput("output", output);
import_core.default.setOutput("stdout", stdout);
import_core.default.setOutput("stderr", stderr);
import_core.default.setOutput("output", output);
import_core.default.setOutput("stdout", stdout);
import_core.default.setOutput("stderr", stderr);
try {
if (file) {
const path = file.split("/");
path.pop();
const dir = path.join("/");
await import_io.default.mkdirP(dir);
await (0, import_promises.writeFile)(file, output);
}
if (fail && exitCode != 0) {
import_core.default.setFailed(stderr);
}
} catch (err) {
import_core.default.setFailed(err.message);
import_core.default.setFailed(`Failed to write output to file: ${err.message}`);
}
if (fail && exitCode != 0) {
import_core.default.setFailed(stderr);
}
})();
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.8",
"version": "1.0.9",
"description": "Capture the output (and duration) of a command as a GitHub Actions `output` or file",
"repository": {
"type": "git",
Expand Down
53 changes: 29 additions & 24 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@ const errorOut = (data: string, hideWarning = false) => {
};

(async () => {
try {
const op: string = core.getInput('cmd');
const args: string[] = core.getInput('args')?.replace(/'/g, '').split(',');
const hideWarning: boolean = core.getInput('hide-warnings') === 'true';
const file: string = core.getInput('file');
const fail: boolean = core.getInput('fail') === 'true';
const op: string = core.getInput('cmd');
const args: string[] = core.getInput('args')?.replace(/'/g, '').split(',');
const hideWarning: boolean = core.getInput('hide-warnings') === 'true';
const file: string = core.getInput('file');
const fail: boolean = core.getInput('fail') === 'true';

let output = '';
let stdout = '';
let stderr = '';
let output = '';
let stdout = '';
let stderr = '';
let exitCode = 0;

const start = performance.now();
const start = performance.now();

try {
core.debug('Running command: ' + op + ' ' + args.join(' '));
const exitCode: number = await exec.exec(op, args, {
exitCode = await exec.exec(op, args, {
listeners: {
stdout: (data: Buffer) => {
stdout += data.toString();
Expand All @@ -48,31 +49,35 @@ const errorOut = (data: string, hideWarning = false) => {
}
}
});
} catch (err) {
core.setFailed(`Unexpected error: ${(<Error>err).message}`);
}

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

const end = performance.now();
core.setOutput('duration', ((end - start) / 1000).toFixed(2));
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);
output = output.trim();
core.debug(`\n*************\nFinal output:\n*************\n${output}`);
core.setOutput('output', output);

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

try {
if (file) {
const path = file.split('/');
path.pop();
const dir = path.join('/');
await io.mkdirP(dir);
await writeFile(file, output);
}

if (fail && exitCode != 0) {
core.setFailed(stderr);
}
} catch (err) {
core.setFailed((<Error>err).message);
core.setFailed(`Failed to write output to file: ${(<Error>err).message}`);
}

if (fail && exitCode != 0) {
core.setFailed(stderr);
}
})();

0 comments on commit 7abe962

Please sign in to comment.