All files / agents installer.ts

87.5% Statements 21/24
60% Branches 6/10
87.5% Functions 7/8
90.47% Lines 19/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55        7x       2x 2x 2x   1x         5x   5x                   5x       5x 5x 5x   5x 4x 2x       2x   2x 2x 2x       5x      
import { execFileSync, spawn } from 'node:child_process';
import * as path from 'node:path';
 
function isWindows(): boolean {
    return process.platform === 'win32';
}
 
export async function checkCopilotCli(): Promise<boolean> {
    try {
        execFileSync('copilot', ['--version'], { stdio: 'ignore' });
        return true;
    } catch {
        return false;
    }
}
 
export async function installCopilotCli(): Promise<void> {
    return new Promise((resolve, reject) => {
        // Spawn the binary directly with shell:false on both platforms
        const [command, args] = isWindows()
            ? ['winget', [
                'install', 'GitHub.Copilot',
                '--silent',
                '--accept-package-agreements',
                '--accept-source-agreements',
                '--disable-interactivity',
            ]]
            : ['bash', ['-c', 'curl -fsSL https://gh.io/copilot-install | bash']] as const;
 
        const proc = spawn(command, args as string[], { shell: false, stdio: 'pipe' });
 
        // Buffer all output silently; only surfaces on failure for diagnostics.
        // Avoids piping winget's CR-delimited progress bars into Azure Pipelines logs.
        const outputChunks: Buffer[] = [];
        proc.stdout?.on('data', (data: Buffer) => outputChunks.push(data));
        proc.stderr?.on('data', (data: Buffer) => outputChunks.push(data));
 
        proc.on('close', (code) => {
            if (code === 0) {
                Iif (!isWindows()) {
                    const localBin = path.join(process.env['HOME'] ?? '', '.local', 'bin');
                    process.env['PATH'] = `${localBin}${path.delimiter}${process.env['PATH']}`;
                }
                resolve();
            } else {
                const diagnostics = Buffer.concat(outputChunks).toString('utf8').trim();
                const detail = diagnostics ? `\n${diagnostics}` : '';
                reject(new Error(`Failed to install GitHub Copilot CLI (exit code: ${code})${detail}`));
            }
        });
 
        proc.on('error', (err) => reject(new Error(`Failed to install GitHub Copilot CLI: ${err.message}`)));
    });
}