Skip to content

Commit ad8003c

Browse files
alerizzoCopilot
andauthored
Apply suggestions from code review
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 5d4863f commit ad8003c

File tree

3 files changed

+48
-31
lines changed

3 files changed

+48
-31
lines changed

src/commands/login.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ Get your token at: https://app.codacy.com/account/access-management
7676
"Invalid API token. Check that it is correct and not expired.",
7777
);
7878
}
79+
if (typeof apiErr?.status === "number") {
80+
throw new Error(
81+
`Codacy API returned an error (status ${apiErr.status}). Please try again or check your permissions.`,
82+
);
83+
}
7984
throw new Error(
8085
"Could not reach the Codacy API. Check your network connection.",
8186
);

src/utils/credentials.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,18 @@ import {
1111
getCredentialsPath,
1212
} from "./credentials";
1313

14+
const mockHomeDir = path.join(os.tmpdir(), `.codacy-home-${process.pid}`);
15+
1416
vi.mock("node:os", async () => {
1517
const actual = await vi.importActual<typeof import("node:os")>("node:os");
1618
return {
1719
...actual,
18-
homedir: () => os.tmpdir().replace(/\/$/, "") + "/.codacy-home-" + process.pid,
20+
homedir: () => mockHomeDir,
1921
};
2022
});
2123

2224
describe("credentials", () => {
23-
const credentialsDir = path.join(
24-
os.tmpdir().replace(/\/$/, "") + "/.codacy-home-" + process.pid,
25-
".codacy",
26-
);
25+
const credentialsDir = path.join(mockHomeDir, ".codacy");
2726
const credentialsFile = path.join(credentialsDir, "credentials");
2827

2928
beforeEach(() => {

src/utils/credentials.ts

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -91,26 +91,35 @@ export function saveCredentials(token: string): void {
9191
}
9292

9393
export function loadCredentials(): string | null {
94+
if (!fs.existsSync(CREDENTIALS_FILE)) {
95+
return null;
96+
}
97+
98+
const payload = fs.readFileSync(CREDENTIALS_FILE, "utf8");
99+
94100
try {
95-
if (!fs.existsSync(CREDENTIALS_FILE)) {
96-
return null;
97-
}
98-
const payload = fs.readFileSync(CREDENTIALS_FILE, "utf8");
99101
return decryptToken(payload);
100102
} catch {
103+
// Treat invalid/corrupted credentials as "no credentials"
101104
return null;
102105
}
103106
}
104107

105108
export function deleteCredentials(): boolean {
109+
if (!fs.existsSync(CREDENTIALS_FILE)) {
110+
return false;
111+
}
112+
106113
try {
107-
if (!fs.existsSync(CREDENTIALS_FILE)) {
108-
return false;
109-
}
110114
fs.unlinkSync(CREDENTIALS_FILE);
111115
return true;
112-
} catch {
113-
return false;
116+
} catch (error) {
117+
const err = error as NodeJS.ErrnoException;
118+
if (err.code === "ENOENT") {
119+
// File was removed between the existence check and unlink
120+
return false;
121+
}
122+
throw error;
114123
}
115124
}
116125

@@ -129,24 +138,28 @@ export function promptForToken(prompt: string): Promise<string> {
129138
let token = "";
130139

131140
const onData = (char: string) => {
132-
const c = char.toString();
133-
134-
if (c === "\n" || c === "\r") {
135-
cleanup();
136-
process.stdout.write("\n");
137-
resolve(token);
138-
} else if (c === "\u0003") {
139-
cleanup();
140-
process.stdout.write("\n");
141-
process.exit(1);
142-
} else if (c === "\u007f" || c === "\b") {
143-
if (token.length > 0) {
144-
token = token.slice(0, -1);
145-
process.stdout.write("\b \b");
141+
const chunk = char.toString();
142+
143+
for (const c of chunk) {
144+
if (c === "\n" || c === "\r") {
145+
cleanup();
146+
process.stdout.write("\n");
147+
resolve(token);
148+
return;
149+
} else if (c === "\u0003") {
150+
cleanup();
151+
process.stdout.write("\n");
152+
process.exit(1);
153+
return;
154+
} else if (c === "\u007f" || c === "\b") {
155+
if (token.length > 0) {
156+
token = token.slice(0, -1);
157+
process.stdout.write("\b \b");
158+
}
159+
} else if (c >= " ") {
160+
token += c;
161+
process.stdout.write("*");
146162
}
147-
} else if (c >= " ") {
148-
token += c;
149-
process.stdout.write("*");
150163
}
151164
};
152165

0 commit comments

Comments
 (0)