-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths.js
More file actions
79 lines (66 loc) · 2.51 KB
/
paths.js
File metadata and controls
79 lines (66 loc) · 2.51 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
73
74
75
76
77
78
79
import { accessSync, constants, existsSync, mkdirSync } from 'fs';
import { homedir } from 'os';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
function firstNonEmpty(value) {
if (typeof value !== 'string') return '';
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : '';
}
function ensureWritableDir(dirPath) {
try {
mkdirSync(dirPath, { recursive: true });
accessSync(dirPath, constants.W_OK);
return true;
} catch {
return false;
}
}
function isNodeModulesInstall(moduleDir) {
return /[\\/]node_modules[\\/](?:@[^\\/]+[\\/])?openclaw-scheduler(?:[\\/]|$)/.test(moduleDir);
}
export function resolveSchedulerHome(env = process.env) {
const explicitHome = firstNonEmpty(env.SCHEDULER_HOME);
if (explicitHome) return explicitHome;
const home = firstNonEmpty(env.HOME) || homedir();
return join(home, '.openclaw', 'scheduler');
}
export function resolveSchedulerDbPath(params = {}) {
const env = params.env || process.env;
const explicitPath = firstNonEmpty(params.explicitPath);
if (explicitPath) return explicitPath;
const envDbPath = firstNonEmpty(env.SCHEDULER_DB);
if (envDbPath) return envDbPath;
const moduleDir = firstNonEmpty(params.moduleDir) || __dirname;
const moduleDbPath = join(moduleDir, 'scheduler.db');
const moduleDirWritable = !isNodeModulesInstall(moduleDir) && ensureWritableDir(moduleDir);
if (!isNodeModulesInstall(moduleDir) && (existsSync(moduleDbPath) || moduleDirWritable)) {
return moduleDbPath;
}
return join(resolveSchedulerHome(env), 'scheduler.db');
}
export function ensureSchedulerDbParent(dbPath) {
const parent = dirname(dbPath);
mkdirSync(parent, { recursive: true });
return parent;
}
export function resolveBackupStagingDir(env = process.env) {
const explicit = firstNonEmpty(env.SCHEDULER_BACKUP_STAGING_DIR);
if (explicit) return explicit;
return join(resolveSchedulerHome(env), '.backup-staging');
}
export function resolveArtifactsDir(params = {}) {
const env = params.env || process.env;
const explicit = firstNonEmpty(params.explicitPath) || firstNonEmpty(env.SCHEDULER_ARTIFACTS_DIR);
if (explicit) return explicit;
const dbPath = firstNonEmpty(params.dbPath);
if (dbPath && dbPath !== ':memory:') {
return join(dirname(dbPath), 'artifacts');
}
return join(resolveSchedulerHome(env), 'artifacts');
}
export function ensureArtifactsDir(dirPath) {
mkdirSync(dirPath, { recursive: true });
return dirPath;
}