Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async function runInstallCommand(options: { force?: boolean; silent?: boolean })
}

// Install
const result = await install({ silent: options.silent });
const result = await install({ silent: options.silent, force: options.force });

if (!result.success) {
logger.blank();
Expand Down
18 changes: 16 additions & 2 deletions src/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import fs from 'fs/promises';
import { Logger } from './utils/logger.js';
import { getConfig, toGitPath } from './utils/paths.js';
import { createHookInstallPlan } from './utils/hook.js';
import { getConfig, toGitPath, pathExists } from './utils/paths.js';
import { createHookInstallPlan, isNococliHook } from './utils/hook.js';
import { getTemplateDir, setTemplateDir } from './utils/git.js';
import { detectPowerShellRuntime } from './utils/runtime.js';
import type { InstallOptions } from './types.js';
Expand Down Expand Up @@ -44,6 +44,20 @@ export async function install(options: InstallOptions = {}): Promise<InstallResu
powerShellCommand: powerShellRuntime ?? undefined,
});

// Backup existing hooks that are not from nococli
if (!options.force) {
for (const file of installPlan.files) {
if (await pathExists(file.path)) {
const existingContent = await fs.readFile(file.path, 'utf8');
if (!isNococliHook(existingContent)) {
const backupPath = `${file.path}.bak`;
await fs.copyFile(file.path, backupPath);
logger.warning(`Existing hook backed up to ${backupPath}`);
}
}
}
}

for (const file of installPlan.files) {
await fs.writeFile(file.path, file.content, {
mode: file.mode,
Expand Down
11 changes: 10 additions & 1 deletion src/uninstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import fs from 'fs/promises';
import { Logger } from './utils/logger.js';
import { getConfig } from './utils/paths.js';
import { getConfig, pathExists } from './utils/paths.js';
import { unsetGitConfig } from './utils/git.js';
import type { UninstallOptions } from './types.js';
import type { HookMode } from './types.js';
Expand Down Expand Up @@ -39,6 +39,15 @@ export async function uninstall(options: UninstallOptions = {}): Promise<Uninsta
logger.info('PowerShell hook file not found (already removed?)');
}

// Restore backups if they exist
for (const hookPath of [config.hookFile, config.powerShellHookFile]) {
const backupPath = `${hookPath}.bak`;
if (await pathExists(backupPath)) {
await fs.rename(backupPath, hookPath);
logger.success(`Restored previous hook from ${backupPath}`);
}
}

try {
const hooksExists = await fs
.access(config.hooksDir)
Expand Down
13 changes: 13 additions & 0 deletions src/utils/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,19 @@ export function createHookInstallPlan(options: HookInstallPlanOptions): HookInst
};
}

/**
* Markers used to identify a nococli-generated hook file.
* Covers both Node.js (//) and PowerShell (#) comment styles.
*/
export const NOCO_HOOK_MARKERS = ['Generated by noco', 'nococli:'] as const;

/**
* Check if hook content was generated by nococli.
*/
export function isNococliHook(content: string): boolean {
return NOCO_HOOK_MARKERS.some((marker) => content.includes(marker));
}

/**
* Get list of default AI patterns
*/
Expand Down
Loading