refactor: rewrite install script to match Dirigent pattern
- Plugin name changed to 'padded-cell' (in OpenClaw) - Install to ~/.openclaw/plugins/padded-cell/ instead of skills - Add plugins.entries.padded-cell configuration - Add to plugins.allow list - Use --install and --uninstall flags - Follow Dirigent's delta tracking pattern
This commit is contained in:
735
install.mjs
735
install.mjs
@@ -4,38 +4,32 @@
|
|||||||
* PaddedCell Plugin Installer
|
* PaddedCell Plugin Installer
|
||||||
*
|
*
|
||||||
* Usage:
|
* Usage:
|
||||||
* node install.mjs
|
* node install.mjs --install
|
||||||
* node install.mjs --prefix /usr/local
|
|
||||||
* node install.mjs --build-only
|
|
||||||
* node install.mjs --skip-check
|
|
||||||
* node install.mjs --uninstall
|
* node install.mjs --uninstall
|
||||||
* node install.mjs --uninstall --prefix /usr/local
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { execSync } from 'child_process';
|
import { execSync, spawnSync } from 'child_process';
|
||||||
import { existsSync, mkdirSync, copyFileSync, writeFileSync, chmodSync } from 'fs';
|
import { existsSync, mkdirSync, copyFileSync, writeFileSync, chmodSync, readdirSync, statSync } from 'fs';
|
||||||
import { dirname, join, resolve } from 'path';
|
import { dirname, join, resolve } from 'path';
|
||||||
import { fileURLToPath } from 'url';
|
import { fileURLToPath } from 'url';
|
||||||
import { homedir, platform } from 'os';
|
import { homedir, platform } from 'os';
|
||||||
import readline from 'readline';
|
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = dirname(__filename);
|
const __dirname = dirname(__filename);
|
||||||
|
|
||||||
|
// Plugin configuration
|
||||||
|
const PLUGIN_NAME = 'padded-cell';
|
||||||
|
const PLUGIN_ENTRY = `plugins.entries.${PLUGIN_NAME}`;
|
||||||
|
const PLUGIN_ALLOW_PATH = 'plugins.allow';
|
||||||
|
const PLUGINS_LOAD_PATHS = 'plugins.load.paths';
|
||||||
|
|
||||||
// Parse arguments
|
// Parse arguments
|
||||||
const args = process.argv.slice(2);
|
const args = process.argv.slice(2);
|
||||||
const options = {
|
const mode = args.includes('--uninstall') ? 'uninstall' : (args.includes('--install') ? 'install' : null);
|
||||||
prefix: null,
|
|
||||||
buildOnly: args.includes('--build-only'),
|
|
||||||
skipCheck: args.includes('--skip-check'),
|
|
||||||
verbose: args.includes('--verbose') || args.includes('-v'),
|
|
||||||
uninstall: args.includes('--uninstall'),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Parse --prefix value
|
if (!mode) {
|
||||||
const prefixIndex = args.indexOf('--prefix');
|
console.error('Usage: install.mjs --install | --uninstall');
|
||||||
if (prefixIndex !== -1 && args[prefixIndex + 1]) {
|
process.exit(2);
|
||||||
options.prefix = resolve(args[prefixIndex + 1]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Colors for output
|
// Colors for output
|
||||||
@@ -52,177 +46,122 @@ function log(message, color = 'reset') {
|
|||||||
console.log(`${colors[color]}${message}${colors.reset}`);
|
console.log(`${colors[color]}${message}${colors.reset}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function logStep(step, message) {
|
|
||||||
log(`[${step}/6] ${message}`, 'cyan');
|
|
||||||
}
|
|
||||||
|
|
||||||
function logSuccess(message) {
|
function logSuccess(message) {
|
||||||
log(` ✓ ${message}`, 'green');
|
log(`[${PLUGIN_NAME}] ✓ ${message}`, 'green');
|
||||||
}
|
}
|
||||||
|
|
||||||
function logWarning(message) {
|
function logWarning(message) {
|
||||||
log(` ⚠ ${message}`, 'yellow');
|
log(`[${PLUGIN_NAME}] ⚠ ${message}`, 'yellow');
|
||||||
}
|
}
|
||||||
|
|
||||||
function logError(message) {
|
function logError(message) {
|
||||||
log(` ✗ ${message}`, 'red');
|
log(`[${PLUGIN_NAME}] ✗ ${message}`, 'red');
|
||||||
}
|
}
|
||||||
|
|
||||||
function exec(command, options = {}) {
|
function logInfo(message) {
|
||||||
const defaultOptions = {
|
log(`[${PLUGIN_NAME}] ${message}`, 'cyan');
|
||||||
cwd: __dirname,
|
|
||||||
stdio: options.silent ? 'pipe' : 'inherit',
|
|
||||||
encoding: 'utf8',
|
|
||||||
};
|
|
||||||
return execSync(command, { ...defaultOptions, ...options });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function prompt(question) {
|
// Execute openclaw command
|
||||||
const rl = readline.createInterface({
|
function runOpenclaw(args, { allowFail = false } = {}) {
|
||||||
input: process.stdin,
|
try {
|
||||||
output: process.stdout,
|
return execSync('openclaw', args, { encoding: 'utf8' }).trim();
|
||||||
});
|
} catch (e) {
|
||||||
return new Promise((resolve) => {
|
if (allowFail) return null;
|
||||||
rl.question(question, (answer) => {
|
throw e;
|
||||||
rl.close();
|
}
|
||||||
resolve(answer.trim());
|
}
|
||||||
});
|
|
||||||
});
|
// Get config value as JSON
|
||||||
|
function getJson(pathKey) {
|
||||||
|
const out = runOpenclaw(['config', 'get', pathKey, '--json'], { allowFail: true });
|
||||||
|
if (out == null || out === '' || out === 'undefined') return undefined;
|
||||||
|
try {
|
||||||
|
return JSON.parse(out);
|
||||||
|
} catch {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set config value as JSON
|
||||||
|
function setJson(pathKey, value) {
|
||||||
|
runOpenclaw(['config', 'set', pathKey, JSON.stringify(value), '--json']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unset config path
|
||||||
|
function unsetPath(pathKey) {
|
||||||
|
runOpenclaw(['config', 'unset', pathKey], { allowFail: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deep clone
|
||||||
|
function clone(v) {
|
||||||
|
if (v === undefined) return undefined;
|
||||||
|
return JSON.parse(JSON.stringify(v));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy directory recursively
|
||||||
|
function copyDir(src, dest) {
|
||||||
|
mkdirSync(dest, { recursive: true });
|
||||||
|
const entries = readdirSync(src, { withFileTypes: true });
|
||||||
|
|
||||||
|
for (const entry of entries) {
|
||||||
|
const srcPath = join(src, entry.name);
|
||||||
|
const destPath = join(dest, entry.name);
|
||||||
|
|
||||||
|
if (entry.isDirectory()) {
|
||||||
|
copyDir(srcPath, destPath);
|
||||||
|
} else {
|
||||||
|
copyFileSync(srcPath, destPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// Step 1: Environment Detection
|
// Detect Environment
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
function detectEnvironment() {
|
function detectEnvironment() {
|
||||||
logStep(1, 'Detecting environment...');
|
|
||||||
|
|
||||||
const env = {
|
const env = {
|
||||||
platform: platform(),
|
openclawDir: join(homedir(), '.openclaw'),
|
||||||
isLinux: platform() === 'linux',
|
|
||||||
isMacOS: platform() === 'darwin',
|
|
||||||
nodeVersion: null,
|
nodeVersion: null,
|
||||||
goVersion: null,
|
goVersion: null,
|
||||||
openclawPath: null,
|
|
||||||
openclawDir: null,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Check Node.js
|
// Check Node.js
|
||||||
try {
|
try {
|
||||||
const version = exec('node --version', { silent: true }).trim();
|
env.nodeVersion = execSync('node --version', { encoding: 'utf8' }).trim();
|
||||||
env.nodeVersion = version;
|
|
||||||
logSuccess(`Node.js ${version}`);
|
|
||||||
} catch {
|
} catch {
|
||||||
logError('Node.js not found');
|
logError('Node.js not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check Go
|
// Check Go
|
||||||
try {
|
try {
|
||||||
const version = exec('go version', { silent: true }).trim();
|
env.goVersion = execSync('go version', { encoding: 'utf8' }).trim();
|
||||||
env.goVersion = version;
|
|
||||||
logSuccess(`Go ${version}`);
|
|
||||||
} catch {
|
} catch {
|
||||||
logError('Go not found');
|
logError('Go not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check openclaw
|
|
||||||
try {
|
|
||||||
const path = exec('which openclaw', { silent: true }).trim();
|
|
||||||
env.openclawPath = path;
|
|
||||||
logSuccess(`openclaw at ${path}`);
|
|
||||||
|
|
||||||
// Try to find openclaw config dir
|
|
||||||
const home = homedir();
|
|
||||||
const possibleDirs = [
|
|
||||||
join(home, '.openclaw'),
|
|
||||||
join(home, '.config', 'openclaw'),
|
|
||||||
'/etc/openclaw',
|
|
||||||
];
|
|
||||||
|
|
||||||
for (const dir of possibleDirs) {
|
|
||||||
if (existsSync(dir)) {
|
|
||||||
env.openclawDir = dir;
|
|
||||||
logSuccess(`openclaw config dir: ${dir}`);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!env.openclawDir) {
|
|
||||||
env.openclawDir = join(home, '.openclaw');
|
|
||||||
logWarning(`openclaw config dir not found, will use: ${env.openclawDir}`);
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
logWarning('openclaw CLI not found in PATH');
|
|
||||||
env.openclawDir = join(homedir(), '.openclaw');
|
|
||||||
}
|
|
||||||
|
|
||||||
return env;
|
return env;
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkDependencies(env) {
|
|
||||||
if (options.skipCheck) {
|
|
||||||
logWarning('Skipping dependency checks');
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
logStep(2, 'Checking dependencies...');
|
|
||||||
|
|
||||||
let hasErrors = false;
|
|
||||||
|
|
||||||
if (!env.nodeVersion) {
|
|
||||||
logError('Node.js is required. Please install Node.js 18+');
|
|
||||||
hasErrors = true;
|
|
||||||
} else {
|
|
||||||
const majorVersion = parseInt(env.nodeVersion.slice(1).split('.')[0]);
|
|
||||||
if (majorVersion < 18) {
|
|
||||||
logError(`Node.js 18+ required, found ${env.nodeVersion}`);
|
|
||||||
hasErrors = true;
|
|
||||||
} else {
|
|
||||||
logSuccess(`Node.js version OK`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!env.goVersion) {
|
|
||||||
logError('Go is required. Please install Go 1.22+');
|
|
||||||
hasErrors = true;
|
|
||||||
} else {
|
|
||||||
logSuccess(`Go version OK`);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hasErrors) {
|
|
||||||
log('\nPlease install missing dependencies and try again.', 'red');
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// Step 3: Build Components
|
// Build Components
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
async function buildComponents(env) {
|
function buildComponents(env) {
|
||||||
logStep(3, 'Building components...');
|
logInfo('Building components...');
|
||||||
|
|
||||||
// Build pass_mgr
|
// Build pass_mgr
|
||||||
log(' Building pass_mgr (Go)...', 'blue');
|
log(' Building pass_mgr (Go)...', 'blue');
|
||||||
try {
|
try {
|
||||||
const passMgrDir = join(__dirname, 'pass_mgr');
|
const passMgrDir = join(__dirname, 'pass_mgr');
|
||||||
if (!existsSync(passMgrDir)) {
|
execSync('go mod tidy', { cwd: passMgrDir, stdio: 'inherit' });
|
||||||
throw new Error('pass_mgr directory not found');
|
execSync('go build -o dist/pass_mgr src/main.go', { cwd: passMgrDir, stdio: 'inherit' });
|
||||||
}
|
|
||||||
|
|
||||||
exec('go mod tidy', { cwd: passMgrDir, silent: !options.verbose });
|
|
||||||
exec('go build -o dist/pass_mgr src/main.go', {
|
|
||||||
cwd: passMgrDir,
|
|
||||||
silent: !options.verbose
|
|
||||||
});
|
|
||||||
|
|
||||||
const binaryPath = join(passMgrDir, 'dist', 'pass_mgr');
|
const binaryPath = join(passMgrDir, 'dist', 'pass_mgr');
|
||||||
if (!existsSync(binaryPath)) {
|
if (!existsSync(binaryPath)) {
|
||||||
throw new Error('pass_mgr binary not found after build');
|
throw new Error('pass_mgr binary not found after build');
|
||||||
}
|
}
|
||||||
|
|
||||||
chmodSync(binaryPath, 0o755);
|
chmodSync(binaryPath, 0o755);
|
||||||
logSuccess('pass_mgr built successfully');
|
logSuccess('pass_mgr built successfully');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -234,13 +173,8 @@ async function buildComponents(env) {
|
|||||||
log(' Building pcexec (TypeScript)...', 'blue');
|
log(' Building pcexec (TypeScript)...', 'blue');
|
||||||
try {
|
try {
|
||||||
const pcexecDir = join(__dirname, 'pcexec');
|
const pcexecDir = join(__dirname, 'pcexec');
|
||||||
if (!existsSync(pcexecDir)) {
|
execSync('npm install', { cwd: pcexecDir, stdio: 'inherit' });
|
||||||
throw new Error('pcexec directory not found');
|
execSync('npm run build', { cwd: pcexecDir, stdio: 'inherit' });
|
||||||
}
|
|
||||||
|
|
||||||
exec('npm install', { cwd: pcexecDir, silent: !options.verbose });
|
|
||||||
exec('npm run build', { cwd: pcexecDir, silent: !options.verbose });
|
|
||||||
|
|
||||||
logSuccess('pcexec built successfully');
|
logSuccess('pcexec built successfully');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logError(`Failed to build pcexec: ${err.message}`);
|
logError(`Failed to build pcexec: ${err.message}`);
|
||||||
@@ -251,13 +185,8 @@ async function buildComponents(env) {
|
|||||||
log(' Building safe-restart (TypeScript)...', 'blue');
|
log(' Building safe-restart (TypeScript)...', 'blue');
|
||||||
try {
|
try {
|
||||||
const safeRestartDir = join(__dirname, 'safe-restart');
|
const safeRestartDir = join(__dirname, 'safe-restart');
|
||||||
if (!existsSync(safeRestartDir)) {
|
execSync('npm install', { cwd: safeRestartDir, stdio: 'inherit' });
|
||||||
throw new Error('safe-restart directory not found');
|
execSync('npm run build', { cwd: safeRestartDir, stdio: 'inherit' });
|
||||||
}
|
|
||||||
|
|
||||||
exec('npm install', { cwd: safeRestartDir, silent: !options.verbose });
|
|
||||||
exec('npm run build', { cwd: safeRestartDir, silent: !options.verbose });
|
|
||||||
|
|
||||||
logSuccess('safe-restart built successfully');
|
logSuccess('safe-restart built successfully');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logError(`Failed to build safe-restart: ${err.message}`);
|
logError(`Failed to build safe-restart: ${err.message}`);
|
||||||
@@ -266,358 +195,254 @@ async function buildComponents(env) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// Step 4: Install Components
|
// Install Plugin
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
async function installComponents(env) {
|
function installPlugin(env) {
|
||||||
if (options.buildOnly) {
|
logInfo('Installing plugin...');
|
||||||
logStep(4, 'Skipping installation (--build-only)');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
logStep(4, 'Installing components...');
|
const pluginDir = join(env.openclawDir, 'plugins', PLUGIN_NAME);
|
||||||
|
const binDir = join(env.openclawDir, 'bin');
|
||||||
const installDir = options.prefix || env.openclawDir;
|
|
||||||
const binDir = join(installDir, 'bin');
|
|
||||||
const skillsDir = join(installDir, 'skills', 'paddedcell');
|
|
||||||
|
|
||||||
log(` Install directory: ${installDir}`, 'blue');
|
|
||||||
log(` Binary directory: ${binDir}`, 'blue');
|
|
||||||
log(` Skills directory: ${skillsDir}`, 'blue');
|
|
||||||
|
|
||||||
|
// Create directories
|
||||||
|
mkdirSync(pluginDir, { recursive: true });
|
||||||
mkdirSync(binDir, { recursive: true });
|
mkdirSync(binDir, { recursive: true });
|
||||||
mkdirSync(skillsDir, { recursive: true });
|
|
||||||
|
// Copy plugin files
|
||||||
|
log(' Copying plugin files...', 'blue');
|
||||||
|
|
||||||
|
// Copy pcexec
|
||||||
|
const pcexecSource = join(__dirname, 'pcexec');
|
||||||
|
const pcexecDest = join(pluginDir, 'pcexec');
|
||||||
|
copyDir(pcexecSource, pcexecDest);
|
||||||
|
logSuccess(`Installed pcexec to ${pcexecDest}`);
|
||||||
|
|
||||||
|
// Copy safe-restart
|
||||||
|
const safeRestartSource = join(__dirname, 'safe-restart');
|
||||||
|
const safeRestartDest = join(pluginDir, 'safe-restart');
|
||||||
|
copyDir(safeRestartSource, safeRestartDest);
|
||||||
|
logSuccess(`Installed safe-restart to ${safeRestartDest}`);
|
||||||
|
|
||||||
// Install pass_mgr binary
|
// Install pass_mgr binary
|
||||||
log(' Installing pass_mgr binary...', 'blue');
|
|
||||||
const passMgrSource = join(__dirname, 'pass_mgr', 'dist', 'pass_mgr');
|
const passMgrSource = join(__dirname, 'pass_mgr', 'dist', 'pass_mgr');
|
||||||
const passMgrDest = join(binDir, 'pass_mgr');
|
const passMgrDest = join(binDir, 'pass_mgr');
|
||||||
copyFileSync(passMgrSource, passMgrDest);
|
copyFileSync(passMgrSource, passMgrDest);
|
||||||
chmodSync(passMgrDest, 0o755);
|
chmodSync(passMgrDest, 0o755);
|
||||||
logSuccess(`pass_mgr installed to ${passMgrDest}`);
|
logSuccess(`Installed pass_mgr binary to ${passMgrDest}`);
|
||||||
|
|
||||||
// Install pcexec
|
return { pluginDir, passMgrPath: passMgrDest };
|
||||||
log(' Installing pcexec module...', 'blue');
|
}
|
||||||
const pcexecSource = join(__dirname, 'pcexec');
|
|
||||||
const pcexecDest = join(skillsDir, 'pcexec');
|
|
||||||
copyModule(pcexecSource, pcexecDest);
|
|
||||||
logSuccess(`pcexec installed to ${pcexecDest}`);
|
|
||||||
|
|
||||||
// Install safe-restart
|
// ============================================================================
|
||||||
log(' Installing safe-restart module...', 'blue');
|
// Configure OpenClaw
|
||||||
const safeRestartSource = join(__dirname, 'safe-restart');
|
// ============================================================================
|
||||||
const safeRestartDest = join(skillsDir, 'safe-restart');
|
|
||||||
copyModule(safeRestartSource, safeRestartDest);
|
|
||||||
logSuccess(`safe-restart installed to ${safeRestartDest}`);
|
|
||||||
|
|
||||||
// Create skill manifest
|
function configureOpenClaw(env, pluginDir, delta) {
|
||||||
log(' Creating skill manifest...', 'blue');
|
logInfo('Configuring OpenClaw...');
|
||||||
const manifest = {
|
|
||||||
name: 'paddedcell',
|
// 1. Add plugin to plugins.allow
|
||||||
version: '0.1.0',
|
const allowList = getJson(PLUGIN_ALLOW_PATH) || [];
|
||||||
description: 'Secure password management, safe execution, and coordinated restart',
|
const oldAllow = clone(allowList);
|
||||||
tools: [
|
|
||||||
{
|
if (!allowList.includes(PLUGIN_NAME)) {
|
||||||
name: 'pcexec',
|
allowList.push(PLUGIN_NAME);
|
||||||
entry: './pcexec/dist/index.js',
|
setJson(PLUGIN_ALLOW_PATH, allowList);
|
||||||
description: 'Safe exec with password sanitization',
|
delta.added[PLUGIN_ALLOW_PATH] = PLUGIN_NAME;
|
||||||
|
delta._prev = delta._prev || {};
|
||||||
|
delta._prev[PLUGIN_ALLOW_PATH] = oldAllow;
|
||||||
|
logSuccess(`Added '${PLUGIN_NAME}' to plugins.allow`);
|
||||||
|
} else {
|
||||||
|
log(' Already in plugins.allow', 'green');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Add plugin entry
|
||||||
|
const oldEntry = getJson(PLUGIN_ENTRY);
|
||||||
|
const newEntry = {
|
||||||
|
enabled: true,
|
||||||
|
config: {
|
||||||
|
enabled: true,
|
||||||
|
passMgrPath: join(env.openclawDir, 'bin', 'pass_mgr'),
|
||||||
|
pluginDir: pluginDir,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: 'safe_restart',
|
|
||||||
entry: './safe-restart/dist/index.js',
|
|
||||||
description: 'Safe coordinated restart',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
binaries: ['pass_mgr'],
|
|
||||||
installedAt: new Date().toISOString(),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
writeFileSync(
|
if (oldEntry === undefined) {
|
||||||
join(skillsDir, 'manifest.json'),
|
delta.added[PLUGIN_ENTRY] = newEntry;
|
||||||
JSON.stringify(manifest, null, 2)
|
|
||||||
);
|
|
||||||
logSuccess('Skill manifest created');
|
|
||||||
}
|
|
||||||
|
|
||||||
function copyModule(source, dest) {
|
|
||||||
mkdirSync(dest, { recursive: true });
|
|
||||||
|
|
||||||
const items = ['package.json', 'tsconfig.json', 'dist', 'src'];
|
|
||||||
for (const item of items) {
|
|
||||||
const srcPath = join(source, item);
|
|
||||||
const destPath = join(dest, item);
|
|
||||||
|
|
||||||
if (!existsSync(srcPath)) continue;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const stat = execSync(`stat -c %F "${srcPath}" 2>/dev/null || echo "file"`, {
|
|
||||||
encoding: 'utf8',
|
|
||||||
cwd: __dirname
|
|
||||||
}).trim();
|
|
||||||
|
|
||||||
if (stat === 'directory') {
|
|
||||||
execSync(`cp -r "${srcPath}" "${destPath}"`, { cwd: __dirname });
|
|
||||||
} else {
|
} else {
|
||||||
copyFileSync(srcPath, destPath);
|
delta.replaced[PLUGIN_ENTRY] = oldEntry;
|
||||||
}
|
}
|
||||||
} catch {
|
|
||||||
try {
|
// Use set for nested path
|
||||||
copyFileSync(srcPath, destPath);
|
const plugins = getJson('plugins') || {};
|
||||||
} catch {
|
plugins.entries = plugins.entries || {};
|
||||||
execSync(`cp -r "${srcPath}" "${destPath}"`, { cwd: __dirname });
|
plugins.entries[PLUGIN_NAME] = newEntry;
|
||||||
|
setJson('plugins', plugins);
|
||||||
|
logSuccess(`Configured ${PLUGIN_ENTRY}`);
|
||||||
|
|
||||||
|
// 3. Add to plugins.load.paths
|
||||||
|
const pluginsConfig = getJson('plugins') || {};
|
||||||
|
const oldPaths = clone(pluginsConfig.load?.paths) || [];
|
||||||
|
const newPaths = clone(oldPaths);
|
||||||
|
|
||||||
|
if (!newPaths.includes(pluginDir)) {
|
||||||
|
newPaths.push(pluginDir);
|
||||||
|
delta.added[PLUGINS_LOAD_PATHS] = pluginDir;
|
||||||
|
delta._prev = delta._prev || {};
|
||||||
|
delta._prev[PLUGINS_LOAD_PATHS] = oldPaths;
|
||||||
|
|
||||||
|
pluginsConfig.load = pluginsConfig.load || {};
|
||||||
|
pluginsConfig.load.paths = newPaths;
|
||||||
|
setJson('plugins', pluginsConfig);
|
||||||
|
logSuccess(`Added plugin path to ${PLUGINS_LOAD_PATHS}`);
|
||||||
|
} else {
|
||||||
|
log(' Plugin path already in load.paths', 'green');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// Unconfigure OpenClaw
|
||||||
|
// ============================================================================
|
||||||
|
|
||||||
|
function unconfigureOpenClaw(env, delta) {
|
||||||
|
logInfo('Removing OpenClaw configuration...');
|
||||||
|
|
||||||
|
// 1. Remove from plugins.allow first (before removing entry)
|
||||||
|
if (delta.added?.[PLUGIN_ALLOW_PATH] !== undefined) {
|
||||||
|
const allowList = getJson(PLUGIN_ALLOW_PATH) || [];
|
||||||
|
const idx = allowList.indexOf(PLUGIN_NAME);
|
||||||
|
if (idx !== -1) {
|
||||||
|
allowList.splice(idx, 1);
|
||||||
|
setJson(PLUGIN_ALLOW_PATH, allowList);
|
||||||
|
logSuccess(`Removed '${PLUGIN_NAME}' from plugins.allow`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Remove plugin entry
|
||||||
|
if (delta.added?.[PLUGIN_ENTRY] !== undefined || delta.replaced?.[PLUGIN_ENTRY] !== undefined) {
|
||||||
|
unsetPath(PLUGIN_ENTRY);
|
||||||
|
logSuccess(`Removed ${PLUGIN_ENTRY}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Remove from plugins.load.paths
|
||||||
|
if (delta.added?.[PLUGINS_LOAD_PATHS] !== undefined) {
|
||||||
|
const plugins = getJson('plugins') || {};
|
||||||
|
const paths = plugins.load?.paths || [];
|
||||||
|
const pluginDir = join(env.openclawDir, 'plugins', PLUGIN_NAME);
|
||||||
|
const idx = paths.indexOf(pluginDir);
|
||||||
|
|
||||||
|
if (idx !== -1) {
|
||||||
|
paths.splice(idx, 1);
|
||||||
|
plugins.load.paths = paths;
|
||||||
|
setJson('plugins', plugins);
|
||||||
|
logSuccess(`Removed plugin path from ${PLUGINS_LOAD_PATHS}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// Step 5: Configuration
|
// Remove Plugin Files
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
async function configure(env) {
|
function removePluginFiles(env) {
|
||||||
if (options.buildOnly) {
|
logInfo('Removing plugin files...');
|
||||||
logStep(5, 'Skipping configuration (--build-only)');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
logStep(5, 'Configuration...');
|
const pluginDir = join(env.openclawDir, 'plugins', PLUGIN_NAME);
|
||||||
|
const passMgrBinary = join(env.openclawDir, 'bin', 'pass_mgr');
|
||||||
const installDir = env.openclawDir || join(homedir(), '.openclaw');
|
|
||||||
const passMgrPath = join(installDir, 'bin', 'pass_mgr');
|
|
||||||
|
|
||||||
// Check if already initialized
|
|
||||||
const adminKeyDir = join(homedir(), '.pass_mgr');
|
|
||||||
const configPath = join(adminKeyDir, 'config.json');
|
|
||||||
|
|
||||||
if (existsSync(configPath)) {
|
|
||||||
logSuccess('pass_mgr already initialized');
|
|
||||||
} else {
|
|
||||||
log(' pass_mgr not initialized yet.', 'yellow');
|
|
||||||
log(` Run "${passMgrPath} admin init" manually after installation.`, 'cyan');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update OpenClaw plugins.load.paths configuration
|
|
||||||
log('\n Updating OpenClaw plugin configuration...', 'blue');
|
|
||||||
|
|
||||||
|
// Remove plugin directory
|
||||||
|
if (existsSync(pluginDir)) {
|
||||||
try {
|
try {
|
||||||
const skillsPath = join(installDir, 'skills', 'paddedcell');
|
execSync(`rm -rf "${pluginDir}"`, { encoding: 'utf8' });
|
||||||
|
logSuccess(`Removed ${pluginDir}`);
|
||||||
// Get current plugins.load.paths using openclaw config
|
|
||||||
let currentPaths = [];
|
|
||||||
try {
|
|
||||||
const result = execSync('openclaw config get plugins.load.paths --json 2>/dev/null || echo "[]"', {
|
|
||||||
encoding: 'utf8',
|
|
||||||
cwd: __dirname
|
|
||||||
}).trim();
|
|
||||||
currentPaths = JSON.parse(result);
|
|
||||||
} catch {
|
|
||||||
currentPaths = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add paddedcell skills path if not already present
|
|
||||||
if (!currentPaths.includes(skillsPath)) {
|
|
||||||
currentPaths.push(skillsPath);
|
|
||||||
execSync(`openclaw config set plugins.load.paths --json '${JSON.stringify(currentPaths)}'`, {
|
|
||||||
cwd: __dirname
|
|
||||||
});
|
|
||||||
logSuccess(`Added plugin path to OpenClaw config: ${skillsPath}`);
|
|
||||||
} else {
|
|
||||||
log(' Plugin path already in OpenClaw config', 'green');
|
|
||||||
}
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logWarning(`Failed to update OpenClaw config: ${err.message}`);
|
logError(`Failed to remove ${pluginDir}: ${err.message}`);
|
||||||
log(' Please manually add the following to your OpenClaw config:', 'yellow');
|
|
||||||
log(` openclaw config set plugins.load.paths --json '["${join(installDir, 'skills', 'paddedcell')}"]'`, 'cyan');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================================
|
// Remove pass_mgr binary
|
||||||
// Step 6: Print Summary
|
|
||||||
// ============================================================================
|
|
||||||
|
|
||||||
function printSummary(env) {
|
|
||||||
logStep(6, 'Installation Summary');
|
|
||||||
|
|
||||||
const installDir = options.prefix || env.openclawDir;
|
|
||||||
const passMgrPath = join(installDir, 'bin', 'pass_mgr');
|
|
||||||
|
|
||||||
console.log('');
|
|
||||||
log('╔════════════════════════════════════════════════════════╗', 'cyan');
|
|
||||||
log('║ PaddedCell Installation Complete ║', 'cyan');
|
|
||||||
log('╚════════════════════════════════════════════════════════╝', 'cyan');
|
|
||||||
console.log('');
|
|
||||||
|
|
||||||
if (options.buildOnly) {
|
|
||||||
log('Build-only mode - binaries built but not installed', 'yellow');
|
|
||||||
console.log('');
|
|
||||||
log('Built artifacts:', 'blue');
|
|
||||||
log(` • pass_mgr: ${join(__dirname, 'pass_mgr', 'dist', 'pass_mgr')}`, 'reset');
|
|
||||||
log(` • pcexec: ${join(__dirname, 'pcexec', 'dist')}`, 'reset');
|
|
||||||
log(` • safe-restart: ${join(__dirname, 'safe-restart', 'dist')}`, 'reset');
|
|
||||||
} else {
|
|
||||||
log('Installed components:', 'blue');
|
|
||||||
log(` • pass_mgr binary: ${passMgrPath}`, 'reset');
|
|
||||||
log(` • pcexec module: ${join(installDir, 'skills', 'paddedcell', 'pcexec')}`, 'reset');
|
|
||||||
log(` • safe-restart module: ${join(installDir, 'skills', 'paddedcell', 'safe-restart')}`, 'reset');
|
|
||||||
console.log('');
|
|
||||||
|
|
||||||
log('Next steps:', 'blue');
|
|
||||||
console.log('');
|
|
||||||
log('1. Initialize pass_mgr (required before first use):', 'yellow');
|
|
||||||
log(` ${passMgrPath} admin init`, 'cyan');
|
|
||||||
console.log('');
|
|
||||||
log('2. Test pass_mgr:', 'yellow');
|
|
||||||
log(` ${passMgrPath} set test_key mypass # Set a test password`, 'cyan');
|
|
||||||
log(` ${passMgrPath} get test_key # Retrieve password`, 'cyan');
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('');
|
|
||||||
log('For more information:', 'blue');
|
|
||||||
log(' • Documentation: https://git.hangman-lab.top/nav/PaddedCell', 'cyan');
|
|
||||||
log(' • Issues: https://git.hangman-lab.top/nav/PaddedCell/issues', 'cyan');
|
|
||||||
console.log('');
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============================================================================
|
|
||||||
// Uninstall Function
|
|
||||||
// ============================================================================
|
|
||||||
|
|
||||||
async function uninstall(env) {
|
|
||||||
logStep(1, 'Uninstalling PaddedCell...');
|
|
||||||
|
|
||||||
const installDir = options.prefix || env.openclawDir || join(homedir(), '.openclaw');
|
|
||||||
const binDir = join(installDir, 'bin');
|
|
||||||
const skillsDir = join(installDir, 'skills', 'paddedcell');
|
|
||||||
|
|
||||||
const itemsToRemove = [];
|
|
||||||
|
|
||||||
// Check what exists
|
|
||||||
const passMgrBinary = join(binDir, 'pass_mgr');
|
|
||||||
if (existsSync(passMgrBinary)) {
|
if (existsSync(passMgrBinary)) {
|
||||||
itemsToRemove.push(passMgrBinary);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (existsSync(skillsDir)) {
|
|
||||||
itemsToRemove.push(skillsDir);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (itemsToRemove.length === 0) {
|
|
||||||
log('No installed components found.', 'yellow');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
log('The following items will be removed:', 'yellow');
|
|
||||||
for (const item of itemsToRemove) {
|
|
||||||
log(` • ${item}`, 'reset');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ask for confirmation
|
|
||||||
const confirm = await prompt('\nAre you sure you want to uninstall? (y/N): ');
|
|
||||||
if (confirm.toLowerCase() !== 'y') {
|
|
||||||
log('Uninstall cancelled.', 'yellow');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Perform uninstall
|
|
||||||
for (const item of itemsToRemove) {
|
|
||||||
try {
|
try {
|
||||||
if (item === passMgrBinary) {
|
execSync(`rm -f "${passMgrBinary}"`, { encoding: 'utf8' });
|
||||||
execSync(`rm -f "${item}"`, { silent: true });
|
logSuccess(`Removed ${passMgrBinary}`);
|
||||||
logSuccess(`Removed: ${item}`);
|
|
||||||
} else {
|
|
||||||
execSync(`rm -rf "${item}"`, { silent: true });
|
|
||||||
logSuccess(`Removed: ${item}`);
|
|
||||||
}
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logError(`Failed to remove: ${item}`);
|
logError(`Failed to remove ${passMgrBinary}: ${err.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove plugin path from OpenClaw config
|
|
||||||
log('\n Removing plugin path from OpenClaw config...', 'blue');
|
|
||||||
try {
|
|
||||||
const skillsPath = join(installDir, 'skills', 'paddedcell');
|
|
||||||
|
|
||||||
// Get current plugins.load.paths
|
|
||||||
let currentPaths = [];
|
|
||||||
try {
|
|
||||||
const result = execSync('openclaw config get plugins.load.paths --json 2>/dev/null || echo "[]"', {
|
|
||||||
encoding: 'utf8',
|
|
||||||
cwd: __dirname
|
|
||||||
}).trim();
|
|
||||||
currentPaths = JSON.parse(result);
|
|
||||||
} catch {
|
|
||||||
currentPaths = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove paddedcell skills path
|
|
||||||
const index = currentPaths.indexOf(skillsPath);
|
|
||||||
if (index > -1) {
|
|
||||||
currentPaths.splice(index, 1);
|
|
||||||
execSync(`openclaw config set plugins.load.paths --json '${JSON.stringify(currentPaths)}'`, {
|
|
||||||
cwd: __dirname
|
|
||||||
});
|
|
||||||
logSuccess(`Removed plugin path from OpenClaw config`);
|
|
||||||
} else {
|
|
||||||
log(' Plugin path not found in OpenClaw config', 'yellow');
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
logWarning(`Failed to update OpenClaw config: ${err.message}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for admin key directory
|
|
||||||
const adminKeyDir = join(homedir(), '.pass_mgr');
|
|
||||||
if (existsSync(adminKeyDir)) {
|
|
||||||
log('\n⚠️ Admin key directory found:', 'yellow');
|
|
||||||
log(` ${adminKeyDir}`, 'cyan');
|
|
||||||
log(' This contains your encryption keys. Remove manually if desired.', 'yellow');
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('');
|
|
||||||
log('╔════════════════════════════════════════════════════════╗', 'cyan');
|
|
||||||
log('║ PaddedCell Uninstall Complete ║', 'cyan');
|
|
||||||
log('╚════════════════════════════════════════════════════════╝', 'cyan');
|
|
||||||
console.log('');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// Main
|
// Main
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
async function main() {
|
function main() {
|
||||||
console.log('');
|
|
||||||
log('╔════════════════════════════════════════════════════════╗', 'cyan');
|
|
||||||
log('║ PaddedCell Plugin Installer v0.1.0 ║', 'cyan');
|
|
||||||
log('╚════════════════════════════════════════════════════════╝', 'cyan');
|
|
||||||
console.log('');
|
|
||||||
|
|
||||||
try {
|
|
||||||
const env = detectEnvironment();
|
const env = detectEnvironment();
|
||||||
|
|
||||||
// Handle uninstall
|
if (mode === 'install') {
|
||||||
if (options.uninstall) {
|
logInfo('Starting installation...');
|
||||||
await uninstall(env);
|
|
||||||
process.exit(0);
|
try {
|
||||||
}
|
// Build components
|
||||||
|
buildComponents(env);
|
||||||
|
|
||||||
|
// Install plugin files
|
||||||
|
const { pluginDir, passMgrPath } = installPlugin(env);
|
||||||
|
|
||||||
|
// Configure OpenClaw
|
||||||
|
const delta = { added: {}, replaced: {}, removed: {} };
|
||||||
|
configureOpenClaw(env, pluginDir, delta);
|
||||||
|
|
||||||
|
logInfo('Installation complete!');
|
||||||
|
console.log('');
|
||||||
|
log('Next steps:', 'cyan');
|
||||||
|
console.log('');
|
||||||
|
log('1. Initialize pass_mgr (required before first use):', 'yellow');
|
||||||
|
log(` ${passMgrPath} admin init`, 'cyan');
|
||||||
|
console.log('');
|
||||||
|
log('2. Test pass_mgr:', 'yellow');
|
||||||
|
log(` ${passMgrPath} set test_key mypass`, 'cyan');
|
||||||
|
log(` ${passMgrPath} get test_key`, 'cyan');
|
||||||
|
console.log('');
|
||||||
|
log('3. Restart OpenClaw gateway to apply changes:', 'yellow');
|
||||||
|
log(' openclaw gateway restart', 'cyan');
|
||||||
|
|
||||||
checkDependencies(env);
|
|
||||||
await buildComponents(env);
|
|
||||||
await installComponents(env);
|
|
||||||
await configure(env);
|
|
||||||
printSummary(env);
|
|
||||||
process.exit(0);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log('');
|
logError(`Installation failed: ${err.message}`);
|
||||||
log('╔════════════════════════════════════════════════════════╗', 'red');
|
|
||||||
log('║ Installation Failed ║', 'red');
|
|
||||||
log('╚════════════════════════════════════════════════════════╝', 'red');
|
|
||||||
console.log('');
|
|
||||||
log(`Error: ${err.message}`, 'red');
|
|
||||||
if (options.verbose) {
|
|
||||||
console.log(err.stack);
|
|
||||||
}
|
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
logInfo('Starting uninstallation...');
|
||||||
|
|
||||||
|
// For uninstall, we need the delta from install
|
||||||
|
// For simplicity, we'll track what we added and remove it
|
||||||
|
const delta = {
|
||||||
|
added: {},
|
||||||
|
replaced: {},
|
||||||
|
removed: {}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Assume we added these during install
|
||||||
|
const pluginDir = join(env.openclawDir, 'plugins', PLUGIN_NAME);
|
||||||
|
delta.added[PLUGIN_ALLOW_PATH] = PLUGIN_NAME;
|
||||||
|
delta.added[PLUGIN_ENTRY] = { enabled: true };
|
||||||
|
delta.added[PLUGINS_LOAD_PATHS] = pluginDir;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Unconfigure OpenClaw
|
||||||
|
unconfigureOpenClaw(env, delta);
|
||||||
|
|
||||||
|
// Remove plugin files
|
||||||
|
removePluginFiles(env);
|
||||||
|
|
||||||
|
logInfo('Uninstallation complete!');
|
||||||
|
console.log('');
|
||||||
|
log('Restart OpenClaw gateway to apply changes:', 'yellow');
|
||||||
|
log(' openclaw gateway restart', 'cyan');
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
logError(`Uninstallation failed: ${err.message}`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
main();
|
main();
|
||||||
|
|||||||
Reference in New Issue
Block a user