@@ -10,23 +10,45 @@ import { giveUserAccess } from "user-access"
10
10
import escapeQuote from "escape-quotes"
11
11
import { pathExists } from "path-exists"
12
12
13
+ type AddEnvOptions = {
14
+ /** If true, the value will be escaped with quotes and spaces will be escaped with backslash */
15
+ shouldEscapeSpace ?: boolean
16
+ /** If true, the variable will be only added if it is not defined */
17
+ shouldAddOnlyIfNotDefined ?: boolean
18
+ }
19
+
20
+ const defaultAddEnvOptions : AddEnvOptions = {
21
+ shouldEscapeSpace : false ,
22
+ shouldAddOnlyIfNotDefined : false ,
23
+ }
24
+
13
25
/**
14
26
* Add an environment variable.
15
27
*
16
28
* This function is cross-platforms and works in all the local or CI systems.
17
29
*/
18
- export async function addEnv ( name : string , valGiven : string | undefined , shouldEscapeSpace : boolean = false ) {
19
- const val = escapeString ( valGiven ?? "" , shouldEscapeSpace )
30
+ export async function addEnv (
31
+ name : string ,
32
+ valGiven : string | undefined ,
33
+ options : AddEnvOptions = defaultAddEnvOptions
34
+ ) {
35
+ const val = escapeString ( valGiven ?? "" , options . shouldEscapeSpace )
20
36
try {
21
37
if ( GITHUB_ACTIONS ) {
22
38
try {
39
+ if ( options . shouldAddOnlyIfNotDefined ) {
40
+ if ( process . env [ name ] !== undefined ) {
41
+ info ( `Environment variable ${ name } is already defined. Skipping.` )
42
+ return
43
+ }
44
+ }
23
45
exportVariable ( name , val )
24
46
} catch ( err ) {
25
47
error ( err as Error )
26
- await addEnvSystem ( name , val )
48
+ await addEnvSystem ( name , val , options )
27
49
}
28
50
} else {
29
- await addEnvSystem ( name , val )
51
+ await addEnvSystem ( name , val , options )
30
52
}
31
53
} catch ( err ) {
32
54
error ( err as Error )
@@ -65,10 +87,16 @@ export async function addPath(path: string) {
65
87
66
88
export const cpprc_path = untildifyUser ( ".cpprc" )
67
89
68
- async function addEnvSystem ( name : string , valGiven : string | undefined ) {
90
+ async function addEnvSystem ( name : string , valGiven : string | undefined , options : AddEnvOptions ) {
69
91
const val = valGiven ?? ""
70
92
switch ( process . platform ) {
71
93
case "win32" : {
94
+ if ( options . shouldAddOnlyIfNotDefined ) {
95
+ if ( process . env [ name ] !== undefined ) {
96
+ info ( `Environment variable ${ name } is already defined. Skipping.` )
97
+ return
98
+ }
99
+ }
72
100
// We do not use `execaSync(`setx PATH "${path};%PATH%"`)` because of its character limit
73
101
await execPowershell ( `[Environment]::SetEnvironmentVariable('${ name } ', '${ val } ', "User")` )
74
102
info ( `${ name } ='${ val } ' was set in the environment.` )
@@ -77,8 +105,13 @@ async function addEnvSystem(name: string, valGiven: string | undefined) {
77
105
case "linux" :
78
106
case "darwin" : {
79
107
await setupCppInProfile ( )
80
- appendFileSync ( cpprc_path , `\nexport ${ name } ="${ val } "\n` )
81
- info ( `${ name } ="${ val } " was added to "${ cpprc_path } ` )
108
+ if ( options . shouldAddOnlyIfNotDefined ) {
109
+ appendFileSync ( cpprc_path , `\nif [ -z "\${${ name } }" ]; then export ${ name } ="${ val } "; fi\n` )
110
+ info ( `if not defined ${ name } then ${ name } ="${ val } " was added to "${ cpprc_path } ` )
111
+ } else {
112
+ appendFileSync ( cpprc_path , `\nexport ${ name } ="${ val } "\n` )
113
+ info ( `${ name } ="${ val } " was added to "${ cpprc_path } ` )
114
+ }
82
115
return
83
116
}
84
117
default : {
0 commit comments