Skip to content

Commit b5e43a8

Browse files
committed
use parseDotenv
1 parent 489b1d8 commit b5e43a8

File tree

1 file changed

+23
-48
lines changed

1 file changed

+23
-48
lines changed

templates/cli/lib/commands/push.js.twig

Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const fs = require('fs');
22
const path = require('path');
3+
const { parse: parseDotenv } = require('dotenv');
34
const chalk = require('chalk');
45
const inquirer = require("inquirer");
56
const JSONbig = require("json-bigint")({ storeAsString: false });
@@ -378,50 +379,6 @@ const getConfirmation = async () => {
378379
};
379380
const isEmpty = (value) => (value === null || value === undefined || (typeof value === "string" && value.trim().length === 0) || (Array.isArray(value) && value.length === 0));
380381

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-
425382
const approveChanges = async (resource, resourceGetFunction, keys, resourceName, resourcePlural, skipKeys = [], secondId = '', secondResourceName = '') => {
426383
log('Checking for changes ...');
427384
const changes = [];
@@ -1281,12 +1238,21 @@ const pushSite = async({ siteId, async, code, withVariables } = { returnOnZero:
12811238
}
12821239

12831240
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+
}
12851251
await Promise.all(envVariables.map(async variable => {
12861252
await sitesCreateVariable({
12871253
siteId: site['$id'],
1288-
key: variable['key'],
1289-
value: variable['value'],
1254+
key: variable.key,
1255+
value: variable.value,
12901256
parseOutput: false
12911257
});
12921258
}));
@@ -1598,7 +1564,16 @@ const pushFunction = async ({ functionId, async, code, withVariables } = { retur
15981564
}
15991565

16001566
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+
}
16021577
await Promise.all(envVariables.map(async variable => {
16031578
await functionsCreateVariable({
16041579
functionId: func['$id'],

0 commit comments

Comments
 (0)