@brunyee-studio/onus-cli 2.28.0

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.
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/config.ts
4
+ import fs from "fs";
5
+ import path from "path";
6
+ import os from "os";
7
+ var DEFAULT_API_URL = "https://onus.brunyeestudio.com/v1";
8
+ function getConfigDirectory() {
9
+ if (process.platform === "win32") {
10
+ return path.join(process.env.APPDATA || path.join(os.homedir(), "AppData", "Roaming"), "onus");
11
+ }
12
+ const configHome = process.env.XDG_CONFIG_HOME || path.join(os.homedir(), ".config");
13
+ return path.join(configHome, "onus");
14
+ }
15
+ function getConfigFilePath() {
16
+ return path.join(getConfigDirectory(), "config.json");
17
+ }
18
+ function loadConfig() {
19
+ let fileConfig = {};
20
+ const filePath = getConfigFilePath();
21
+ try {
22
+ if (fs.existsSync(filePath)) {
23
+ const data = fs.readFileSync(filePath, "utf8");
24
+ fileConfig = JSON.parse(data);
25
+ }
26
+ } catch {
27
+ }
28
+ const envToken = process.env.ONUS_TOKEN || process.env.ONUS_API_KEY;
29
+ const envRefreshToken = process.env.ONUS_REFRESH_TOKEN;
30
+ const envUrl = process.env.ONUS_API_URL || process.env.ONUS_BASE_URL;
31
+ return {
32
+ token: envToken || fileConfig.token,
33
+ refreshToken: envRefreshToken || fileConfig.refreshToken,
34
+ apiUrl: envUrl || fileConfig.apiUrl || DEFAULT_API_URL
35
+ };
36
+ }
37
+ function saveConfig(updates) {
38
+ let fileConfig = {};
39
+ const filePath = getConfigFilePath();
40
+ try {
41
+ if (fs.existsSync(filePath)) {
42
+ fileConfig = JSON.parse(fs.readFileSync(filePath, "utf8"));
43
+ }
44
+ } catch {
45
+ }
46
+ const next = {
47
+ apiUrl: DEFAULT_API_URL,
48
+ ...fileConfig,
49
+ ...updates
50
+ };
51
+ const dir = getConfigDirectory();
52
+ if (!fs.existsSync(dir)) {
53
+ fs.mkdirSync(dir, { recursive: true, mode: 448 });
54
+ }
55
+ fs.writeFileSync(filePath, JSON.stringify(next, null, 2) + "\n", {
56
+ mode: 384
57
+ });
58
+ try {
59
+ fs.chmodSync(filePath, 384);
60
+ } catch {
61
+ }
62
+ return next;
63
+ }
64
+ function clearConfig() {
65
+ const filePath = getConfigFilePath();
66
+ if (fs.existsSync(filePath)) {
67
+ fs.unlinkSync(filePath);
68
+ }
69
+ }
70
+
71
+ export {
72
+ getConfigDirectory,
73
+ getConfigFilePath,
74
+ loadConfig,
75
+ saveConfig,
76
+ clearConfig
77
+ };