refactor(cursor): Improve portability of sed commands in install script and update error handling in common.ps1
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
# Common utility functions for Cursor hooks (PowerShell)
|
# Common utility functions for Cursor hooks (PowerShell)
|
||||||
# Dot-source this file in hook scripts: . "$PSScriptRoot\common.ps1"
|
# Dot-source this file in hook scripts: . "$PSScriptRoot\common.ps1"
|
||||||
|
# Note: ErrorActionPreference should be set in each script, not globally here
|
||||||
$ErrorActionPreference = "SilentlyContinue"
|
|
||||||
|
|
||||||
# Get worker port from settings with validation
|
# Get worker port from settings with validation
|
||||||
function Get-WorkerPort {
|
function Get-WorkerPort {
|
||||||
|
|||||||
+11
-6
@@ -56,18 +56,23 @@ echo "Copying hooks.json..."
|
|||||||
cp "$SCRIPT_DIR/hooks.json" "$TARGET_DIR/hooks.json"
|
cp "$SCRIPT_DIR/hooks.json" "$TARGET_DIR/hooks.json"
|
||||||
|
|
||||||
# Update paths in hooks.json if needed
|
# Update paths in hooks.json if needed
|
||||||
|
# Use portable sed approach that works on both BSD (macOS) and GNU (Linux) sed
|
||||||
if [ "$INSTALL_TYPE" = "project" ]; then
|
if [ "$INSTALL_TYPE" = "project" ]; then
|
||||||
# For project-level, paths should be relative
|
# For project-level, paths should be relative
|
||||||
sed -i.bak 's|\./cursor-hooks/|\./\.cursor/hooks/|g' "$TARGET_DIR/hooks.json"
|
# Create temp file, modify, then move (portable across sed variants)
|
||||||
rm -f "$TARGET_DIR/hooks.json.bak"
|
tmp_file=$(mktemp)
|
||||||
|
sed 's|\./cursor-hooks/|\./\.cursor/hooks/|g' "$TARGET_DIR/hooks.json" > "$tmp_file"
|
||||||
|
mv "$tmp_file" "$TARGET_DIR/hooks.json"
|
||||||
elif [ "$INSTALL_TYPE" = "user" ]; then
|
elif [ "$INSTALL_TYPE" = "user" ]; then
|
||||||
# For user-level, use absolute paths
|
# For user-level, use absolute paths
|
||||||
sed -i.bak "s|\./cursor-hooks/|${HOOKS_DIR}/|g" "$TARGET_DIR/hooks.json"
|
tmp_file=$(mktemp)
|
||||||
rm -f "$TARGET_DIR/hooks.json.bak"
|
sed "s|\./cursor-hooks/|${HOOKS_DIR}/|g" "$TARGET_DIR/hooks.json" > "$tmp_file"
|
||||||
|
mv "$tmp_file" "$TARGET_DIR/hooks.json"
|
||||||
elif [ "$INSTALL_TYPE" = "enterprise" ]; then
|
elif [ "$INSTALL_TYPE" = "enterprise" ]; then
|
||||||
# For enterprise, use absolute paths
|
# For enterprise, use absolute paths
|
||||||
sed -i.bak "s|\./cursor-hooks/|${HOOKS_DIR}/|g" "$TARGET_DIR/hooks.json"
|
tmp_file=$(mktemp)
|
||||||
rm -f "$TARGET_DIR/hooks.json.bak"
|
sed "s|\./cursor-hooks/|${HOOKS_DIR}/|g" "$TARGET_DIR/hooks.json" > "$tmp_file"
|
||||||
|
mv "$tmp_file" "$TARGET_DIR/hooks.json"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
|
|||||||
@@ -116,26 +116,28 @@ function unregisterCursorProject(projectName: string): void {
|
|||||||
export async function updateCursorContextForProject(projectName: string, port: number): Promise<void> {
|
export async function updateCursorContextForProject(projectName: string, port: number): Promise<void> {
|
||||||
const registry = readCursorRegistry();
|
const registry = readCursorRegistry();
|
||||||
const entry = registry[projectName];
|
const entry = registry[projectName];
|
||||||
|
|
||||||
if (!entry) return; // Project doesn't have Cursor hooks installed
|
if (!entry) return; // Project doesn't have Cursor hooks installed
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Fetch fresh context from worker
|
// Fetch fresh context from worker
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`http://127.0.0.1:${port}/api/context/inject?project=${encodeURIComponent(projectName)}`
|
`http://127.0.0.1:${port}/api/context/inject?project=${encodeURIComponent(projectName)}`
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!response.ok) return;
|
if (!response.ok) return;
|
||||||
|
|
||||||
const context = await response.text();
|
const context = await response.text();
|
||||||
if (!context || !context.trim()) return;
|
if (!context || !context.trim()) return;
|
||||||
|
|
||||||
// Write to the project's Cursor rules file
|
// Write to the project's Cursor rules file
|
||||||
const rulesDir = path.join(entry.workspacePath, '.cursor', 'rules');
|
const rulesDir = path.join(entry.workspacePath, '.cursor', 'rules');
|
||||||
const rulesFile = path.join(rulesDir, 'claude-mem-context.mdc');
|
const rulesFile = path.join(rulesDir, 'claude-mem-context.mdc');
|
||||||
|
|
||||||
mkdirSync(rulesDir, { recursive: true });
|
mkdirSync(rulesDir, { recursive: true });
|
||||||
|
|
||||||
|
// Write to temp file first, then atomically move (prevents corruption)
|
||||||
|
const tempFile = `${rulesFile}.tmp`;
|
||||||
const content = `---
|
const content = `---
|
||||||
alwaysApply: true
|
alwaysApply: true
|
||||||
description: "Claude-mem context from past sessions (auto-updated)"
|
description: "Claude-mem context from past sessions (auto-updated)"
|
||||||
@@ -150,8 +152,9 @@ ${context}
|
|||||||
---
|
---
|
||||||
*Updated after last session. Use claude-mem's MCP search tools for more detailed queries.*
|
*Updated after last session. Use claude-mem's MCP search tools for more detailed queries.*
|
||||||
`;
|
`;
|
||||||
|
|
||||||
writeFileSync(rulesFile, content);
|
writeFileSync(tempFile, content);
|
||||||
|
fs.renameSync(tempFile, rulesFile);
|
||||||
logger.debug('CURSOR', 'Updated context file', { projectName, rulesFile });
|
logger.debug('CURSOR', 'Updated context file', { projectName, rulesFile });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.warn('CURSOR', 'Failed to update context file', { projectName, error: (error as Error).message });
|
logger.warn('CURSOR', 'Failed to update context file', { projectName, error: (error as Error).message });
|
||||||
|
|||||||
Reference in New Issue
Block a user