@zhuoyuezs/ml-platform 0.2.0 → 0.2.2-alpha.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.
@@ -1,56 +1,63 @@
1
1
  "use strict";
2
-
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const fs = require("fs");
4
4
  const os = require("os");
5
5
  const path = require("path");
6
-
7
6
  function expandHome(value) {
8
- if (value === "~") return os.homedir();
9
- if (value.startsWith(`~${path.sep}`)) return path.join(os.homedir(), value.slice(2));
10
- return value;
7
+ if (value === "~")
8
+ return os.homedir();
9
+ if (value.startsWith(`~${path.sep}`))
10
+ return path.join(os.homedir(), value.slice(2));
11
+ return value;
11
12
  }
12
-
13
13
  function configPath() {
14
- for (const variable of ["ML_PLATFORM_CONFIG_PATH", "DATA_PLATFORM_DEMO_CONFIG_PATH"]) {
15
- if (process.env[variable]?.trim()) return path.resolve(expandHome(process.env[variable]));
16
- }
17
- const base = process.env.XDG_CONFIG_HOME
18
- ? path.resolve(process.env.XDG_CONFIG_HOME)
19
- : path.join(os.homedir(), ".config");
20
- const primary = path.join(base, "ml-platform", "config.json");
21
- const legacy = path.join(base, "data-platform-demo", "config.json");
22
- return !fs.existsSync(primary) && fs.existsSync(legacy) ? legacy : primary;
14
+ for (const variable of ["ML_PLATFORM_CONFIG_PATH", "DATA_PLATFORM_DEMO_CONFIG_PATH"]) {
15
+ if (process.env[variable]?.trim())
16
+ return path.resolve(expandHome(process.env[variable]));
17
+ }
18
+ const base = process.env.XDG_CONFIG_HOME
19
+ ? path.resolve(process.env.XDG_CONFIG_HOME)
20
+ : path.join(os.homedir(), ".config");
21
+ const primary = path.join(base, "ml-platform", "config.json");
22
+ const legacy = path.join(base, "data-platform-demo", "config.json");
23
+ return !fs.existsSync(primary) && fs.existsSync(legacy) ? legacy : primary;
23
24
  }
24
-
25
25
  function normalizeApiUrl(value) {
26
- const normalized = String(value).trim().replace(/\/+$/, "");
27
- let parsed;
28
- try { parsed = new URL(normalized); } catch (_) { throw new Error("api_url must be an absolute http:// or https:// URL"); }
29
- if (!["http:", "https:"].includes(parsed.protocol) || !parsed.host) {
30
- throw new Error("api_url must be an absolute http:// or https:// URL");
31
- }
32
- return normalized;
26
+ const normalized = String(value).trim().replace(/\/+$/, "");
27
+ let parsed;
28
+ try {
29
+ parsed = new URL(normalized);
30
+ }
31
+ catch (_) {
32
+ throw new Error("api_url must be an absolute http:// or https:// URL");
33
+ }
34
+ if (!["http:", "https:"].includes(parsed.protocol) || !parsed.host) {
35
+ throw new Error("api_url must be an absolute http:// or https:// URL");
36
+ }
37
+ return normalized;
33
38
  }
34
-
35
39
  function loadApiUrl() {
36
- const file = configPath();
37
- if (!fs.existsSync(file)) return null;
38
- let payload;
39
- try { payload = JSON.parse(fs.readFileSync(file, "utf8")); }
40
- catch (error) { throw new Error(`cannot read CLI config ${file}: ${error.message}`); }
41
- if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
42
- throw new Error(`CLI config must contain a JSON object: ${file}`);
43
- }
44
- return payload.api_url ? normalizeApiUrl(payload.api_url) : null;
40
+ const file = configPath();
41
+ if (!fs.existsSync(file))
42
+ return null;
43
+ let payload;
44
+ try {
45
+ payload = JSON.parse(fs.readFileSync(file, "utf8"));
46
+ }
47
+ catch (error) {
48
+ throw new Error(`cannot read CLI config ${file}: ${error.message}`);
49
+ }
50
+ if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
51
+ throw new Error(`CLI config must contain a JSON object: ${file}`);
52
+ }
53
+ return payload.api_url ? normalizeApiUrl(payload.api_url) : null;
45
54
  }
46
-
47
55
  function saveApiUrl(value) {
48
- const file = configPath();
49
- fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
50
- const temporary = `${file}.${process.pid}.tmp`;
51
- fs.writeFileSync(temporary, `${JSON.stringify({ profile: "server", api_url: normalizeApiUrl(value) }, null, 2)}\n`, { mode: 0o600 });
52
- fs.renameSync(temporary, file);
53
- return file;
56
+ const file = configPath();
57
+ fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
58
+ const temporary = `${file}.${process.pid}.tmp`;
59
+ fs.writeFileSync(temporary, `${JSON.stringify({ profile: "server", api_url: normalizeApiUrl(value) }, null, 2)}\n`, { mode: 0o600 });
60
+ fs.renameSync(temporary, file);
61
+ return file;
54
62
  }
55
-
56
63
  module.exports = { configPath, expandHome, loadApiUrl, normalizeApiUrl, saveApiUrl };