"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.upgrade = upgrade;
const node_child_process_1 = require("node:child_process");
const UPGRADE_COMMAND = ['install', '-g', '@playdrop/playdrop-cli@latest'];
async function upgrade(dryRun = false) {
    const commandString = `npm ${UPGRADE_COMMAND.join(' ')}`;
    if (dryRun) {
        console.log(commandString);
        return;
    }
    console.log('Running:', commandString);
    await new Promise((resolve, reject) => {
        const child = (0, node_child_process_1.spawn)('npm', UPGRADE_COMMAND, {
            stdio: 'inherit',
            shell: process.platform === 'win32'
        });
        child.on('close', (code) => {
            if (code === 0) {
                resolve();
            }
            else {
                reject(new Error(`npm exited with code ${code}`));
            }
        });
        child.on('error', (error) => reject(error));
    });
}
