Skip to content

Commit 8835a77

Browse files
committed
fix(nx-heroku): handle update of config vars during merge
1 parent 0a174e2 commit 8835a77

File tree

3 files changed

+45
-31
lines changed

3 files changed

+45
-31
lines changed

packages/nx-heroku/src/executors/common/heroku/config-vars.ts

+38-30
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,6 @@ export function serializeConfigVars(
4848
}, []);
4949
}
5050

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-
7351
export async function setConfigVars(options: {
7452
appName: string;
7553
configVars: SerializedConfigVar[];
@@ -95,29 +73,59 @@ export async function setConfigVars(options: {
9573
return undefined;
9674
}
9775

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+
98104
/*
99105
* check existing config variables to only push changed or new variables
100106
*/
101107
export async function mergeConfigVars(options: {
102108
appName: string;
103109
variables?: Variables;
104110
excludeKeys?: string[];
111+
update?: boolean;
105112
}): Promise<Variables> {
106-
const { appName, variables = {}, excludeKeys = [] } = options;
113+
const { appName, variables = {}, excludeKeys = [], update } = options;
107114
const currentConfigVars = await getConfigVars(options);
108115
let configVars: SerializedConfigVar[] = [];
109116
configVars = [
110-
...configVars,
111-
...addConfigVars(
112-
process.env,
113-
currentConfigVars,
117+
...addConfigVars(process.env, currentConfigVars, {
114118
excludeKeys,
115-
HEROKU_ENV_VARIABLES_PREFIX
116-
),
119+
prefix: HEROKU_ENV_VARIABLES_PREFIX,
120+
update,
121+
}),
117122
];
118123
configVars = [
119124
...configVars,
120-
...addConfigVars(variables, currentConfigVars, excludeKeys),
125+
...addConfigVars(variables, currentConfigVars, {
126+
excludeKeys,
127+
update,
128+
}),
121129
];
122130
return setConfigVars({ appName, configVars });
123131
}

packages/nx-heroku/src/executors/deploy/services/heroku-app.service.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,12 @@ class HerokuApp {
206206
}
207207

208208
private async mergeConfigVars(): Promise<void> {
209-
const updatedConfigVars = await mergeConfigVars(this.options);
209+
const { appName, variables } = this.options;
210+
const updatedConfigVars = await mergeConfigVars({
211+
appName,
212+
variables,
213+
update: true,
214+
});
210215
if (updatedConfigVars) {
211216
this.logger.info(
212217
`Merged config vars : ${serializeConfigVars(updatedConfigVars)}.`

packages/nx-heroku/src/executors/promote/services/heroku-promote.service.ts

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export class HerokuPromoteService extends HerokuBaseService<PromoteExecutorSchem
7878
await mergeConfigVars({
7979
appName,
8080
variables,
81+
update: true,
8182
});
8283

8384
this.logger.log(`Promoting app ${appName}...`);

0 commit comments

Comments
 (0)