Repository navigation
Expand file tree
/
Copy pathhook-flags.js
More file actions
153 lines (131 loc) · 4.23 KB
/
Copy pathhook-flags.js
File metadata and controls
153 lines (131 loc) · 4.23 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env node
/**
* Shared hook enable/disable controls.
*
* Controls:
* - ECC_HOOKS_ENABLED=true|false (default: true)
* - ECC_HOOK_PROFILE=minimal|standard|strict (default: standard)
* - ECC_DISABLED_HOOKS=comma,separated,hook,ids
*
* Claude plugin options are used when their corresponding ECC variable is
* absent. A managed install can provide ecc/setup.json as the final fallback.
*/
'use strict';
const fs = require('fs');
const path = require('path');
const VALID_PROFILES = new Set(['minimal', 'standard', 'strict']);
function normalizeId(value) {
return String(value || '').trim().toLowerCase();
}
function parseBoolean(value, fallback = true) {
if (value === undefined || value === null || String(value).trim() === '') {
return fallback;
}
const normalized = String(value).trim().toLowerCase();
if (['1', 'true', 'yes', 'on'].includes(normalized)) return true;
if (['0', 'false', 'no', 'off'].includes(normalized)) return false;
return fallback;
}
function sanitizeDiagnostic(value) {
return String(value || '')
// eslint-disable-next-line no-control-regex
.replace(/\x1b(?:\[[0-9;?]*[A-Za-z]|\][^\x07\x1b]*(?:\x07|\x1b\\)|\([A-Z]|[A-Z])/g, '')
.replace(/[^\x20-\x7E]/g, '?');
}
function readManagedHookConfig(env = process.env) {
const pluginRoot = String(
env.CLAUDE_PLUGIN_ROOT || env.ECC_PLUGIN_ROOT || ''
).trim();
const configPath = String(env.ECC_HOOK_CONFIG || '').trim()
|| (pluginRoot ? path.join(pluginRoot, 'ecc', 'setup.json') : '');
if (!configPath || !fs.existsSync(configPath)) return {};
try {
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
return config?.hooks
&& typeof config.hooks === 'object'
&& !Array.isArray(config.hooks)
? config.hooks
: {};
} catch (error) {
process.stderr.write(`${sanitizeDiagnostic(
`Warning: unable to read managed ECC hook config at ${configPath}: ${error.message}`
)}\n`);
return {};
}
}
function areHooksEnabled(env = process.env, managed = readManagedHookConfig(env)) {
const raw = env.ECC_HOOKS_ENABLED !== undefined
? env.ECC_HOOKS_ENABLED
: (
env.CLAUDE_PLUGIN_OPTION_HOOKS_ENABLED !== undefined
? env.CLAUDE_PLUGIN_OPTION_HOOKS_ENABLED
: managed.enabled
);
return parseBoolean(raw, true);
}
function getHookProfile(env = process.env, managed = readManagedHookConfig(env)) {
const selected = env.ECC_HOOK_PROFILE !== undefined
? env.ECC_HOOK_PROFILE
: (
env.CLAUDE_PLUGIN_OPTION_HOOK_PROFILE !== undefined
? env.CLAUDE_PLUGIN_OPTION_HOOK_PROFILE
: managed.profile
);
const raw = String(selected ?? 'standard').trim().toLowerCase();
return VALID_PROFILES.has(raw) ? raw : 'standard';
}
function getDisabledHookIds(env = process.env) {
const raw = String(env.ECC_DISABLED_HOOKS || '');
if (!raw.trim()) return new Set();
return new Set(
raw
.split(',')
.map(v => normalizeId(v))
.filter(Boolean)
);
}
function parseProfiles(rawProfiles, fallback = ['standard', 'strict']) {
if (!rawProfiles) return [...fallback];
if (Array.isArray(rawProfiles)) {
const parsed = rawProfiles
.map(v => String(v || '').trim().toLowerCase())
.filter(v => VALID_PROFILES.has(v));
return parsed.length > 0 ? parsed : [...fallback];
}
const parsed = String(rawProfiles)
.split(',')
.map(v => v.trim().toLowerCase())
.filter(v => VALID_PROFILES.has(v));
return parsed.length > 0 ? parsed : [...fallback];
}
function isDryRun(env = process.env) {
return env.ECC_DRY_RUN === '1';
}
function isHookEnabled(hookId, options = {}) {
const env = options.env || process.env;
const managed = readManagedHookConfig(env);
if (!areHooksEnabled(env, managed)) {
return false;
}
const id = normalizeId(hookId);
if (!id) return true;
const disabled = getDisabledHookIds(env);
if (disabled.has(id)) {
return false;
}
const profile = getHookProfile(env, managed);
const allowedProfiles = parseProfiles(options.profiles);
return allowedProfiles.includes(profile);
}
module.exports = {
VALID_PROFILES,
normalizeId,
parseBoolean,
readManagedHookConfig,
areHooksEnabled,
getHookProfile,
getDisabledHookIds,
parseProfiles,
isHookEnabled,
isDryRun,
};