Skip to content
Merged
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
68 changes: 30 additions & 38 deletions scripts/npm-postinstall.cjs
Original file line number Diff line number Diff line change
@@ -1,59 +1,51 @@
/**
* npm postinstall script for aidevops
* Runs setup.sh to deploy agents after npm install -g
*
* The npm package contains only the CLI wrapper. The full agent files
* are deployed from ~/Git/aidevops via `aidevops update`.
*/

const { execSync } = require('child_process');
const path = require('path');
const fs = require('fs');
const os = require('os');
const path = require('path');

const packageDir = path.resolve(__dirname, '..');
const setupScript = path.join(packageDir, 'setup.sh');
const agentsDir = path.join(os.homedir(), '.aidevops', 'agents');
const versionFile = path.join(agentsDir, 'VERSION');

// Check if this is a global install
const isGlobalInstall = process.env.npm_config_global === 'true';

// Skip if not global install (local dev doesn't need postinstall)
if (!isGlobalInstall && fs.existsSync(agentsDir)) {
console.log('aidevops: Local install detected, skipping setup (agents already deployed)');
process.exit(0);
// Check current installed version
let installedVersion = 'not installed';
if (fs.existsSync(versionFile)) {
installedVersion = fs.readFileSync(versionFile, 'utf8').trim();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fs.readFileSync(versionFile, ...) can still throw even if existsSync returned true (e.g., permission issues/broken symlink), which would fail the entire npm install because this postinstall script isn’t guarded. Consider wrapping version detection in a try/catch and falling back to not installed to keep installs resilient.

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎

}
Comment on lines +17 to 19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The script could be more robust by handling potential errors when reading the version file. While fs.existsSync checks for the file, fs.readFileSync could still fail due to permissions or other file system issues. An unhandled exception would print a stack trace, which is not ideal for a post-install script. Wrapping this logic in a try...catch block would gracefully handle such errors and improve the user experience.

try {
    if (fs.existsSync(versionFile)) {
        installedVersion = fs.readFileSync(versionFile, 'utf8').trim();
    }
} catch (error) {
    // If reading the version fails, we can't determine the version.
    // We'll proceed as if it's not installed and log a warning.
    console.warn(`aidevops: Could not read agents version file: ${error.message}`);
}


// Check if setup.sh exists
if (!fs.existsSync(setupScript)) {
console.log('aidevops: setup.sh not found, skipping postinstall');
process.exit(0);
}
// Get package version
const packageJson = require('../package.json');
const packageVersion = packageJson.version;

console.log('aidevops: Running setup to deploy agents...');
console.log('');
console.log('aidevops CLI installed successfully!');
console.log('');
console.log(` CLI version: ${packageVersion}`);
console.log(` Agents version: ${installedVersion}`);
console.log('');

try {
// Run setup.sh non-interactively
execSync(`bash "${setupScript}"`, {
stdio: 'inherit',
cwd: packageDir,
env: {
...process.env,
// Skip interactive prompts
AIDEVOPS_NONINTERACTIVE: '1'
}
});

if (installedVersion === 'not installed') {
console.log('To complete installation, run:');
console.log('');
console.log('aidevops installed successfully!');
console.log(' aidevops update');
console.log('');
console.log('This will clone the repository and deploy agents to ~/.aidevops/agents/');
} else if (installedVersion !== packageVersion) {
console.log('To update agents to match CLI version, run:');
console.log('');
console.log(' aidevops update');
console.log('');
} else {
console.log('CLI and agents are in sync. Ready to use!');
console.log('');
console.log('Quick start:');
console.log(' aidevops status # Check installation');
console.log(' aidevops init # Initialize in a project');
console.log(' aidevops help # Show all commands');
console.log('');
} catch (error) {
console.error('aidevops: Setup encountered issues (non-critical)');
console.error(`Run manually: bash "${setupScript}"`);
console.error('Or reinstall with: bash <(curl -fsSL https://aidevops.sh)');
// Don't fail the install
process.exit(0);
}
console.log('');
Loading