mirror of
https://github.com/PR0M3TH3AN/TORCH.git
synced 2026-11-06 12:51:54 +00:00
Merge pull request #346 from PR0M3TH3AN/claude/add-torch-removal-command-keFx5
Add cmdRemove command to completely uninstall TORCH from projects
This commit is contained in:
7 files changed
+584
-3
No files matched your search
@@ -23,6 +23,19 @@ export const VALID_CADENCES = new Set(['daily', 'weekly']);
|
||||
export const KIND_APP_DATA = 30078;
|
||||
export const MS_PER_SECOND = 1000;
|
||||
|
||||
/**
|
||||
* Script keys injected into the host package.json during `torch-lock init`.
|
||||
* Used by both init (to add) and remove (to clean up).
|
||||
*/
|
||||
export const TORCH_HOST_SCRIPTS = [
|
||||
'torch:dashboard',
|
||||
'torch:check',
|
||||
'torch:lock',
|
||||
'torch:health',
|
||||
'torch:memory:list',
|
||||
'torch:memory:inspect',
|
||||
];
|
||||
|
||||
export const USAGE_TEXT = `Usage: torch-lock <command> [options]
|
||||
|
||||
Commands:
|
||||
@@ -34,6 +47,7 @@ Commands:
|
||||
dashboard [--port <port>] [--host <host>] Serve the dashboard (default: ${DEFAULT_DASHBOARD_PORT})
|
||||
init [--force] Initialize torch/ directory in current project
|
||||
update [--force] Update torch/ configuration (backups, merges)
|
||||
remove [--force] Remove all TORCH files and configuration from project
|
||||
|
||||
list-memories [--agent <id>] [--type <type>] [--tags <a,b>] [--pinned <true|false>] [--full]
|
||||
inspect-memory --id <memoryId>
|
||||
|
||||
+6
-1
@@ -20,7 +20,7 @@ import {
|
||||
USAGE_TEXT,
|
||||
MS_PER_SECOND,
|
||||
} from './constants.mjs';
|
||||
import { cmdInit, cmdUpdate } from './ops.mjs';
|
||||
import { cmdInit, cmdUpdate, cmdRemove } from './ops.mjs';
|
||||
import { parseArgs } from './cli-parser.mjs';
|
||||
import { getRoster as _getRoster } from './roster.mjs';
|
||||
import { queryLocks as _queryLocks, publishLock as _publishLock, parseLockEvent } from './lock-ops.mjs';
|
||||
@@ -637,6 +637,11 @@ export async function main(argv) {
|
||||
break;
|
||||
}
|
||||
|
||||
case 'remove': {
|
||||
await cmdRemove(args.force);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'list-memories': {
|
||||
const result = await listMemories({
|
||||
agent_id: args.agent,
|
||||
|
||||
+252
-1
@@ -4,7 +4,7 @@ import { fileURLToPath } from 'node:url';
|
||||
import crypto from 'node:crypto';
|
||||
import readline from 'node:readline/promises';
|
||||
import { stdin as input, stdout as output } from 'node:process';
|
||||
import { DEFAULT_RELAYS } from './constants.mjs';
|
||||
import { DEFAULT_RELAYS, TORCH_HOST_SCRIPTS } from './constants.mjs';
|
||||
import { ensureDir } from './utils.mjs';
|
||||
|
||||
const PKG_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
||||
@@ -401,6 +401,257 @@ function injectScriptsIntoHost(hostRoot, installDirName) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Detects the TORCH install directory within a project.
|
||||
*
|
||||
* Strategy:
|
||||
* 1. If a 'torch/' directory exists with a roster.json or bin/, it's the install dir.
|
||||
* 2. If the current directory's package.json has name "torch-lock", installed to '.'.
|
||||
* 3. Returns null if TORCH is not detected.
|
||||
*
|
||||
* @param {string} cwd - The project root directory
|
||||
* @returns {string|null} - The install directory name ('torch', '.', etc.) or null
|
||||
*/
|
||||
function detectTorchInstallDir(cwd) {
|
||||
// Check for 'torch/' subdirectory with TORCH markers
|
||||
const torchSubdir = path.join(cwd, 'torch');
|
||||
if (fs.existsSync(torchSubdir)) {
|
||||
const hasRoster = fs.existsSync(path.join(torchSubdir, 'roster.json'));
|
||||
const hasBin = fs.existsSync(path.join(torchSubdir, 'bin'));
|
||||
if (hasRoster || hasBin) {
|
||||
return 'torch';
|
||||
}
|
||||
}
|
||||
|
||||
// Check if installed to current directory
|
||||
const pkgPath = path.join(cwd, 'package.json');
|
||||
if (fs.existsSync(pkgPath)) {
|
||||
try {
|
||||
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
||||
if (pkg.name === 'torch-lock') {
|
||||
return '.';
|
||||
}
|
||||
} catch (_e) {
|
||||
// Ignore parse errors
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a directory or file if it exists, logging the action.
|
||||
*
|
||||
* @param {string} target - Absolute path to remove
|
||||
* @param {string} label - Human-readable label for log output
|
||||
* @param {Function} log - Logging function
|
||||
* @returns {boolean} - True if something was removed
|
||||
*/
|
||||
function removeIfExists(target, label, log) {
|
||||
if (!fs.existsSync(target)) return false;
|
||||
|
||||
const stat = fs.statSync(target);
|
||||
if (stat.isDirectory()) {
|
||||
fs.rmSync(target, { recursive: true, force: true });
|
||||
} else {
|
||||
fs.unlinkSync(target);
|
||||
}
|
||||
log(` Removed ${label}`);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes TORCH torch:* scripts from the host package.json.
|
||||
*
|
||||
* @param {string} hostRoot - Project root containing package.json
|
||||
* @param {Function} log - Logging function
|
||||
* @returns {boolean} - True if any scripts were removed
|
||||
*/
|
||||
function removeHostScripts(hostRoot, log) {
|
||||
const hostPkgPath = path.join(hostRoot, 'package.json');
|
||||
if (!fs.existsSync(hostPkgPath)) return false;
|
||||
|
||||
try {
|
||||
const raw = fs.readFileSync(hostPkgPath, 'utf8');
|
||||
const pkg = JSON.parse(raw);
|
||||
if (!pkg.scripts) return false;
|
||||
|
||||
let modified = false;
|
||||
for (const key of TORCH_HOST_SCRIPTS) {
|
||||
if (key in pkg.scripts) {
|
||||
delete pkg.scripts[key];
|
||||
log(` Removed script: "${key}"`);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
fs.writeFileSync(hostPkgPath, JSON.stringify(pkg, null, 2) + '\n', 'utf8');
|
||||
log(' Updated package.json.');
|
||||
}
|
||||
return modified;
|
||||
} catch (e) {
|
||||
log(` Warning: Could not update package.json: ${e.message}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Completely removes TORCH from a project that installed it via
|
||||
* `npm install <torch-tarball> && npx torch-lock init`.
|
||||
*
|
||||
* What it removes:
|
||||
* 1. The torch/ install directory (or equivalent)
|
||||
* 2. torch-config.json at the project root
|
||||
* 3. .torch/ hidden directory (prompt history)
|
||||
* 4. .scheduler-memory/ directory (runtime memory store)
|
||||
* 5. task-logs/ directory (scheduler run logs)
|
||||
* 6. src/proposals/ directory (governance proposals)
|
||||
* 7. torch:* scripts from the host package.json
|
||||
* 8. The torch-lock npm package (node_modules/torch-lock)
|
||||
*
|
||||
* @param {boolean} force - If true, skip the confirmation prompt
|
||||
* @param {string} cwd - Working directory (project root)
|
||||
* @param {Object|null} mockAnswers - For testing: { confirm: true/false }
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function cmdRemove(force = false, cwd = process.cwd(), mockAnswers = null) {
|
||||
const log = console.log;
|
||||
const installDir = detectTorchInstallDir(cwd);
|
||||
|
||||
if (!installDir) {
|
||||
log('No TORCH installation detected in this directory.');
|
||||
log('Looked for:');
|
||||
log(' - A torch/ subdirectory containing roster.json or bin/');
|
||||
log(' - A package.json with name "torch-lock" (root install)');
|
||||
return;
|
||||
}
|
||||
|
||||
const isRootInstall = installDir === '.';
|
||||
const torchDir = path.resolve(cwd, installDir);
|
||||
|
||||
// Build a manifest of what will be removed
|
||||
const targets = [];
|
||||
|
||||
if (!isRootInstall && fs.existsSync(torchDir)) {
|
||||
targets.push({ path: torchDir, label: `${installDir}/ (TORCH install directory)` });
|
||||
}
|
||||
|
||||
const configPath = path.join(cwd, 'torch-config.json');
|
||||
if (fs.existsSync(configPath)) {
|
||||
targets.push({ path: configPath, label: 'torch-config.json' });
|
||||
}
|
||||
|
||||
const dotTorch = path.join(cwd, '.torch');
|
||||
if (fs.existsSync(dotTorch)) {
|
||||
targets.push({ path: dotTorch, label: '.torch/ (prompt history)' });
|
||||
}
|
||||
|
||||
const schedulerMemory = path.join(cwd, '.scheduler-memory');
|
||||
if (fs.existsSync(schedulerMemory)) {
|
||||
targets.push({ path: schedulerMemory, label: '.scheduler-memory/ (memory store)' });
|
||||
}
|
||||
|
||||
const taskLogs = path.join(cwd, 'task-logs');
|
||||
if (fs.existsSync(taskLogs)) {
|
||||
targets.push({ path: taskLogs, label: 'task-logs/ (scheduler logs)' });
|
||||
}
|
||||
|
||||
const proposals = path.join(cwd, 'src', 'proposals');
|
||||
if (fs.existsSync(proposals)) {
|
||||
targets.push({ path: proposals, label: 'src/proposals/ (governance proposals)' });
|
||||
}
|
||||
|
||||
// Check for host scripts
|
||||
const hostPkgPath = path.join(cwd, 'package.json');
|
||||
let hasHostScripts = false;
|
||||
if (fs.existsSync(hostPkgPath)) {
|
||||
try {
|
||||
const pkg = JSON.parse(fs.readFileSync(hostPkgPath, 'utf8'));
|
||||
if (pkg.scripts) {
|
||||
hasHostScripts = TORCH_HOST_SCRIPTS.some(key => key in pkg.scripts);
|
||||
}
|
||||
} catch (_e) { /* ignore */ }
|
||||
}
|
||||
|
||||
if (targets.length === 0 && !hasHostScripts) {
|
||||
log('Nothing to remove — no TORCH artifacts found.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Show what will be removed
|
||||
log('\nThe following TORCH artifacts will be removed:\n');
|
||||
for (const t of targets) {
|
||||
log(` - ${t.label}`);
|
||||
}
|
||||
if (hasHostScripts) {
|
||||
log(' - torch:* scripts from package.json');
|
||||
}
|
||||
log(' - torch-lock npm package (via npm uninstall)');
|
||||
log('');
|
||||
|
||||
// Confirm unless --force
|
||||
if (!force) {
|
||||
let confirmed;
|
||||
|
||||
if (mockAnswers) {
|
||||
confirmed = mockAnswers.confirm;
|
||||
} else {
|
||||
const rl = readline.createInterface({ input, output });
|
||||
try {
|
||||
const answer = await rl.question('Proceed with removal? (yes/no): ');
|
||||
confirmed = answer.trim().toLowerCase() === 'yes';
|
||||
} finally {
|
||||
rl.close();
|
||||
}
|
||||
}
|
||||
|
||||
if (!confirmed) {
|
||||
log('Removal cancelled.');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
log('\nRemoving TORCH...\n');
|
||||
|
||||
// 1. Remove directories and files
|
||||
for (const t of targets) {
|
||||
removeIfExists(t.path, t.label, log);
|
||||
}
|
||||
|
||||
// 2. Clean up empty src/ directory if proposals was the only thing in it
|
||||
const srcDir = path.join(cwd, 'src');
|
||||
if (fs.existsSync(srcDir)) {
|
||||
try {
|
||||
const entries = fs.readdirSync(srcDir);
|
||||
if (entries.length === 0) {
|
||||
fs.rmdirSync(srcDir);
|
||||
log(' Removed empty src/ directory');
|
||||
}
|
||||
} catch (_e) { /* leave it if not empty or permission error */ }
|
||||
}
|
||||
|
||||
// 3. Remove host package.json scripts
|
||||
if (hasHostScripts) {
|
||||
removeHostScripts(cwd, log);
|
||||
}
|
||||
|
||||
// 4. Uninstall the npm package
|
||||
log('\n Running npm uninstall torch-lock...');
|
||||
try {
|
||||
const { execSync } = await import('node:child_process');
|
||||
execSync('npm uninstall torch-lock', { cwd, stdio: 'pipe' });
|
||||
log(' Uninstalled torch-lock package.');
|
||||
} catch (e) {
|
||||
log(` Warning: npm uninstall torch-lock failed: ${e.message}`);
|
||||
log(' You may need to run "npm uninstall torch-lock" manually.');
|
||||
}
|
||||
|
||||
log('\nTORCH has been completely removed from this project.');
|
||||
log('If you used TORCH environment variables (NOSTR_LOCK_*, TORCH_*),');
|
||||
log('remember to remove them from your shell profile or CI configuration.');
|
||||
}
|
||||
|
||||
export function cmdUpdate(force = false, cwd = process.cwd()) {
|
||||
// Update logic needs to know WHERE torch is installed.
|
||||
// We can look for torch directory? Or assume 'torch'?
|
||||
|
||||
Reference in new issue
Block a user