@@ -10,23 +10,45 @@ import { giveUserAccess } from "user-access"
1010import escapeQuote from "escape-quotes"
1111import { pathExists } from "path-exists"
1212
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+
1325/**
1426 * Add an environment variable.
1527 *
1628 * This function is cross-platforms and works in all the local or CI systems.
1729 */
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 )
2036 try {
2137 if ( GITHUB_ACTIONS ) {
2238 try {
39+ if ( options . shouldAddOnlyIfNotDefined ) {
40+ if ( process . env [ name ] !== undefined ) {
41+ info ( `Environment variable ${ name } is already defined. Skipping.` )
42+ return
43+ }
44+ }
2345 exportVariable ( name , val )
2446 } catch ( err ) {
2547 error ( err as Error )
26- await addEnvSystem ( name , val )
48+ await addEnvSystem ( name , val , options )
2749 }
2850 } else {
29- await addEnvSystem ( name , val )
51+ await addEnvSystem ( name , val , options )
3052 }
3153 } catch ( err ) {
3254 error ( err as Error )
@@ -65,10 +87,16 @@ export async function addPath(path: string) {
6587
6688export const cpprc_path = untildifyUser ( ".cpprc" )
6789
68- async function addEnvSystem ( name : string , valGiven : string | undefined ) {
90+ async function addEnvSystem ( name : string , valGiven : string | undefined , options : AddEnvOptions ) {
6991 const val = valGiven ?? ""
7092 switch ( process . platform ) {
7193 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+ }
72100 // We do not use `execaSync(`setx PATH "${path};%PATH%"`)` because of its character limit
73101 await execPowershell ( `[Environment]::SetEnvironmentVariable('${ name } ', '${ val } ', "User")` )
74102 info ( `${ name } ='${ val } ' was set in the environment.` )
@@ -77,8 +105,13 @@ async function addEnvSystem(name: string, valGiven: string | undefined) {
77105 case "linux" :
78106 case "darwin" : {
79107 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+ }
82115 return
83116 }
84117 default : {
0 commit comments