Skip to content
This repository was archived by the owner on Apr 28, 2026. It is now read-only.

Commit aa71d52

Browse files
committed
fixed teste
1 parent 988dedf commit aa71d52

2 files changed

Lines changed: 29 additions & 16 deletions

File tree

src/utils/config.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ import fs from 'fs/promises';
22
import path from 'path';
33
import os from 'os';
44

5-
const CONFIG_DIR = path.join(os.homedir(), '.kodus');
6-
const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
5+
function getConfigDir(): string {
6+
return path.join(os.homedir(), '.kodus');
7+
}
8+
9+
function getConfigFile(): string {
10+
return path.join(getConfigDir(), 'config.json');
11+
}
712

813
export interface CliConfig {
914
teamKey: string;
@@ -20,7 +25,7 @@ function isJsonParseError(error: unknown): boolean {
2025

2126
async function ensureConfigDir(): Promise<void> {
2227
try {
23-
await fs.mkdir(CONFIG_DIR, { recursive: true, mode: 0o700 });
28+
await fs.mkdir(getConfigDir(), { recursive: true, mode: 0o700 });
2429
} catch (error) {
2530
if ((error as NodeJS.ErrnoException).code !== 'EEXIST') {
2631
throw error;
@@ -30,16 +35,18 @@ async function ensureConfigDir(): Promise<void> {
3035

3136
export async function saveConfig(config: CliConfig): Promise<void> {
3237
await ensureConfigDir();
33-
const tmpFile = `${CONFIG_FILE}.${process.pid}.${Date.now()}.tmp`;
38+
const configFile = getConfigFile();
39+
const tmpFile = `${configFile}.${process.pid}.${Date.now()}.tmp`;
3440
const content = JSON.stringify(config, null, 2);
3541

3642
await fs.writeFile(tmpFile, content, { encoding: 'utf-8', mode: 0o600 });
37-
await fs.rename(tmpFile, CONFIG_FILE);
43+
await fs.rename(tmpFile, configFile);
3844
}
3945

4046
export async function loadConfig(): Promise<CliConfig | null> {
4147
try {
42-
const content = await fs.readFile(CONFIG_FILE, 'utf-8');
48+
const configFile = getConfigFile();
49+
const content = await fs.readFile(configFile, 'utf-8');
4350
return JSON.parse(content) as CliConfig;
4451
} catch (error) {
4552
const code = (error as NodeJS.ErrnoException).code;
@@ -49,8 +56,9 @@ export async function loadConfig(): Promise<CliConfig | null> {
4956

5057
// Self-heal malformed JSON by isolating the broken file and treating as no config.
5158
if (isJsonParseError(error)) {
52-
const brokenFile = `${CONFIG_FILE}.corrupted.${Date.now()}`;
53-
await fs.rename(CONFIG_FILE, brokenFile).catch(() => {});
59+
const configFile = getConfigFile();
60+
const brokenFile = `${configFile}.corrupted.${Date.now()}`;
61+
await fs.rename(configFile, brokenFile).catch(() => {});
5462
return null;
5563
}
5664

@@ -60,7 +68,7 @@ export async function loadConfig(): Promise<CliConfig | null> {
6068

6169
export async function clearConfig(): Promise<void> {
6270
try {
63-
await fs.unlink(CONFIG_FILE);
71+
await fs.unlink(getConfigFile());
6472
} catch (error) {
6573
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {
6674
throw error;
@@ -70,7 +78,7 @@ export async function clearConfig(): Promise<void> {
7078

7179
export async function configExists(): Promise<boolean> {
7280
try {
73-
await fs.access(CONFIG_FILE);
81+
await fs.access(getConfigFile());
7482
return true;
7583
} catch {
7684
return false;

src/utils/device.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ interface DeviceData {
1010
tokenUpdatedAt?: string;
1111
}
1212

13-
const KODUS_DIR = path.join(os.homedir(), '.kodus');
14-
const DEVICE_FILE = path.join(KODUS_DIR, 'device.json');
13+
function getKodusDir(): string {
14+
return path.join(os.homedir(), '.kodus');
15+
}
16+
17+
function getDeviceFile(): string {
18+
return path.join(getKodusDir(), 'device.json');
19+
}
1520

1621
const UUID_REGEX =
1722
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
@@ -31,7 +36,7 @@ function isValidDeviceToken(value: unknown): value is string {
3136

3237
async function ensureKodusDir(): Promise<void> {
3338
try {
34-
await fs.mkdir(KODUS_DIR, { recursive: true, mode: 0o700 });
39+
await fs.mkdir(getKodusDir(), { recursive: true, mode: 0o700 });
3540
} catch (error) {
3641
if ((error as NodeJS.ErrnoException).code !== 'EEXIST') {
3742
throw error;
@@ -41,17 +46,17 @@ async function ensureKodusDir(): Promise<void> {
4146

4247
async function writeDeviceData(data: DeviceData): Promise<void> {
4348
await ensureKodusDir();
44-
const tmpFile = `${DEVICE_FILE}.${process.pid}.${Date.now()}.tmp`;
49+
const tmpFile = `${getDeviceFile()}.${process.pid}.${Date.now()}.tmp`;
4550
await fs.writeFile(tmpFile, JSON.stringify(data, null, 2), {
4651
encoding: 'utf-8',
4752
mode: 0o600,
4853
});
49-
await fs.rename(tmpFile, DEVICE_FILE);
54+
await fs.rename(tmpFile, getDeviceFile());
5055
}
5156

5257
async function readStoredDeviceData(): Promise<DeviceData | null> {
5358
try {
54-
const content = await fs.readFile(DEVICE_FILE, 'utf-8');
59+
const content = await fs.readFile(getDeviceFile(), 'utf-8');
5560
const parsed = JSON.parse(content) as Partial<DeviceData>;
5661
if (!isValidDeviceId(parsed.deviceId)) {
5762
return null;

0 commit comments

Comments
 (0)