Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions apps/studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"strip-ansi": "^7.1.2",
"tar": "^7.5.16",
"tus-js-client": "^4.3.1",
"winreg": "1.2.4",
"yauzl": "^3.2.1",
"zod": "^4.0.0"
},
Expand Down Expand Up @@ -81,7 +80,6 @@
"@types/react-dom": "^19.2.3",
"@types/semver": "^7.7.1",
"@types/shell-quote": "^1.7.5",
"@types/winreg": "^1.2.36",
"@types/yauzl": "^2.10.3",
"@vitejs/plugin-react": "^6.0.2",
"@wordpress/components": "34.0.0",
Expand Down
57 changes: 34 additions & 23 deletions apps/studio/src/modules/cli/lib/windows-installation-manager.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
import { app, dialog } from 'electron';
import { mkdir, rm, writeFile } from 'fs/promises';
import { spawn } from 'node:child_process';
import { existsSync } from 'node:fs';
import path from 'path';
import * as Sentry from '@sentry/electron/main';
import { __ } from '@wordpress/i18n';
import Registry from 'winreg'; // don't update winreg to 1.2.5 - https://github.com/fresc81/node-winreg/issues/65
import { getMainWindow } from 'src/main-window';
import { StudioCliInstallationManager } from 'src/modules/cli/lib/ipc-handlers';
import { loadUserData, updateAppdata } from 'src/storage/user-data';

// `STABLE_BIN_DIR_PATH` resolves to C:\Users\<USERNAME>\AppData\Local\studio\bin
export const STABLE_BIN_DIR_PATH = path.resolve( path.dirname( app.getPath( 'exe' ) ), '../bin' );

const PATH_KEY = 'Path';

const currentUserRegistry = new Registry( {
hive: Registry.HKCU,
key: '\\Environment',
} );

export class WindowsCliInstallationManager implements StudioCliInstallationManager {
constructor() {
if ( process.platform !== 'win32' ) {
Expand Down Expand Up @@ -118,28 +111,46 @@ export class WindowsCliInstallationManager implements StudioCliInstallationManag
}
}

private getPathFromRegistry(): Promise< string > {
private runPowerShell( script: string ): Promise< string > {
return new Promise( ( resolve, reject ) => {
currentUserRegistry.get( PATH_KEY, ( error, item ) => {
if ( error ) {
return reject( error );
}
const child = spawn(
'powershell.exe',
[ '-NoProfile', '-NonInteractive', '-WindowStyle', 'Hidden', '-Command', script ],
{ windowsHide: true }
);

resolve( item?.value || '' );
let stdout = '';
child.stdout.on( 'data', ( chunk ) => {
stdout += chunk.toString();
} );
child.on( 'error', reject );
child.on( 'exit', ( code ) =>
code === 0
? resolve( stdout )
: reject( new Error( `PowerShell exited with code ${ code }` ) )
);
} );
}

private setPathInRegistry( updatedPath: string ): Promise< void > {
return new Promise( ( resolve, reject ) => {
currentUserRegistry.set( PATH_KEY, Registry.REG_EXPAND_SZ, updatedPath, ( error ) => {
if ( error ) {
return reject( error );
}
private async getPathFromRegistry(): Promise< string > {
// Read the raw, unexpanded value (DoNotExpandEnvironmentNames) so that any
// %VAR% entries survive a read/modify/write round-trip untouched.
const script = `
$key = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey('Environment')
if ($key) {
[Console]::Out.Write([string]$key.GetValue('Path', '', [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames))
$key.Close()
}`;
return ( await this.runPowerShell( script ) ).trim();
}

resolve();
} );
} );
private async setPathInRegistry( updatedPath: string ): Promise< void > {
// SetEnvironmentVariable(..., 'User') also broadcasts WM_SETTINGCHANGE so
// open shells pick up the new PATH without a re-login.
const escaped = updatedPath.replace( /'/g, "''" );
await this.runPowerShell(
`[Environment]::SetEnvironmentVariable('PATH', '${ escaped }', 'User')`
);
}

private async isStudioCliDirInPath(): Promise< boolean > {
Expand Down
26 changes: 0 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading