226
install.mjs
226
install.mjs
@@ -1,18 +1,20 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* PaddedCell Plugin Installer v0.2.0
|
||||
*
|
||||
* Usage:
|
||||
* node install.mjs
|
||||
* node install.mjs --openclaw-profile-path /path/to/.openclaw
|
||||
* node install.mjs --build-only
|
||||
* node install.mjs --skip-check
|
||||
* node install.mjs --uninstall
|
||||
* PaddedCell Plugin Installer v0.3.0
|
||||
*/
|
||||
|
||||
import { execSync } from 'child_process';
|
||||
import { existsSync, mkdirSync, copyFileSync, chmodSync, readdirSync, rmSync, readFileSync, writeFileSync } from 'fs';
|
||||
import {
|
||||
existsSync,
|
||||
mkdirSync,
|
||||
copyFileSync,
|
||||
chmodSync,
|
||||
readdirSync,
|
||||
rmSync,
|
||||
readFileSync,
|
||||
writeFileSync,
|
||||
} from 'fs';
|
||||
import { randomBytes } from 'crypto';
|
||||
import { dirname, join, resolve } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
@@ -24,7 +26,6 @@ const __dirname = resolve(dirname(__filename));
|
||||
const PLUGIN_NAME = 'padded-cell';
|
||||
const SRC_DIST_DIR = join(__dirname, 'dist', PLUGIN_NAME);
|
||||
|
||||
// Parse arguments
|
||||
const args = process.argv.slice(2);
|
||||
const options = {
|
||||
openclawProfilePath: null,
|
||||
@@ -32,22 +33,20 @@ const options = {
|
||||
skipCheck: args.includes('--skip-check'),
|
||||
verbose: args.includes('--verbose') || args.includes('-v'),
|
||||
uninstall: args.includes('--uninstall'),
|
||||
installOnly: args.includes('--install'),
|
||||
};
|
||||
|
||||
// Parse --openclaw-profile-path value
|
||||
const profileIdx = args.indexOf('--openclaw-profile-path');
|
||||
if (profileIdx !== -1 && args[profileIdx + 1]) {
|
||||
options.openclawProfilePath = resolve(args[profileIdx + 1]);
|
||||
}
|
||||
|
||||
// Resolve openclaw path: --openclaw-profile-path → $OPENCLAW_PATH → ~/.openclaw
|
||||
function resolveOpenclawPath() {
|
||||
if (options.openclawProfilePath) return options.openclawProfilePath;
|
||||
if (process.env.OPENCLAW_PATH) return resolve(process.env.OPENCLAW_PATH);
|
||||
return join(homedir(), '.openclaw');
|
||||
}
|
||||
|
||||
// Colors
|
||||
const c = {
|
||||
reset: '\x1b[0m', red: '\x1b[31m', green: '\x1b[32m',
|
||||
yellow: '\x1b[33m', blue: '\x1b[34m', cyan: '\x1b[36m',
|
||||
@@ -86,26 +85,20 @@ function copyDir(src, dest) {
|
||||
for (const entry of readdirSync(src, { withFileTypes: true })) {
|
||||
const s = join(src, entry.name);
|
||||
const d = join(dest, entry.name);
|
||||
if (entry.name === 'node_modules') continue; // skip node_modules
|
||||
if (entry.name === 'node_modules') continue;
|
||||
entry.isDirectory() ? copyDir(s, d) : copyFileSync(s, d);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Step 1: Detect ──────────────────────────────────────────────────────
|
||||
|
||||
function detectEnvironment() {
|
||||
logStep(1, 6, 'Detecting environment...');
|
||||
const env = { platform: platform(), nodeVersion: null, goVersion: null };
|
||||
|
||||
try { env.nodeVersion = exec('node --version', { silent: true }).trim(); logOk(`Node.js ${env.nodeVersion}`); } catch { logErr('Node.js not found'); }
|
||||
try { env.goVersion = exec('go version', { silent: true }).trim(); logOk(`Go: ${env.goVersion}`); } catch { logErr('Go not found'); }
|
||||
try { logOk(`openclaw at ${exec('which openclaw', { silent: true }).trim()}`); } catch { logWarn('openclaw CLI not in PATH'); }
|
||||
|
||||
return env;
|
||||
}
|
||||
|
||||
// ── Step 2: Check deps ──────────────────────────────────────────────────
|
||||
|
||||
function checkDeps(env) {
|
||||
if (options.skipCheck) { logStep(2, 6, 'Skipping dep checks'); return; }
|
||||
logStep(2, 6, 'Checking dependencies...');
|
||||
@@ -116,8 +109,6 @@ function checkDeps(env) {
|
||||
logOk('All deps OK');
|
||||
}
|
||||
|
||||
// ── Step 3: Build ───────────────────────────────────────────────────────
|
||||
|
||||
function ensureBuildSecret() {
|
||||
const secretFile = join(__dirname, '.build-secret');
|
||||
if (existsSync(secretFile)) {
|
||||
@@ -136,10 +127,10 @@ function ensureBuildSecret() {
|
||||
async function build() {
|
||||
logStep(3, 6, 'Building components...');
|
||||
|
||||
// Generate / load build secret for pass_mgr
|
||||
const buildSecret = ensureBuildSecret();
|
||||
|
||||
// pass_mgr (Go)
|
||||
rmSync(SRC_DIST_DIR, { recursive: true, force: true });
|
||||
|
||||
log(' Building pass_mgr...', 'blue');
|
||||
const pmDir = join(__dirname, 'pass_mgr');
|
||||
exec('go mod tidy', { cwd: pmDir, silent: !options.verbose });
|
||||
@@ -148,7 +139,6 @@ async function build() {
|
||||
chmodSync(join(pmDir, 'dist', 'pass_mgr'), 0o755);
|
||||
logOk('pass_mgr');
|
||||
|
||||
// pcguard (Go)
|
||||
log(' Building pcguard...', 'blue');
|
||||
const pgDir = join(__dirname, 'pcguard');
|
||||
exec('go mod tidy', { cwd: pgDir, silent: !options.verbose });
|
||||
@@ -156,15 +146,85 @@ async function build() {
|
||||
chmodSync(join(pgDir, 'dist', 'pcguard'), 0o755);
|
||||
logOk('pcguard');
|
||||
|
||||
// Plugin (TypeScript)
|
||||
log(' Building plugin...', 'blue');
|
||||
const pluginDir = join(__dirname, 'plugin');
|
||||
exec('npm install', { cwd: pluginDir, silent: !options.verbose });
|
||||
exec('npx tsc', { cwd: pluginDir, silent: !options.verbose });
|
||||
logOk('plugin');
|
||||
|
||||
const skillsSrc = join(__dirname, 'skills');
|
||||
const skillsDist = join(SRC_DIST_DIR, 'skills');
|
||||
if (existsSync(skillsSrc)) {
|
||||
copyDir(skillsSrc, skillsDist);
|
||||
logOk('skills copied to dist/padded-cell/skills');
|
||||
}
|
||||
}
|
||||
|
||||
// ── Step 4: Install ─────────────────────────────────────────────────────
|
||||
function handoffSecretIfPossible(openclawPath) {
|
||||
const passMgrPath = join(openclawPath, 'bin', 'pass_mgr');
|
||||
if (!existsSync(passMgrPath)) return null;
|
||||
|
||||
const storeA = join(openclawPath, 'pc-pass-store');
|
||||
const storeB = join(openclawPath, 'pc-secret-store');
|
||||
if (!existsSync(storeA) && !existsSync(storeB)) return null;
|
||||
|
||||
const secretFile = join(openclawPath, 'pc-pass-store.secret');
|
||||
try {
|
||||
exec(`${passMgrPath} admin handoff ${secretFile}`, { silent: !options.verbose });
|
||||
logOk(`handoff secret → ${secretFile}`);
|
||||
return secretFile;
|
||||
} catch (err) {
|
||||
logWarn(`handoff failed: ${err.message}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function clearInstallTargets(openclawPath) {
|
||||
const binDir = join(openclawPath, 'bin');
|
||||
for (const name of ['pass_mgr', 'pcguard']) {
|
||||
const p = join(binDir, name);
|
||||
if (existsSync(p)) { rmSync(p, { force: true }); logOk(`Removed ${p}`); }
|
||||
}
|
||||
|
||||
const destDir = join(openclawPath, 'plugins', PLUGIN_NAME);
|
||||
if (existsSync(destDir)) { rmSync(destDir, { recursive: true, force: true }); logOk(`Removed ${destDir}`); }
|
||||
}
|
||||
|
||||
function cleanupConfig(openclawPath) {
|
||||
const destDir = join(openclawPath, 'plugins', PLUGIN_NAME);
|
||||
const skillsDir = join(openclawPath, 'skills');
|
||||
try {
|
||||
const allow = getOpenclawConfig('plugins.allow', []);
|
||||
const idx = allow.indexOf(PLUGIN_NAME);
|
||||
if (idx !== -1) {
|
||||
allow.splice(idx, 1);
|
||||
setOpenclawConfig('plugins.allow', allow);
|
||||
logOk('Removed from allow list');
|
||||
}
|
||||
|
||||
unsetOpenclawConfig(`plugins.entries.${PLUGIN_NAME}`);
|
||||
logOk('Removed plugin entry');
|
||||
|
||||
const paths = getOpenclawConfig('plugins.load.paths', []);
|
||||
const pidx = paths.indexOf(destDir);
|
||||
if (pidx !== -1) {
|
||||
paths.splice(pidx, 1);
|
||||
setOpenclawConfig('plugins.load.paths', paths);
|
||||
logOk('Removed from load paths');
|
||||
}
|
||||
|
||||
const skillEntries = ['pcexec', 'safe-restart', 'safe_restart', 'pass-mgr'];
|
||||
for (const sk of skillEntries) {
|
||||
const p = join(skillsDir, sk);
|
||||
if (existsSync(p)) {
|
||||
rmSync(p, { recursive: true, force: true });
|
||||
logOk(`Removed skill ${p}`);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
logWarn(`Config cleanup: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function install() {
|
||||
if (options.buildOnly) { logStep(4, 6, 'Skipping install (--build-only)'); return null; }
|
||||
@@ -174,23 +234,29 @@ async function install() {
|
||||
const binDir = join(openclawPath, 'bin');
|
||||
const pluginsDir = join(openclawPath, 'plugins');
|
||||
const destDir = join(pluginsDir, PLUGIN_NAME);
|
||||
const skillsDir = join(openclawPath, 'skills');
|
||||
const distSkillsDir = join(SRC_DIST_DIR, 'skills');
|
||||
|
||||
log(` OpenClaw path: ${openclawPath}`, 'blue');
|
||||
|
||||
// Copy dist/padded-cell → plugins/padded-cell
|
||||
// update/reinstall path: remove old install first
|
||||
if (existsSync(destDir) || existsSync(join(binDir, 'pass_mgr')) || existsSync(join(binDir, 'pcguard'))) {
|
||||
logWarn('Existing install detected, uninstalling before install...');
|
||||
handoffSecretIfPossible(openclawPath);
|
||||
clearInstallTargets(openclawPath);
|
||||
cleanupConfig(openclawPath);
|
||||
}
|
||||
|
||||
if (existsSync(destDir)) rmSync(destDir, { recursive: true, force: true });
|
||||
copyDir(SRC_DIST_DIR, destDir);
|
||||
|
||||
// Copy openclaw.plugin.json and package.json
|
||||
copyFileSync(join(__dirname, 'plugin', 'openclaw.plugin.json'), join(destDir, 'openclaw.plugin.json'));
|
||||
copyFileSync(join(__dirname, 'plugin', 'package.json'), join(destDir, 'package.json'));
|
||||
logOk(`Plugin files → ${destDir}`);
|
||||
|
||||
// Install runtime deps into dest (express, ws)
|
||||
exec('npm install --omit=dev', { cwd: destDir, silent: !options.verbose });
|
||||
logOk('Runtime deps installed');
|
||||
|
||||
// Binaries
|
||||
mkdirSync(binDir, { recursive: true });
|
||||
const bins = [
|
||||
{ name: 'pass_mgr', src: join(__dirname, 'pass_mgr', 'dist', 'pass_mgr') },
|
||||
@@ -203,11 +269,34 @@ async function install() {
|
||||
logOk(`${b.name} → ${dest}`);
|
||||
}
|
||||
|
||||
// Only copy dist/padded-cell/skills to ~/.openclaw/skills
|
||||
mkdirSync(skillsDir, { recursive: true });
|
||||
if (existsSync(distSkillsDir)) {
|
||||
for (const entry of readdirSync(distSkillsDir, { withFileTypes: true })) {
|
||||
const s = join(distSkillsDir, entry.name);
|
||||
const d = join(skillsDir, entry.name);
|
||||
rmSync(d, { recursive: true, force: true });
|
||||
entry.isDirectory() ? copyDir(s, d) : copyFileSync(s, d);
|
||||
logOk(`skill synced → ${d}`);
|
||||
}
|
||||
}
|
||||
|
||||
// if prior encrypted store exists, run init-from once new binary is installed
|
||||
const hasStore = existsSync(join(openclawPath, 'pc-pass-store')) || existsSync(join(openclawPath, 'pc-secret-store'));
|
||||
const secretFile = join(openclawPath, 'pc-pass-store.secret');
|
||||
if (hasStore && existsSync(secretFile)) {
|
||||
const passMgrPath = join(binDir, 'pass_mgr');
|
||||
try {
|
||||
exec(`${passMgrPath} admin init-from ${secretFile}`, { silent: !options.verbose });
|
||||
logOk('init-from completed from handoff secret');
|
||||
} catch (err) {
|
||||
logWarn(`init-from failed: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
return { binDir, destDir };
|
||||
}
|
||||
|
||||
// ── Step 5: Configure ───────────────────────────────────────────────────
|
||||
|
||||
async function configure() {
|
||||
if (options.buildOnly) { logStep(5, 6, 'Skipping config'); return; }
|
||||
logStep(5, 6, 'Configuring OpenClaw...');
|
||||
@@ -217,17 +306,14 @@ async function configure() {
|
||||
const passMgrPath = join(openclawPath, 'bin', 'pass_mgr');
|
||||
|
||||
try {
|
||||
// plugins.load.paths
|
||||
const paths = getOpenclawConfig('plugins.load.paths', []);
|
||||
if (!paths.includes(destDir)) { paths.push(destDir); setOpenclawConfig('plugins.load.paths', paths); }
|
||||
logOk(`plugins.load.paths includes ${destDir}`);
|
||||
|
||||
// plugins.allow
|
||||
const allow = getOpenclawConfig('plugins.allow', []);
|
||||
if (!allow.includes(PLUGIN_NAME)) { allow.push(PLUGIN_NAME); setOpenclawConfig('plugins.allow', allow); }
|
||||
logOk(`plugins.allow includes ${PLUGIN_NAME}`);
|
||||
|
||||
// plugins.entries
|
||||
const plugins = getOpenclawConfig('plugins', {});
|
||||
plugins.entries = plugins.entries || {};
|
||||
plugins.entries[PLUGIN_NAME] = {
|
||||
@@ -239,22 +325,13 @@ async function configure() {
|
||||
} catch (err) {
|
||||
logWarn(`Config failed: ${err.message}`);
|
||||
}
|
||||
|
||||
// Check pass_mgr init
|
||||
if (existsSync(join(homedir(), '.pass_mgr', 'config.json'))) {
|
||||
logOk('pass_mgr already initialized');
|
||||
} else {
|
||||
logWarn(`pass_mgr not initialized — run: ${passMgrPath} admin init`);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Step 6: Summary ─────────────────────────────────────────────────────
|
||||
|
||||
function summary(result) {
|
||||
function summary() {
|
||||
logStep(6, 6, 'Done!');
|
||||
console.log('');
|
||||
log('╔══════════════════════════════════════════════╗', 'cyan');
|
||||
log('║ PaddedCell v0.2.0 Install Complete ║', 'cyan');
|
||||
log('║ PaddedCell v0.3.0 Install Complete ║', 'cyan');
|
||||
log('╚══════════════════════════════════════════════╝', 'cyan');
|
||||
|
||||
if (options.buildOnly) {
|
||||
@@ -265,63 +342,42 @@ function summary(result) {
|
||||
console.log('');
|
||||
log('Next steps:', 'blue');
|
||||
log(' 1. openclaw gateway restart', 'cyan');
|
||||
|
||||
const openclawPath = resolveOpenclawPath();
|
||||
const pmPath = join(openclawPath, 'bin', 'pass_mgr');
|
||||
if (!existsSync(join(homedir(), '.pass_mgr', 'config.json'))) {
|
||||
log(` 2. ${pmPath} admin init`, 'cyan');
|
||||
}
|
||||
console.log('');
|
||||
}
|
||||
|
||||
// ── Uninstall ───────────────────────────────────────────────────────────
|
||||
|
||||
async function uninstall() {
|
||||
log('Uninstalling PaddedCell...', 'cyan');
|
||||
const openclawPath = resolveOpenclawPath();
|
||||
|
||||
// Remove binaries
|
||||
for (const name of ['pass_mgr', 'pcguard']) {
|
||||
const p = join(openclawPath, 'bin', name);
|
||||
if (existsSync(p)) { rmSync(p); logOk(`Removed ${p}`); }
|
||||
}
|
||||
|
||||
// Remove plugin dir
|
||||
const destDir = join(openclawPath, 'plugins', PLUGIN_NAME);
|
||||
if (existsSync(destDir)) { rmSync(destDir, { recursive: true }); logOk(`Removed ${destDir}`); }
|
||||
|
||||
// Remove config
|
||||
try {
|
||||
const allow = getOpenclawConfig('plugins.allow', []);
|
||||
const idx = allow.indexOf(PLUGIN_NAME);
|
||||
if (idx !== -1) { allow.splice(idx, 1); setOpenclawConfig('plugins.allow', allow); logOk('Removed from allow list'); }
|
||||
unsetOpenclawConfig(`plugins.entries.${PLUGIN_NAME}`);
|
||||
logOk('Removed plugin entry');
|
||||
const paths = getOpenclawConfig('plugins.load.paths', []);
|
||||
const pidx = paths.indexOf(destDir);
|
||||
if (pidx !== -1) { paths.splice(pidx, 1); setOpenclawConfig('plugins.load.paths', paths); logOk('Removed from load paths'); }
|
||||
} catch (err) { logWarn(`Config cleanup: ${err.message}`); }
|
||||
|
||||
handoffSecretIfPossible(openclawPath);
|
||||
clearInstallTargets(openclawPath);
|
||||
cleanupConfig(openclawPath);
|
||||
log('\nRun: openclaw gateway restart', 'yellow');
|
||||
}
|
||||
|
||||
// ── Main ────────────────────────────────────────────────────────────────
|
||||
|
||||
async function main() {
|
||||
console.log('');
|
||||
log('╔══════════════════════════════════════════════╗', 'cyan');
|
||||
log('║ PaddedCell Plugin Installer v0.2.0 ║', 'cyan');
|
||||
log('║ PaddedCell Plugin Installer v0.3.0 ║', 'cyan');
|
||||
log('╚══════════════════════════════════════════════╝', 'cyan');
|
||||
console.log('');
|
||||
|
||||
try {
|
||||
const env = detectEnvironment();
|
||||
if (options.uninstall) { await uninstall(); process.exit(0); }
|
||||
|
||||
if (options.uninstall) {
|
||||
await uninstall();
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
checkDeps(env);
|
||||
await build();
|
||||
const result = await install();
|
||||
await configure();
|
||||
summary(result);
|
||||
|
||||
if (!options.buildOnly) {
|
||||
await install();
|
||||
await configure();
|
||||
}
|
||||
|
||||
summary();
|
||||
} catch (err) {
|
||||
log(`\nInstallation failed: ${err.message}`, 'red');
|
||||
process.exit(1);
|
||||
|
||||
Reference in New Issue
Block a user