cc-safe-setup 10.2.0 → 10.3.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.
- package/index.mjs +91 -0
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -97,6 +97,8 @@ const MIGRATE_FROM_IDX = process.argv.findIndex(a => a === '--migrate-from');
|
|
|
97
97
|
const MIGRATE_FROM = MIGRATE_FROM_IDX !== -1 ? process.argv[MIGRATE_FROM_IDX + 1] : null;
|
|
98
98
|
const HEALTH = process.argv.includes('--health');
|
|
99
99
|
const FROM_CLAUDEMD = process.argv.includes('--from-claudemd');
|
|
100
|
+
const GUARD_IDX = process.argv.findIndex(a => a === '--guard');
|
|
101
|
+
const GUARD_DESC = GUARD_IDX !== -1 ? process.argv.slice(GUARD_IDX + 1).join(' ') : null;
|
|
100
102
|
const DIFF_HOOKS_IDX = process.argv.findIndex(a => a === '--diff-hooks');
|
|
101
103
|
const DIFF_HOOKS = DIFF_HOOKS_IDX !== -1 ? process.argv[DIFF_HOOKS_IDX + 1] : null;
|
|
102
104
|
const PROFILE_IDX = process.argv.findIndex(a => a === '--profile');
|
|
@@ -136,6 +138,7 @@ if (HELP) {
|
|
|
136
138
|
npx cc-safe-setup --doctor Diagnose why hooks aren't working
|
|
137
139
|
npx cc-safe-setup --watch Live dashboard of blocked commands
|
|
138
140
|
npx cc-safe-setup --create "<desc>" Generate a custom hook from description
|
|
141
|
+
npx cc-safe-setup --guard "<rule>" Instantly enforce a rule (generate + install + activate)
|
|
139
142
|
npx cc-safe-setup --diff-hooks <path> Compare hooks between two settings files
|
|
140
143
|
npx cc-safe-setup --from-claudemd Convert CLAUDE.md rules into hooks
|
|
141
144
|
npx cc-safe-setup --health Hook health dashboard (size, permissions, age)
|
|
@@ -850,6 +853,93 @@ async function fullSetup() {
|
|
|
850
853
|
console.log();
|
|
851
854
|
}
|
|
852
855
|
|
|
856
|
+
async function guard(description) {
|
|
857
|
+
if (!description) {
|
|
858
|
+
console.log();
|
|
859
|
+
console.log(c.bold + ' cc-safe-setup --guard "<rule>"' + c.reset);
|
|
860
|
+
console.log(c.dim + ' Instantly enforce a rule — generates, installs, and activates a hook.' + c.reset);
|
|
861
|
+
console.log();
|
|
862
|
+
console.log(' Examples:');
|
|
863
|
+
console.log(c.dim + ' npx cc-safe-setup --guard "never touch the database"' + c.reset);
|
|
864
|
+
console.log(c.dim + ' npx cc-safe-setup --guard "block all sudo commands"' + c.reset);
|
|
865
|
+
console.log(c.dim + ' npx cc-safe-setup --guard "no force push"' + c.reset);
|
|
866
|
+
console.log(c.dim + ' npx cc-safe-setup --guard "warn before deleting files"' + c.reset);
|
|
867
|
+
console.log();
|
|
868
|
+
return;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
console.log();
|
|
872
|
+
console.log(c.bold + ` 🛡️ Guard: "${description}"` + c.reset);
|
|
873
|
+
console.log();
|
|
874
|
+
|
|
875
|
+
const desc = description.toLowerCase();
|
|
876
|
+
let hookName, hookScript, trigger = 'PreToolUse', matcher = 'Bash';
|
|
877
|
+
|
|
878
|
+
// Map natural language to hook patterns
|
|
879
|
+
if (desc.match(/database|drop|migrate|prisma|sql/)) {
|
|
880
|
+
hookName = 'guard-database';
|
|
881
|
+
hookScript = `#!/bin/bash\nCOMMAND=$(cat | jq -r '.tool_input.command // empty' 2>/dev/null)\n[ -z "$COMMAND" ] && exit 0\nif echo "$COMMAND" | grep -qiE '(DROP\\s+(DATABASE|TABLE)|migrate:fresh|prisma\\s+reset|db:drop|TRUNCATE)'; then\n echo "BLOCKED: Database operation blocked by guard rule." >&2\n echo "Rule: ${description}" >&2\n exit 2\nfi\nexit 0`;
|
|
882
|
+
} else if (desc.match(/sudo|root|admin/)) {
|
|
883
|
+
hookName = 'guard-sudo';
|
|
884
|
+
hookScript = `#!/bin/bash\nCOMMAND=$(cat | jq -r '.tool_input.command // empty' 2>/dev/null)\n[ -z "$COMMAND" ] && exit 0\nif echo "$COMMAND" | grep -qE '^\\s*sudo\\b'; then\n echo "BLOCKED: sudo blocked by guard rule." >&2\n echo "Rule: ${description}" >&2\n exit 2\nfi\nexit 0`;
|
|
885
|
+
} else if (desc.match(/force.?push|push.*force/)) {
|
|
886
|
+
hookName = 'guard-force-push';
|
|
887
|
+
hookScript = `#!/bin/bash\nCOMMAND=$(cat | jq -r '.tool_input.command // empty' 2>/dev/null)\n[ -z "$COMMAND" ] && exit 0\nif echo "$COMMAND" | grep -qE 'git\\s+push\\s+.*--force'; then\n echo "BLOCKED: Force push blocked by guard rule." >&2\n echo "Rule: ${description}" >&2\n exit 2\nfi\nexit 0`;
|
|
888
|
+
} else if (desc.match(/push.*main|main.*push/)) {
|
|
889
|
+
hookName = 'guard-push-main';
|
|
890
|
+
hookScript = `#!/bin/bash\nCOMMAND=$(cat | jq -r '.tool_input.command // empty' 2>/dev/null)\n[ -z "$COMMAND" ] && exit 0\nif echo "$COMMAND" | grep -qE 'git\\s+push\\s+.*\\b(main|master)\\b'; then\n echo "BLOCKED: Push to main blocked by guard rule." >&2\n echo "Rule: ${description}" >&2\n exit 2\nfi\nexit 0`;
|
|
891
|
+
} else if (desc.match(/delet|rm|remov/)) {
|
|
892
|
+
hookName = 'guard-delete';
|
|
893
|
+
hookScript = `#!/bin/bash\nCOMMAND=$(cat | jq -r '.tool_input.command // empty' 2>/dev/null)\n[ -z "$COMMAND" ] && exit 0\nif echo "$COMMAND" | grep -qE 'rm\\s+.*-rf'; then\n echo "WARNING: Deletion detected." >&2\n echo "Rule: ${description}" >&2\nfi\nexit 0`;
|
|
894
|
+
} else if (desc.match(/deploy|ship|release|publish/)) {
|
|
895
|
+
hookName = 'guard-deploy';
|
|
896
|
+
hookScript = `#!/bin/bash\nCOMMAND=$(cat | jq -r '.tool_input.command // empty' 2>/dev/null)\n[ -z "$COMMAND" ] && exit 0\nif echo "$COMMAND" | grep -qiE '(deploy|publish|release|vercel|netlify)'; then\n echo "WARNING: Deploy/publish command detected." >&2\n echo "Rule: ${description}" >&2\nfi\nexit 0`;
|
|
897
|
+
} else if (desc.match(/test|spec/)) {
|
|
898
|
+
hookName = 'guard-test-required';
|
|
899
|
+
hookScript = `#!/bin/bash\nCOMMAND=$(cat | jq -r '.tool_input.command // empty' 2>/dev/null)\n[ -z "$COMMAND" ] && exit 0\nif echo "$COMMAND" | grep -qE 'git\\s+commit'; then\n echo "WARNING: Commit detected." >&2\n echo "Rule: ${description}" >&2\nfi\nexit 0`;
|
|
900
|
+
} else {
|
|
901
|
+
// Generic guard — extract keyword and block commands containing it
|
|
902
|
+
const keyword = desc.replace(/[^a-z0-9 ]/g, '').split(' ').filter(w => w.length > 3).pop() || 'guard';
|
|
903
|
+
hookName = `guard-${keyword}`;
|
|
904
|
+
hookScript = `#!/bin/bash\nCOMMAND=$(cat | jq -r '.tool_input.command // empty' 2>/dev/null)\n[ -z "$COMMAND" ] && exit 0\nif echo "$COMMAND" | grep -qiE '${keyword}'; then\n echo "WARNING: Command matches guard rule." >&2\n echo "Rule: ${description}" >&2\nfi\nexit 0`;
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
// Write hook
|
|
908
|
+
mkdirSync(HOOKS_DIR, { recursive: true });
|
|
909
|
+
const hookPath = join(HOOKS_DIR, `${hookName}.sh`);
|
|
910
|
+
writeFileSync(hookPath, hookScript);
|
|
911
|
+
chmodSync(hookPath, 0o755);
|
|
912
|
+
console.log(c.green + ' ✓' + c.reset + ` Hook created: ${hookPath}`);
|
|
913
|
+
|
|
914
|
+
// Register in settings.json
|
|
915
|
+
let settings = {};
|
|
916
|
+
if (existsSync(SETTINGS_PATH)) {
|
|
917
|
+
try { settings = JSON.parse(readFileSync(SETTINGS_PATH, 'utf-8')); } catch {}
|
|
918
|
+
}
|
|
919
|
+
if (!settings.hooks) settings.hooks = {};
|
|
920
|
+
if (!settings.hooks[trigger]) settings.hooks[trigger] = [];
|
|
921
|
+
|
|
922
|
+
const cmd = `bash ${hookPath}`;
|
|
923
|
+
const alreadyExists = JSON.stringify(settings.hooks).includes(hookName);
|
|
924
|
+
if (!alreadyExists) {
|
|
925
|
+
const existing = settings.hooks[trigger].find(e => e.matcher === matcher);
|
|
926
|
+
if (existing) {
|
|
927
|
+
existing.hooks.push({ type: 'command', command: cmd });
|
|
928
|
+
} else {
|
|
929
|
+
settings.hooks[trigger].push({ matcher, hooks: [{ type: 'command', command: cmd }] });
|
|
930
|
+
}
|
|
931
|
+
writeFileSync(SETTINGS_PATH, JSON.stringify(settings, null, 2));
|
|
932
|
+
console.log(c.green + ' ✓' + c.reset + ' Registered in settings.json');
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
console.log(c.green + ' ✓' + c.reset + ' Guard active immediately');
|
|
936
|
+
console.log();
|
|
937
|
+
console.log(c.dim + ` Rule: "${description}"` + c.reset);
|
|
938
|
+
console.log(c.dim + ` Hook: ${hookName}.sh` + c.reset);
|
|
939
|
+
console.log(c.dim + ' Remove: npx cc-safe-setup --uninstall' + c.reset);
|
|
940
|
+
console.log();
|
|
941
|
+
}
|
|
942
|
+
|
|
853
943
|
async function diffHooks(otherPath) {
|
|
854
944
|
console.log();
|
|
855
945
|
console.log(c.bold + ' cc-safe-setup --diff-hooks' + c.reset);
|
|
@@ -3815,6 +3905,7 @@ async function main() {
|
|
|
3815
3905
|
if (FULL) return fullSetup();
|
|
3816
3906
|
if (DOCTOR) return doctor();
|
|
3817
3907
|
if (WATCH) return watch();
|
|
3908
|
+
if (GUARD_IDX !== -1) return guard(GUARD_DESC);
|
|
3818
3909
|
if (DIFF_HOOKS_IDX !== -1) return diffHooks(DIFF_HOOKS);
|
|
3819
3910
|
if (FROM_CLAUDEMD) return fromClaudeMd();
|
|
3820
3911
|
if (HEALTH) return health();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cc-safe-setup",
|
|
3
|
-
"version": "10.
|
|
3
|
+
"version": "10.3.0",
|
|
4
4
|
"description": "One command to make Claude Code safe. 59 hooks (8 built-in + 51 examples). 26 CLI commands: dashboard, create, audit, lint, diff, migrate, compare, generate-ci. 284 tests.",
|
|
5
5
|
"main": "index.mjs",
|
|
6
6
|
"bin": {
|