| 34 | continue; |
| 35 | } |
| 36 | return sensitive; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | function normalizeFileValue(value: string): string { |
| 41 | const trailingNewlineLength = value.endsWith('\r\n') ? 2 : value.endsWith('\n') ? 1 : 0; |
| 42 | if (trailingNewlineLength === 0) return value; |
| 43 | const valueWithoutTrailingNewline = value.slice(0, -trailingNewlineLength); |
| 44 | return /[\r\n]/.test(valueWithoutTrailingNewline) ? value : valueWithoutTrailingNewline; |
| 45 | } |
| 46 | |
| 47 | async function collectValues( |
| 48 | options: SetOptions, |
| 49 | environments: readonly Environment[] |
| 50 | ): Promise<Partial<Record<Environment, string>>> { |
| 51 | const values: Partial<Record<Environment, string>> = {}; |
| 52 | for (const environment of environments) { |
| 53 | const file = options.valueFiles[environment]; |
| 54 | if (file) { |
| 55 | const value = stripSurroundingQuotes( |
| 56 | normalizeFileValue(readFileSync(path.resolve(file), 'utf8')) |
| 57 | ); |
| 58 | if (!value) throw new Error(`${environment} value file cannot be empty.`); |
| 59 | values[environment] = value; |
| 60 | continue; |
| 61 | } |
| 62 | |
| 63 | while (!values[environment]) { |
| 64 | const value = stripSurroundingQuotes(await readSecret(`${environment} value: `)); |