mirror of
https://github.com/sweetwisdom/everything-claude-code-zh.git
synced 2026-03-22 06:20:10 +00:00
feat: cross-platform support with Node.js scripts
- Rewrite all bash hooks to Node.js for Windows/macOS/Linux compatibility - Add package manager auto-detection (npm, pnpm, yarn, bun) - Add scripts/lib/ with cross-platform utilities - Add /setup-pm command for package manager configuration - Add comprehensive test suite (62 tests) Co-authored-by: zerx-lab
This commit is contained in:
390
scripts/lib/package-manager.js
Normal file
390
scripts/lib/package-manager.js
Normal file
@@ -0,0 +1,390 @@
|
||||
/**
|
||||
* Package Manager Detection and Selection
|
||||
* Automatically detects the preferred package manager or lets user choose
|
||||
*
|
||||
* Supports: npm, pnpm, yarn, bun
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { commandExists, getClaudeDir, readFile, writeFile, log, runCommand } = require('./utils');
|
||||
|
||||
// Package manager definitions
|
||||
const PACKAGE_MANAGERS = {
|
||||
npm: {
|
||||
name: 'npm',
|
||||
lockFile: 'package-lock.json',
|
||||
installCmd: 'npm install',
|
||||
runCmd: 'npm run',
|
||||
execCmd: 'npx',
|
||||
testCmd: 'npm test',
|
||||
buildCmd: 'npm run build',
|
||||
devCmd: 'npm run dev'
|
||||
},
|
||||
pnpm: {
|
||||
name: 'pnpm',
|
||||
lockFile: 'pnpm-lock.yaml',
|
||||
installCmd: 'pnpm install',
|
||||
runCmd: 'pnpm',
|
||||
execCmd: 'pnpm dlx',
|
||||
testCmd: 'pnpm test',
|
||||
buildCmd: 'pnpm build',
|
||||
devCmd: 'pnpm dev'
|
||||
},
|
||||
yarn: {
|
||||
name: 'yarn',
|
||||
lockFile: 'yarn.lock',
|
||||
installCmd: 'yarn',
|
||||
runCmd: 'yarn',
|
||||
execCmd: 'yarn dlx',
|
||||
testCmd: 'yarn test',
|
||||
buildCmd: 'yarn build',
|
||||
devCmd: 'yarn dev'
|
||||
},
|
||||
bun: {
|
||||
name: 'bun',
|
||||
lockFile: 'bun.lockb',
|
||||
installCmd: 'bun install',
|
||||
runCmd: 'bun run',
|
||||
execCmd: 'bunx',
|
||||
testCmd: 'bun test',
|
||||
buildCmd: 'bun run build',
|
||||
devCmd: 'bun run dev'
|
||||
}
|
||||
};
|
||||
|
||||
// Priority order for detection
|
||||
const DETECTION_PRIORITY = ['pnpm', 'bun', 'yarn', 'npm'];
|
||||
|
||||
// Config file path
|
||||
function getConfigPath() {
|
||||
return path.join(getClaudeDir(), 'package-manager.json');
|
||||
}
|
||||
|
||||
/**
|
||||
* Load saved package manager configuration
|
||||
*/
|
||||
function loadConfig() {
|
||||
const configPath = getConfigPath();
|
||||
const content = readFile(configPath);
|
||||
|
||||
if (content) {
|
||||
try {
|
||||
return JSON.parse(content);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save package manager configuration
|
||||
*/
|
||||
function saveConfig(config) {
|
||||
const configPath = getConfigPath();
|
||||
writeFile(configPath, JSON.stringify(config, null, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect package manager from lock file in project directory
|
||||
*/
|
||||
function detectFromLockFile(projectDir = process.cwd()) {
|
||||
for (const pmName of DETECTION_PRIORITY) {
|
||||
const pm = PACKAGE_MANAGERS[pmName];
|
||||
const lockFilePath = path.join(projectDir, pm.lockFile);
|
||||
|
||||
if (fs.existsSync(lockFilePath)) {
|
||||
return pmName;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect package manager from package.json packageManager field
|
||||
*/
|
||||
function detectFromPackageJson(projectDir = process.cwd()) {
|
||||
const packageJsonPath = path.join(projectDir, 'package.json');
|
||||
const content = readFile(packageJsonPath);
|
||||
|
||||
if (content) {
|
||||
try {
|
||||
const pkg = JSON.parse(content);
|
||||
if (pkg.packageManager) {
|
||||
// Format: "pnpm@8.6.0" or just "pnpm"
|
||||
const pmName = pkg.packageManager.split('@')[0];
|
||||
if (PACKAGE_MANAGERS[pmName]) {
|
||||
return pmName;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Invalid package.json
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get available package managers (installed on system)
|
||||
*/
|
||||
function getAvailablePackageManagers() {
|
||||
const available = [];
|
||||
|
||||
for (const pmName of Object.keys(PACKAGE_MANAGERS)) {
|
||||
if (commandExists(pmName)) {
|
||||
available.push(pmName);
|
||||
}
|
||||
}
|
||||
|
||||
return available;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the package manager to use for current project
|
||||
*
|
||||
* Detection priority:
|
||||
* 1. Environment variable CLAUDE_PACKAGE_MANAGER
|
||||
* 2. Project-specific config (in .claude/package-manager.json)
|
||||
* 3. package.json packageManager field
|
||||
* 4. Lock file detection
|
||||
* 5. Global user preference (in ~/.claude/package-manager.json)
|
||||
* 6. First available package manager (by priority)
|
||||
*
|
||||
* @param {object} options - { projectDir, fallbackOrder }
|
||||
* @returns {object} - { name, config, source }
|
||||
*/
|
||||
function getPackageManager(options = {}) {
|
||||
const { projectDir = process.cwd(), fallbackOrder = DETECTION_PRIORITY } = options;
|
||||
|
||||
// 1. Check environment variable
|
||||
const envPm = process.env.CLAUDE_PACKAGE_MANAGER;
|
||||
if (envPm && PACKAGE_MANAGERS[envPm]) {
|
||||
return {
|
||||
name: envPm,
|
||||
config: PACKAGE_MANAGERS[envPm],
|
||||
source: 'environment'
|
||||
};
|
||||
}
|
||||
|
||||
// 2. Check project-specific config
|
||||
const projectConfigPath = path.join(projectDir, '.claude', 'package-manager.json');
|
||||
const projectConfig = readFile(projectConfigPath);
|
||||
if (projectConfig) {
|
||||
try {
|
||||
const config = JSON.parse(projectConfig);
|
||||
if (config.packageManager && PACKAGE_MANAGERS[config.packageManager]) {
|
||||
return {
|
||||
name: config.packageManager,
|
||||
config: PACKAGE_MANAGERS[config.packageManager],
|
||||
source: 'project-config'
|
||||
};
|
||||
}
|
||||
} catch {
|
||||
// Invalid config
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Check package.json packageManager field
|
||||
const fromPackageJson = detectFromPackageJson(projectDir);
|
||||
if (fromPackageJson) {
|
||||
return {
|
||||
name: fromPackageJson,
|
||||
config: PACKAGE_MANAGERS[fromPackageJson],
|
||||
source: 'package.json'
|
||||
};
|
||||
}
|
||||
|
||||
// 4. Check lock file
|
||||
const fromLockFile = detectFromLockFile(projectDir);
|
||||
if (fromLockFile) {
|
||||
return {
|
||||
name: fromLockFile,
|
||||
config: PACKAGE_MANAGERS[fromLockFile],
|
||||
source: 'lock-file'
|
||||
};
|
||||
}
|
||||
|
||||
// 5. Check global user preference
|
||||
const globalConfig = loadConfig();
|
||||
if (globalConfig && globalConfig.packageManager && PACKAGE_MANAGERS[globalConfig.packageManager]) {
|
||||
return {
|
||||
name: globalConfig.packageManager,
|
||||
config: PACKAGE_MANAGERS[globalConfig.packageManager],
|
||||
source: 'global-config'
|
||||
};
|
||||
}
|
||||
|
||||
// 6. Use first available package manager
|
||||
const available = getAvailablePackageManagers();
|
||||
for (const pmName of fallbackOrder) {
|
||||
if (available.includes(pmName)) {
|
||||
return {
|
||||
name: pmName,
|
||||
config: PACKAGE_MANAGERS[pmName],
|
||||
source: 'fallback'
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Default to npm (always available with Node.js)
|
||||
return {
|
||||
name: 'npm',
|
||||
config: PACKAGE_MANAGERS.npm,
|
||||
source: 'default'
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Set user's preferred package manager (global)
|
||||
*/
|
||||
function setPreferredPackageManager(pmName) {
|
||||
if (!PACKAGE_MANAGERS[pmName]) {
|
||||
throw new Error(`Unknown package manager: ${pmName}`);
|
||||
}
|
||||
|
||||
const config = loadConfig() || {};
|
||||
config.packageManager = pmName;
|
||||
config.setAt = new Date().toISOString();
|
||||
saveConfig(config);
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set project's preferred package manager
|
||||
*/
|
||||
function setProjectPackageManager(pmName, projectDir = process.cwd()) {
|
||||
if (!PACKAGE_MANAGERS[pmName]) {
|
||||
throw new Error(`Unknown package manager: ${pmName}`);
|
||||
}
|
||||
|
||||
const configDir = path.join(projectDir, '.claude');
|
||||
const configPath = path.join(configDir, 'package-manager.json');
|
||||
|
||||
const config = {
|
||||
packageManager: pmName,
|
||||
setAt: new Date().toISOString()
|
||||
};
|
||||
|
||||
writeFile(configPath, JSON.stringify(config, null, 2));
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the command to run a script
|
||||
* @param {string} script - Script name (e.g., "dev", "build", "test")
|
||||
* @param {object} options - { projectDir }
|
||||
*/
|
||||
function getRunCommand(script, options = {}) {
|
||||
const pm = getPackageManager(options);
|
||||
|
||||
switch (script) {
|
||||
case 'install':
|
||||
return pm.config.installCmd;
|
||||
case 'test':
|
||||
return pm.config.testCmd;
|
||||
case 'build':
|
||||
return pm.config.buildCmd;
|
||||
case 'dev':
|
||||
return pm.config.devCmd;
|
||||
default:
|
||||
return `${pm.config.runCmd} ${script}`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the command to execute a package binary
|
||||
* @param {string} binary - Binary name (e.g., "prettier", "eslint")
|
||||
* @param {string} args - Arguments to pass
|
||||
*/
|
||||
function getExecCommand(binary, args = '', options = {}) {
|
||||
const pm = getPackageManager(options);
|
||||
return `${pm.config.execCmd} ${binary}${args ? ' ' + args : ''}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interactive prompt for package manager selection
|
||||
* Returns a message for Claude to show to user
|
||||
*/
|
||||
function getSelectionPrompt() {
|
||||
const available = getAvailablePackageManagers();
|
||||
const current = getPackageManager();
|
||||
|
||||
let message = '[PackageManager] Available package managers:\n';
|
||||
|
||||
for (const pmName of available) {
|
||||
const indicator = pmName === current.name ? ' (current)' : '';
|
||||
message += ` - ${pmName}${indicator}\n`;
|
||||
}
|
||||
|
||||
message += '\nTo set your preferred package manager:\n';
|
||||
message += ' - Global: Set CLAUDE_PACKAGE_MANAGER environment variable\n';
|
||||
message += ' - Or add to ~/.claude/package-manager.json: {"packageManager": "pnpm"}\n';
|
||||
message += ' - Or add to package.json: {"packageManager": "pnpm@8"}\n';
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a regex pattern that matches commands for all package managers
|
||||
* @param {string} action - Action pattern (e.g., "run dev", "install", "test")
|
||||
*/
|
||||
function getCommandPattern(action) {
|
||||
const patterns = [];
|
||||
|
||||
if (action === 'dev') {
|
||||
patterns.push(
|
||||
'npm run dev',
|
||||
'pnpm( run)? dev',
|
||||
'yarn dev',
|
||||
'bun run dev'
|
||||
);
|
||||
} else if (action === 'install') {
|
||||
patterns.push(
|
||||
'npm install',
|
||||
'pnpm install',
|
||||
'yarn( install)?',
|
||||
'bun install'
|
||||
);
|
||||
} else if (action === 'test') {
|
||||
patterns.push(
|
||||
'npm test',
|
||||
'pnpm test',
|
||||
'yarn test',
|
||||
'bun test'
|
||||
);
|
||||
} else if (action === 'build') {
|
||||
patterns.push(
|
||||
'npm run build',
|
||||
'pnpm( run)? build',
|
||||
'yarn build',
|
||||
'bun run build'
|
||||
);
|
||||
} else {
|
||||
// Generic run command
|
||||
patterns.push(
|
||||
`npm run ${action}`,
|
||||
`pnpm( run)? ${action}`,
|
||||
`yarn ${action}`,
|
||||
`bun run ${action}`
|
||||
);
|
||||
}
|
||||
|
||||
return `(${patterns.join('|')})`;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
PACKAGE_MANAGERS,
|
||||
DETECTION_PRIORITY,
|
||||
getPackageManager,
|
||||
setPreferredPackageManager,
|
||||
setProjectPackageManager,
|
||||
getAvailablePackageManagers,
|
||||
detectFromLockFile,
|
||||
detectFromPackageJson,
|
||||
getRunCommand,
|
||||
getExecCommand,
|
||||
getSelectionPrompt,
|
||||
getCommandPattern
|
||||
};
|
||||
368
scripts/lib/utils.js
Normal file
368
scripts/lib/utils.js
Normal file
@@ -0,0 +1,368 @@
|
||||
/**
|
||||
* Cross-platform utility functions for Claude Code hooks and scripts
|
||||
* Works on Windows, macOS, and Linux
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const os = require('os');
|
||||
const { execSync, spawnSync } = require('child_process');
|
||||
|
||||
// Platform detection
|
||||
const isWindows = process.platform === 'win32';
|
||||
const isMacOS = process.platform === 'darwin';
|
||||
const isLinux = process.platform === 'linux';
|
||||
|
||||
/**
|
||||
* Get the user's home directory (cross-platform)
|
||||
*/
|
||||
function getHomeDir() {
|
||||
return os.homedir();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Claude config directory
|
||||
*/
|
||||
function getClaudeDir() {
|
||||
return path.join(getHomeDir(), '.claude');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sessions directory
|
||||
*/
|
||||
function getSessionsDir() {
|
||||
return path.join(getClaudeDir(), 'sessions');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the learned skills directory
|
||||
*/
|
||||
function getLearnedSkillsDir() {
|
||||
return path.join(getClaudeDir(), 'skills', 'learned');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the temp directory (cross-platform)
|
||||
*/
|
||||
function getTempDir() {
|
||||
return os.tmpdir();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure a directory exists (create if not)
|
||||
*/
|
||||
function ensureDir(dirPath) {
|
||||
if (!fs.existsSync(dirPath)) {
|
||||
fs.mkdirSync(dirPath, { recursive: true });
|
||||
}
|
||||
return dirPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current date in YYYY-MM-DD format
|
||||
*/
|
||||
function getDateString() {
|
||||
const now = new Date();
|
||||
const year = now.getFullYear();
|
||||
const month = String(now.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(now.getDate()).padStart(2, '0');
|
||||
return `${year}-${month}-${day}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current time in HH:MM format
|
||||
*/
|
||||
function getTimeString() {
|
||||
const now = new Date();
|
||||
const hours = String(now.getHours()).padStart(2, '0');
|
||||
const minutes = String(now.getMinutes()).padStart(2, '0');
|
||||
return `${hours}:${minutes}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current datetime in YYYY-MM-DD HH:MM:SS format
|
||||
*/
|
||||
function getDateTimeString() {
|
||||
const now = new Date();
|
||||
const year = now.getFullYear();
|
||||
const month = String(now.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(now.getDate()).padStart(2, '0');
|
||||
const hours = String(now.getHours()).padStart(2, '0');
|
||||
const minutes = String(now.getMinutes()).padStart(2, '0');
|
||||
const seconds = String(now.getSeconds()).padStart(2, '0');
|
||||
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find files matching a pattern in a directory (cross-platform alternative to find)
|
||||
* @param {string} dir - Directory to search
|
||||
* @param {string} pattern - File pattern (e.g., "*.tmp", "*.md")
|
||||
* @param {object} options - Options { maxAge: days, recursive: boolean }
|
||||
*/
|
||||
function findFiles(dir, pattern, options = {}) {
|
||||
const { maxAge = null, recursive = false } = options;
|
||||
const results = [];
|
||||
|
||||
if (!fs.existsSync(dir)) {
|
||||
return results;
|
||||
}
|
||||
|
||||
const regexPattern = pattern
|
||||
.replace(/\./g, '\\.')
|
||||
.replace(/\*/g, '.*')
|
||||
.replace(/\?/g, '.');
|
||||
const regex = new RegExp(`^${regexPattern}$`);
|
||||
|
||||
function searchDir(currentDir) {
|
||||
try {
|
||||
const entries = fs.readdirSync(currentDir, { withFileTypes: true });
|
||||
|
||||
for (const entry of entries) {
|
||||
const fullPath = path.join(currentDir, entry.name);
|
||||
|
||||
if (entry.isFile() && regex.test(entry.name)) {
|
||||
if (maxAge !== null) {
|
||||
const stats = fs.statSync(fullPath);
|
||||
const ageInDays = (Date.now() - stats.mtimeMs) / (1000 * 60 * 60 * 24);
|
||||
if (ageInDays <= maxAge) {
|
||||
results.push({ path: fullPath, mtime: stats.mtimeMs });
|
||||
}
|
||||
} else {
|
||||
const stats = fs.statSync(fullPath);
|
||||
results.push({ path: fullPath, mtime: stats.mtimeMs });
|
||||
}
|
||||
} else if (entry.isDirectory() && recursive) {
|
||||
searchDir(fullPath);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
// Ignore permission errors
|
||||
}
|
||||
}
|
||||
|
||||
searchDir(dir);
|
||||
|
||||
// Sort by modification time (newest first)
|
||||
results.sort((a, b) => b.mtime - a.mtime);
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read JSON from stdin (for hook input)
|
||||
*/
|
||||
async function readStdinJson() {
|
||||
return new Promise((resolve, reject) => {
|
||||
let data = '';
|
||||
|
||||
process.stdin.setEncoding('utf8');
|
||||
process.stdin.on('data', chunk => {
|
||||
data += chunk;
|
||||
});
|
||||
|
||||
process.stdin.on('end', () => {
|
||||
try {
|
||||
if (data.trim()) {
|
||||
resolve(JSON.parse(data));
|
||||
} else {
|
||||
resolve({});
|
||||
}
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
|
||||
process.stdin.on('error', reject);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Log to stderr (visible to user in Claude Code)
|
||||
*/
|
||||
function log(message) {
|
||||
console.error(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Output to stdout (returned to Claude)
|
||||
*/
|
||||
function output(data) {
|
||||
if (typeof data === 'object') {
|
||||
console.log(JSON.stringify(data));
|
||||
} else {
|
||||
console.log(data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a text file safely
|
||||
*/
|
||||
function readFile(filePath) {
|
||||
try {
|
||||
return fs.readFileSync(filePath, 'utf8');
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a text file
|
||||
*/
|
||||
function writeFile(filePath, content) {
|
||||
ensureDir(path.dirname(filePath));
|
||||
fs.writeFileSync(filePath, content, 'utf8');
|
||||
}
|
||||
|
||||
/**
|
||||
* Append to a text file
|
||||
*/
|
||||
function appendFile(filePath, content) {
|
||||
ensureDir(path.dirname(filePath));
|
||||
fs.appendFileSync(filePath, content, 'utf8');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a command exists in PATH
|
||||
*/
|
||||
function commandExists(cmd) {
|
||||
try {
|
||||
if (isWindows) {
|
||||
execSync(`where ${cmd}`, { stdio: 'pipe' });
|
||||
} else {
|
||||
execSync(`which ${cmd}`, { stdio: 'pipe' });
|
||||
}
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a command and return output
|
||||
*/
|
||||
function runCommand(cmd, options = {}) {
|
||||
try {
|
||||
const result = execSync(cmd, {
|
||||
encoding: 'utf8',
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
...options
|
||||
});
|
||||
return { success: true, output: result.trim() };
|
||||
} catch (err) {
|
||||
return { success: false, output: err.stderr || err.message };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if current directory is a git repository
|
||||
*/
|
||||
function isGitRepo() {
|
||||
return runCommand('git rev-parse --git-dir').success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get git modified files
|
||||
*/
|
||||
function getGitModifiedFiles(patterns = []) {
|
||||
if (!isGitRepo()) return [];
|
||||
|
||||
const result = runCommand('git diff --name-only HEAD');
|
||||
if (!result.success) return [];
|
||||
|
||||
let files = result.output.split('\n').filter(Boolean);
|
||||
|
||||
if (patterns.length > 0) {
|
||||
files = files.filter(file => {
|
||||
return patterns.some(pattern => {
|
||||
const regex = new RegExp(pattern);
|
||||
return regex.test(file);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace text in a file (cross-platform sed alternative)
|
||||
*/
|
||||
function replaceInFile(filePath, search, replace) {
|
||||
const content = readFile(filePath);
|
||||
if (content === null) return false;
|
||||
|
||||
const newContent = content.replace(search, replace);
|
||||
writeFile(filePath, newContent);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Count occurrences of a pattern in a file
|
||||
*/
|
||||
function countInFile(filePath, pattern) {
|
||||
const content = readFile(filePath);
|
||||
if (content === null) return 0;
|
||||
|
||||
const regex = pattern instanceof RegExp ? pattern : new RegExp(pattern, 'g');
|
||||
const matches = content.match(regex);
|
||||
return matches ? matches.length : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for pattern in file and return matching lines with line numbers
|
||||
*/
|
||||
function grepFile(filePath, pattern) {
|
||||
const content = readFile(filePath);
|
||||
if (content === null) return [];
|
||||
|
||||
const regex = pattern instanceof RegExp ? pattern : new RegExp(pattern);
|
||||
const lines = content.split('\n');
|
||||
const results = [];
|
||||
|
||||
lines.forEach((line, index) => {
|
||||
if (regex.test(line)) {
|
||||
results.push({ lineNumber: index + 1, content: line });
|
||||
}
|
||||
});
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
// Platform info
|
||||
isWindows,
|
||||
isMacOS,
|
||||
isLinux,
|
||||
|
||||
// Directories
|
||||
getHomeDir,
|
||||
getClaudeDir,
|
||||
getSessionsDir,
|
||||
getLearnedSkillsDir,
|
||||
getTempDir,
|
||||
ensureDir,
|
||||
|
||||
// Date/Time
|
||||
getDateString,
|
||||
getTimeString,
|
||||
getDateTimeString,
|
||||
|
||||
// File operations
|
||||
findFiles,
|
||||
readFile,
|
||||
writeFile,
|
||||
appendFile,
|
||||
replaceInFile,
|
||||
countInFile,
|
||||
grepFile,
|
||||
|
||||
// Hook I/O
|
||||
readStdinJson,
|
||||
log,
|
||||
output,
|
||||
|
||||
// System
|
||||
commandExists,
|
||||
runCommand,
|
||||
isGitRepo,
|
||||
getGitModifiedFiles
|
||||
};
|
||||
Reference in New Issue
Block a user