|
1 | 1 | const fs = require('fs');
|
2 | 2 | const path = require('path');
|
| 3 | +const { parse: parseDotenv } = require('dotenv'); |
3 | 4 | const chalk = require('chalk');
|
4 | 5 | const inquirer = require("inquirer");
|
5 | 6 | const JSONbig = require("json-bigint")({ storeAsString: false });
|
@@ -378,50 +379,6 @@ const getConfirmation = async () => {
|
378 | 379 | };
|
379 | 380 | const isEmpty = (value) => (value === null || value === undefined || (typeof value === "string" && value.trim().length === 0) || (Array.isArray(value) && value.length === 0));
|
380 | 381 |
|
381 |
| -const parseEnvFile = (filePath) => { |
382 |
| - const envVars = []; |
383 |
| - |
384 |
| - try { |
385 |
| - if (!fs.existsSync(filePath)) { |
386 |
| - return envVars; |
387 |
| - } |
388 |
| - |
389 |
| - const envContent = fs.readFileSync(filePath, 'utf8'); |
390 |
| - const lines = envContent.split('\n'); |
391 |
| - |
392 |
| - for (const line of lines) { |
393 |
| - const trimmedLine = line.trim(); |
394 |
| - |
395 |
| - // Skip empty lines and comments |
396 |
| - if (!trimmedLine || trimmedLine.startsWith('#')) { |
397 |
| - continue; |
398 |
| - } |
399 |
| - |
400 |
| - // Parse KEY=VALUE format |
401 |
| - const equalIndex = trimmedLine.indexOf('='); |
402 |
| - if (equalIndex > 0) { |
403 |
| - const key = trimmedLine.substring(0, equalIndex).trim(); |
404 |
| - let value = trimmedLine.substring(equalIndex + 1).trim(); |
405 |
| - |
406 |
| - // Remove surrounding quotes if present |
407 |
| - if ((value.startsWith('"') && value.endsWith('"')) || |
408 |
| - (value.startsWith("'") && value.endsWith("'"))) { |
409 |
| - value = value.slice(1, -1); |
410 |
| - } |
411 |
| - |
412 |
| - if (key) { |
413 |
| - envVars.push({ key, value }); |
414 |
| - } |
415 |
| - } |
416 |
| - } |
417 |
| - } catch (error) { |
418 |
| - // Silently ignore file reading errors |
419 |
| - console.warn(`Warning: Could not read .env file at ${filePath}: ${error.message}`); |
420 |
| - } |
421 |
| - |
422 |
| - return envVars; |
423 |
| -}; |
424 |
| - |
425 | 382 | const approveChanges = async (resource, resourceGetFunction, keys, resourceName, resourcePlural, skipKeys = [], secondId = '', secondResourceName = '') => {
|
426 | 383 | log('Checking for changes ...');
|
427 | 384 | const changes = [];
|
@@ -1281,12 +1238,21 @@ const pushSite = async({ siteId, async, code, withVariables } = { returnOnZero:
|
1281 | 1238 | }
|
1282 | 1239 |
|
1283 | 1240 | const envFileLocation = `${site['path']}/.env`;
|
1284 |
| - const envVariables = parseEnvFile(envFileLocation); |
| 1241 | + let envVariables = []; |
| 1242 | + try { |
| 1243 | + if (fs.existsSync(envFileLocation)) { |
| 1244 | + const envObject = parseDotenv(fs.readFileSync(envFileLocation, 'utf8')); |
| 1245 | + envVariables = Object.entries(envObject || {}).map(([key, value]) => ({ key, value })); |
| 1246 | + } |
| 1247 | + } catch (error) { |
| 1248 | + // Handle parsing errors gracefully |
| 1249 | + envVariables = []; |
| 1250 | + } |
1285 | 1251 | await Promise.all(envVariables.map(async variable => {
|
1286 | 1252 | await sitesCreateVariable({
|
1287 | 1253 | siteId: site['$id'],
|
1288 |
| - key: variable['key'], |
1289 |
| - value: variable['value'], |
| 1254 | + key: variable.key, |
| 1255 | + value: variable.value, |
1290 | 1256 | parseOutput: false
|
1291 | 1257 | });
|
1292 | 1258 | }));
|
@@ -1598,7 +1564,16 @@ const pushFunction = async ({ functionId, async, code, withVariables } = { retur
|
1598 | 1564 | }
|
1599 | 1565 |
|
1600 | 1566 | const envFileLocation = `${func['path']}/.env`;
|
1601 |
| - const envVariables = parseEnvFile(envFileLocation); |
| 1567 | + let envVariables = []; |
| 1568 | + try { |
| 1569 | + if (fs.existsSync(envFileLocation)) { |
| 1570 | + const envObject = parseDotenv(fs.readFileSync(envFileLocation, 'utf8')); |
| 1571 | + envVariables = Object.entries(envObject || {}).map(([key, value]) => ({ key, value })); |
| 1572 | + } |
| 1573 | + } catch (error) { |
| 1574 | + // Handle parsing errors gracefully |
| 1575 | + envVariables = []; |
| 1576 | + } |
1602 | 1577 | await Promise.all(envVariables.map(async variable => {
|
1603 | 1578 | await functionsCreateVariable({
|
1604 | 1579 | functionId: func['$id'],
|
|
0 commit comments