-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspace.js
More file actions
72 lines (63 loc) · 2.17 KB
/
workspace.js
File metadata and controls
72 lines (63 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// =============================================================================
// workspace.js - Workspace path management for GitDock (open source)
// =============================================================================
// User data directory is chosen on first run (packaged) or uses project dir (dev).
// Stored in ~/.gitdock/workspace.json
// =============================================================================
const fs = require("fs");
const path = require("path");
const os = require("os");
const GITDOCK_DIR = path.join(os.homedir(), ".gitdock");
const WORKSPACE_PATH = path.join(GITDOCK_DIR, "workspace.json");
function ensureGitDockDir() {
if (!fs.existsSync(GITDOCK_DIR)) {
fs.mkdirSync(GITDOCK_DIR, { recursive: true });
}
}
function getDefaultWorkspacePath() {
return path.join(os.homedir(), "GitDock");
}
function loadWorkspace() {
try {
if (fs.existsSync(WORKSPACE_PATH)) {
const raw = fs.readFileSync(WORKSPACE_PATH, "utf8");
const data = JSON.parse(raw);
if (data && typeof data.path === "string" && data.path.trim()) {
return data.path.trim();
}
}
} catch (e) {
console.warn("[workspace] Could not load workspace:", e.message);
}
return null;
}
function saveWorkspace(dirPath) {
ensureGitDockDir();
const resolved = path.resolve(dirPath.trim());
if (!fs.existsSync(resolved)) {
fs.mkdirSync(resolved, { recursive: true });
}
const data = { path: resolved, createdAt: new Date().toISOString() };
fs.writeFileSync(WORKSPACE_PATH, JSON.stringify(data, null, 2), "utf8");
console.log("[workspace] Workspace set to: " + resolved);
const configPath = path.join(resolved, "config.json");
if (!fs.existsSync(configPath)) {
const emptyConfig = { accounts: {} };
fs.writeFileSync(configPath, JSON.stringify(emptyConfig, null, 2), "utf8");
console.log("[workspace] Created empty config.json");
}
return resolved;
}
function isWorkspaceConfigured() {
const ws = loadWorkspace();
return ws !== null && fs.existsSync(ws);
}
module.exports = {
GITDOCK_DIR,
WORKSPACE_PATH,
ensureGitDockDir,
getDefaultWorkspacePath,
loadWorkspace,
saveWorkspace,
isWorkspaceConfigured,
};