@@ -48,28 +48,6 @@ export function serializeConfigVars(
48
48
} , [ ] ) ;
49
49
}
50
50
51
- export function addConfigVars (
52
- variables : Variables ,
53
- source : Variables ,
54
- excludeKeys : string [ ] = [ ] ,
55
- prefix ?: string
56
- ) : SerializedConfigVar [ ] {
57
- const configVars : SerializedConfigVar [ ] = [ ] ;
58
- for ( const key in variables ) {
59
- if ( excludeKeys . includes ( key ) ) continue ;
60
- const newValue = variables [ key ] ;
61
- if ( prefix && key . startsWith ( prefix ) ) {
62
- const newKey = key . substring ( prefix . length ) ;
63
- if ( ! ( newKey in source ) || source [ newKey ] !== newValue ) {
64
- configVars . push ( serializeConfigVar ( newKey , newValue ) ) ;
65
- }
66
- } else if ( ! prefix && ! ( key in source ) ) {
67
- configVars . push ( serializeConfigVar ( key , newValue ) ) ;
68
- }
69
- }
70
- return configVars ;
71
- }
72
-
73
51
export async function setConfigVars ( options : {
74
52
appName : string ;
75
53
configVars : SerializedConfigVar [ ] ;
@@ -95,29 +73,59 @@ export async function setConfigVars(options: {
95
73
return undefined ;
96
74
}
97
75
76
+ function addConfigVars (
77
+ variables : Variables ,
78
+ source : Variables ,
79
+ options : {
80
+ excludeKeys ?: string [ ] ;
81
+ prefix ?: string ;
82
+ update ?: boolean ;
83
+ }
84
+ ) : SerializedConfigVar [ ] {
85
+ const { excludeKeys = [ ] , update, prefix } = options ;
86
+ const configVars : SerializedConfigVar [ ] = [ ] ;
87
+
88
+ function shouldInsert ( key : string ) : boolean {
89
+ return ! ( key in source ) || ( source [ key ] !== variables [ key ] && update ) ;
90
+ }
91
+
92
+ for ( let key in variables ) {
93
+ if ( excludeKeys . includes ( key ) ) continue ;
94
+ if ( prefix && ! key . startsWith ( prefix ) ) continue ;
95
+ const newValue = variables [ key ] ;
96
+ key = prefix ? key . substring ( prefix . length ) : key ;
97
+ if ( shouldInsert ( key ) ) {
98
+ configVars . push ( serializeConfigVar ( key , newValue ) ) ;
99
+ }
100
+ }
101
+ return configVars ;
102
+ }
103
+
98
104
/*
99
105
* check existing config variables to only push changed or new variables
100
106
*/
101
107
export async function mergeConfigVars ( options : {
102
108
appName : string ;
103
109
variables ?: Variables ;
104
110
excludeKeys ?: string [ ] ;
111
+ update ?: boolean ;
105
112
} ) : Promise < Variables > {
106
- const { appName, variables = { } , excludeKeys = [ ] } = options ;
113
+ const { appName, variables = { } , excludeKeys = [ ] , update } = options ;
107
114
const currentConfigVars = await getConfigVars ( options ) ;
108
115
let configVars : SerializedConfigVar [ ] = [ ] ;
109
116
configVars = [
110
- ...configVars ,
111
- ...addConfigVars (
112
- process . env ,
113
- currentConfigVars ,
117
+ ...addConfigVars ( process . env , currentConfigVars , {
114
118
excludeKeys,
115
- HEROKU_ENV_VARIABLES_PREFIX
116
- ) ,
119
+ prefix : HEROKU_ENV_VARIABLES_PREFIX ,
120
+ update,
121
+ } ) ,
117
122
] ;
118
123
configVars = [
119
124
...configVars ,
120
- ...addConfigVars ( variables , currentConfigVars , excludeKeys ) ,
125
+ ...addConfigVars ( variables , currentConfigVars , {
126
+ excludeKeys,
127
+ update,
128
+ } ) ,
121
129
] ;
122
130
return setConfigVars ( { appName, configVars } ) ;
123
131
}
0 commit comments