Skip to content

Commit c7ab52c

Browse files
fix: correctly handle file reading and updates
This commit refactors the handling of file read/write operations to use synchronous methods for simplicity.
1 parent d5fb3f1 commit c7ab52c

40 files changed

+5381
-1886
lines changed

index.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const axios = require("axios");
22
const core = require("@actions/core");
33
const exec = require("@actions/exec");
4-
const fs = require("fs").promises;
4+
const fs = require("fs");
55
const tar = require("tar");
66
const { Octokit } = require("@octokit/rest");
77

@@ -24,9 +24,17 @@ async function downloadFile(url, outputPath) {
2424
async function checkForSecrets() {
2525
let secretsDetected = false;
2626

27-
const data = await fs.readFile(secretsFilePath, "utf8");
28-
if (!data || data.trim().length === 0) {
29-
console.log("No data or empty file found, skipping processing...");
27+
let data;
28+
try {
29+
data = fs.readFileSync(secretsFilePath, "utf8");
30+
} catch (err) {
31+
console.error(`Error reading file: ${err}`);
32+
throw err;
33+
}
34+
35+
// If the file is empty or only contains an empty array, assume no secrets found
36+
if (!data || data.trim() === "[]") {
37+
console.log("No secrets found, skipping processing...");
3038
return secretsDetected;
3139
}
3240

@@ -95,6 +103,11 @@ function getRepoData(repoUrl) {
95103
}
96104

97105
async function run() {
106+
// Ensure secrets.json exists at the start
107+
if (!fs.existsSync(secretsFilePath)) {
108+
fs.writeFileSync(secretsFilePath, "[]"); // Create an empty JSON array
109+
}
110+
98111
try {
99112
const tarballPath = "./trufflehog.tar.gz";
100113
await downloadFile(
@@ -103,7 +116,6 @@ async function run() {
103116
);
104117
await tar.x({ file: tarballPath });
105118

106-
let output = "";
107119
const options = {
108120
listeners: {
109121
stdout: (data) => {
@@ -132,9 +144,9 @@ async function run() {
132144
],
133145
options
134146
);
135-
fs.writeFileSync(secretsFilePath, output);
136147
} catch (error) {
137148
console.error(`Error executing trufflehog: ${error}`);
149+
throw error;
138150
}
139151

140152
const secretsFound = await checkForSecrets();

0 commit comments

Comments
 (0)