|
1 | 1 | import { Flags } from '@oclif/core';
|
| 2 | +import chalk from 'chalk'; |
2 | 3 | import dotenv from 'dotenv';
|
3 | 4 | import * as fs from 'fs-extra';
|
4 | 5 | import path from 'path';
|
@@ -43,6 +44,31 @@ export default class EnvPull extends EasCommand {
|
43 | 44 | }),
|
44 | 45 | };
|
45 | 46 |
|
| 47 | + async isVariableEqualAsync( |
| 48 | + currentEnvValue: string | undefined, |
| 49 | + newVariable: EnvironmentVariableWithFileContent |
| 50 | + ): Promise<boolean> { |
| 51 | + if (newVariable.visibility === EnvironmentVariableVisibility.Secret) { |
| 52 | + return true; |
| 53 | + } |
| 54 | + |
| 55 | + if ( |
| 56 | + newVariable.type === EnvironmentSecretType.FileBase64 && |
| 57 | + newVariable.valueWithFileContent && |
| 58 | + currentEnvValue |
| 59 | + ) { |
| 60 | + if (!(await fs.exists(currentEnvValue))) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + const fileContent = await fs.readFile(currentEnvValue, 'base64'); |
| 65 | + |
| 66 | + return fileContent === newVariable.valueWithFileContent; |
| 67 | + } |
| 68 | + |
| 69 | + return currentEnvValue === newVariable.value; |
| 70 | + } |
| 71 | + |
46 | 72 | async runAsync(): Promise<void> {
|
47 | 73 | let {
|
48 | 74 | args: { environment: argEnvironment },
|
@@ -105,6 +131,31 @@ export default class EnvPull extends EasCommand {
|
105 | 131 | await fs.mkdir(envDir, { recursive: true });
|
106 | 132 | }
|
107 | 133 |
|
| 134 | + const allVariableNames = new Set([ |
| 135 | + ...environmentVariables.map(v => v.name), |
| 136 | + ...Object.keys(currentEnvLocal), |
| 137 | + ]); |
| 138 | + |
| 139 | + const diffLog = []; |
| 140 | + |
| 141 | + for (const variableName of allVariableNames) { |
| 142 | + const newVariable = environmentVariables.find(v => v.name === variableName); |
| 143 | + if (newVariable) { |
| 144 | + if (Object.hasOwn(currentEnvLocal, variableName)) { |
| 145 | + if (await this.isVariableEqualAsync(currentEnvLocal[variableName], newVariable)) { |
| 146 | + diffLog.push(chalk.black(` ${variableName}`)); |
| 147 | + } else { |
| 148 | + diffLog.push(chalk.yellow(`~ ${variableName}`)); |
| 149 | + } |
| 150 | + } else { |
| 151 | + diffLog.push(chalk.green(`+ ${variableName}`)); |
| 152 | + } |
| 153 | + } else if (currentEnvLocal[variableName]) { |
| 154 | + diffLog.push(chalk.red(`- ${variableName}`)); |
| 155 | + } |
| 156 | + } |
| 157 | + Log.addNewLineIfNone(); |
| 158 | + |
108 | 159 | const skippedSecretVariables: string[] = [];
|
109 | 160 | const overridenSecretVariables: string[] = [];
|
110 | 161 |
|
@@ -146,5 +197,10 @@ export default class EnvPull extends EasCommand {
|
146 | 197 | )}.`
|
147 | 198 | );
|
148 | 199 | }
|
| 200 | + |
| 201 | + Log.addNewLineIfNone(); |
| 202 | + diffLog.forEach(line => { |
| 203 | + Log.log(line); |
| 204 | + }); |
149 | 205 | }
|
150 | 206 | }
|
0 commit comments