11import { runPs , isAdmin } from './powershell.js' ;
22import { SYSTEM_PATHS , USER_PATHS , ENV_VARS } from './paths.js' ;
33import { logger } from '../core/logger.js' ;
4+ import fs from 'fs-extra' ;
45
56export async function setEnv ( name : string , value : string , scope : 'Machine' | 'User' ) {
67 if ( scope === 'Machine' && ! await isAdmin ( ) ) {
@@ -10,22 +11,68 @@ export async function setEnv(name: string, value: string, scope: 'Machine' | 'Us
1011 await runPs ( `[Environment]::SetEnvironmentVariable("${ name } ", "${ value } ", "${ scope } ")` ) ;
1112}
1213
13- export async function setupEnvironment ( scope : 'Machine' | 'User' = 'User' ) {
14+ export async function setupEnvironment ( scope : 'Machine' | 'User' = 'User' , force : boolean = false ) {
1415 logger . info ( `Configuring ${ scope } environment variables...` ) ;
1516 const paths = scope === 'Machine' ? SYSTEM_PATHS : USER_PATHS ;
1617
17- await setEnv ( ENV_VARS . HOME , paths . HOME , scope ) ;
18- await setEnv ( ENV_VARS . LOGS , paths . LOGS , scope ) ;
19- await setEnv ( ENV_VARS . PROXY_PATH , paths . PROXY_EXE , scope ) ;
18+ const currentHome = await runPs ( `[Environment]::GetEnvironmentVariable("${ ENV_VARS . HOME } ", "${ scope } ")` ) ;
19+ if ( force || ! currentHome ) {
20+ await setEnv ( ENV_VARS . HOME , paths . HOME , scope ) ;
21+ }
22+
23+ const currentLogs = await runPs ( `[Environment]::GetEnvironmentVariable("${ ENV_VARS . LOGS } ", "${ scope } ")` ) ;
24+ if ( force || ! currentLogs ) {
25+ await setEnv ( ENV_VARS . LOGS , paths . LOGS , scope ) ;
26+ }
27+
28+ const currentProxy = await runPs ( `[Environment]::GetEnvironmentVariable("${ ENV_VARS . PROXY_PATH } ", "${ scope } ")` ) ;
29+ if ( force || ! currentProxy ) {
30+ await setEnv ( ENV_VARS . PROXY_PATH , paths . PROXY_EXE , scope ) ;
31+ }
2032}
2133
34+ /** @deprecated Use checkEnvironmentDetailed instead */
2235export async function checkEnvironment ( scope : 'Machine' | 'User' = 'User' ) : Promise < boolean > {
23- const paths = scope === 'Machine' ? SYSTEM_PATHS : USER_PATHS ;
36+ const result = await checkEnvironmentDetailed ( scope ) ;
37+ return result . ok ;
38+ }
39+
40+ export async function checkEnvironmentDetailed ( scope : 'Machine' | 'User' = 'User' ) : Promise < { ok : boolean , problems : string [ ] , values : { home : string , logs : string , proxy : string } } > {
2441 const home = await runPs ( `[Environment]::GetEnvironmentVariable("${ ENV_VARS . HOME } ", "${ scope } ")` ) ;
2542 const logs = await runPs ( `[Environment]::GetEnvironmentVariable("${ ENV_VARS . LOGS } ", "${ scope } ")` ) ;
2643 const proxy = await runPs ( `[Environment]::GetEnvironmentVariable("${ ENV_VARS . PROXY_PATH } ", "${ scope } ")` ) ;
2744
28- return home === paths . HOME &&
29- logs === paths . LOGS &&
30- proxy === paths . PROXY_EXE ;
45+ const problems : string [ ] = [ ] ;
46+
47+ if ( ! home ) {
48+ problems . push ( `${ ENV_VARS . HOME } is not set.` ) ;
49+ } else {
50+ try {
51+ await fs . ensureDir ( home ) ;
52+ } catch {
53+ problems . push ( `${ ENV_VARS . HOME } path (${ home } ) does not exist and cannot be created.` ) ;
54+ }
55+ }
56+
57+ if ( ! logs ) {
58+ problems . push ( `${ ENV_VARS . LOGS } is not set.` ) ;
59+ } else {
60+ try {
61+ await fs . ensureDir ( logs ) ;
62+ } catch {
63+ problems . push ( `${ ENV_VARS . LOGS } path (${ logs } ) does not exist and cannot be created.` ) ;
64+ }
65+ }
66+
67+ if ( ! proxy ) {
68+ problems . push ( `${ ENV_VARS . PROXY_PATH } is not set.` ) ;
69+ } else if ( ! fs . existsSync ( proxy ) ) {
70+ problems . push ( `${ ENV_VARS . PROXY_PATH } points to non-existent file (${ proxy } ).` ) ;
71+ }
72+
73+ return {
74+ ok : problems . length === 0 ,
75+ problems,
76+ values : { home, logs, proxy }
77+ } ;
3178}
0 commit comments