@techdivision/opencode-time-tracking 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/services/ConfigLoader.ts +48 -6
package/package.json
CHANGED
|
@@ -16,6 +16,42 @@ import "../types/Bun"
|
|
|
16
16
|
*/
|
|
17
17
|
const ENV_USER_EMAIL = "OPENCODE_USER_EMAIL"
|
|
18
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Loads a value from a .env file in the specified directory.
|
|
21
|
+
*
|
|
22
|
+
* @param directory - The directory containing the .env file
|
|
23
|
+
* @param key - The environment variable key to look for
|
|
24
|
+
* @returns The value if found, or `null` if not found
|
|
25
|
+
*
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
async function loadEnvValue(
|
|
29
|
+
directory: string,
|
|
30
|
+
key: string
|
|
31
|
+
): Promise<string | null> {
|
|
32
|
+
const envPath = `${directory}/.env`
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
const file = Bun.file(envPath)
|
|
36
|
+
|
|
37
|
+
if (await file.exists()) {
|
|
38
|
+
const content = await file.text()
|
|
39
|
+
// Match KEY=value, handling optional quotes
|
|
40
|
+
const regex = new RegExp(`^${key}=(.*)$`, "m")
|
|
41
|
+
const match = content.match(regex)
|
|
42
|
+
|
|
43
|
+
if (match) {
|
|
44
|
+
// Remove surrounding quotes (single or double) and trim
|
|
45
|
+
return match[1].trim().replace(/^["']|["']$/g, "")
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return null
|
|
50
|
+
} catch {
|
|
51
|
+
return null
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
19
55
|
/**
|
|
20
56
|
* Loads the plugin configuration from the project directory.
|
|
21
57
|
*
|
|
@@ -23,9 +59,10 @@ const ENV_USER_EMAIL = "OPENCODE_USER_EMAIL"
|
|
|
23
59
|
* The configuration file is expected at `.opencode/opencode-project.json`
|
|
24
60
|
* within the project directory, with a `time_tracking` section.
|
|
25
61
|
*
|
|
26
|
-
* The `user_email` is resolved from:
|
|
27
|
-
* 1. `OPENCODE_USER_EMAIL` environment variable
|
|
28
|
-
* 2.
|
|
62
|
+
* The `user_email` is resolved from (in order of priority):
|
|
63
|
+
* 1. `OPENCODE_USER_EMAIL` system environment variable
|
|
64
|
+
* 2. `OPENCODE_USER_EMAIL` from project `.env` file
|
|
65
|
+
* 3. System username (fallback)
|
|
29
66
|
*/
|
|
30
67
|
export class ConfigLoader {
|
|
31
68
|
/**
|
|
@@ -39,7 +76,7 @@ export class ConfigLoader {
|
|
|
39
76
|
* const config = await ConfigLoader.load("/path/to/project")
|
|
40
77
|
* if (config) {
|
|
41
78
|
* console.log(config.csv_file)
|
|
42
|
-
* console.log(config.user_email) // Resolved from ENV or system username
|
|
79
|
+
* console.log(config.user_email) // Resolved from ENV, .env file, or system username
|
|
43
80
|
* }
|
|
44
81
|
* ```
|
|
45
82
|
*/
|
|
@@ -55,8 +92,13 @@ export class ConfigLoader {
|
|
|
55
92
|
if (projectConfig.time_tracking) {
|
|
56
93
|
const jsonConfig = projectConfig.time_tracking
|
|
57
94
|
|
|
58
|
-
// Resolve user_email
|
|
59
|
-
|
|
95
|
+
// Resolve user_email with fallback chain:
|
|
96
|
+
// 1. System environment variable
|
|
97
|
+
// 2. Project .env file
|
|
98
|
+
// 3. System username
|
|
99
|
+
const envValue = await loadEnvValue(directory, ENV_USER_EMAIL)
|
|
100
|
+
const userEmail =
|
|
101
|
+
process.env[ENV_USER_EMAIL] || envValue || userInfo().username
|
|
60
102
|
|
|
61
103
|
return {
|
|
62
104
|
...jsonConfig,
|