Skip to content

Commit 02a7e0d

Browse files
authored
Merge pull request #1109 from appwrite/fix-cli-env-not-pushing
fix: CLI env variables not pushing
2 parents fbe236a + f82b346 commit 02a7e0d

File tree

1 file changed

+56
-52
lines changed

1 file changed

+56
-52
lines changed

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

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const { parse: parseDotenv } = require('dotenv');
14
const chalk = require('chalk');
25
const inquirer = require("inquirer");
36
const JSONbig = require("json-bigint")({ storeAsString: false });
@@ -12,7 +15,7 @@ const { proxyCreateFunctionRule, proxyCreateSiteRule, proxyListRules } = require
1215
const { consoleVariables } = require('./console');
1316
const { sdkForConsole } = require('../sdks')
1417
const { functionsGet, functionsCreate, functionsUpdate, functionsCreateDeployment, functionsGetDeployment, functionsListVariables, functionsDeleteVariable, functionsCreateVariable } = require('./functions');
15-
const { sitesGet, sitesCreate, sitesUpdate, sitesCreateDeployment, sitesGetDeployment, sitesCreateVariable } = require('./sites');
18+
const { sitesGet, sitesCreate, sitesUpdate, sitesCreateDeployment, sitesGetDeployment, sitesCreateVariable, sitesListVariables, sitesDeleteVariable } = require('./sites');
1619
const {
1720
databasesGet,
1821
databasesCreate,
@@ -149,37 +152,6 @@ const awaitPools = {
149152
iteration + 1
150153
);
151154
},
152-
wipeVariables: async (functionId, iteration = 1) => {
153-
if (iteration > pollMaxDebounces) {
154-
return false;
155-
}
156-
157-
const { total } = await functionsListVariables({
158-
functionId,
159-
queries: ['limit(1)'],
160-
parseOutput: false
161-
});
162-
163-
if (total === 0) {
164-
return true;
165-
}
166-
167-
if (pollMaxDebounces === POLL_DEFAULT_VALUE) {
168-
let steps = Math.max(1, Math.ceil(total / STEP_SIZE));
169-
if (steps > 1 && iteration === 1) {
170-
pollMaxDebounces *= steps;
171-
172-
log('Found a large number of variables, increasing timeout to ' + (pollMaxDebounces * POLL_DEBOUNCE / 1000 / 60) + ' minutes')
173-
}
174-
}
175-
176-
await new Promise(resolve => setTimeout(resolve, POLL_DEBOUNCE));
177-
178-
return await awaitPools.wipeVariables(
179-
functionId,
180-
iteration + 1
181-
);
182-
},
183155
deleteAttributes: async (databaseId, collectionId, attributeKeys, iteration = 1) => {
184156
if (iteration > pollMaxDebounces) {
185157
return false;
@@ -1047,7 +1019,7 @@ const pushSettings = async () => {
10471019
}
10481020
}
10491021

1050-
const pushSite = async({ siteId, async, code } = { returnOnZero: false }) => {
1022+
const pushSite = async({ siteId, async, code, withVariables } = { returnOnZero: false }) => {
10511023
process.chdir(localConfig.configDirectoryPath)
10521024

10531025
const siteIds = [];
@@ -1180,7 +1152,6 @@ const pushSite = async({ siteId, async, code } = { returnOnZero: false }) => {
11801152
timeout: site.timeout,
11811153
enabled: site.enabled,
11821154
logging: site.logging,
1183-
vars: JSON.stringify(site.vars),
11841155
parseOutput: false
11851156
});
11861157

@@ -1213,16 +1184,43 @@ const pushSite = async({ siteId, async, code } = { returnOnZero: false }) => {
12131184
}
12141185
}
12151186

1216-
updaterRow.update({ status: 'Creating variables' }).replaceSpinner(SPINNER_ARC);
1187+
if (withVariables) {
1188+
updaterRow.update({ status: 'Creating variables' }).replaceSpinner(SPINNER_ARC);
12171189

1218-
await Promise.all((site['vars'] ?? []).map(async variable => {
1219-
await sitesCreateVariable({
1190+
const { variables } = await paginate(sitesListVariables, {
12201191
siteId: site['$id'],
1221-
key: variable['key'],
1222-
value: variable['value'],
12231192
parseOutput: false
1224-
});
1225-
}));
1193+
}, 100, 'variables');
1194+
1195+
await Promise.all(variables.map(async variable => {
1196+
await sitesDeleteVariable({
1197+
siteId: site['$id'],
1198+
variableId: variable['$id'],
1199+
parseOutput: false
1200+
});
1201+
}));
1202+
1203+
const envFileLocation = `${site['path']}/.env`;
1204+
let envVariables = [];
1205+
try {
1206+
if (fs.existsSync(envFileLocation)) {
1207+
const envObject = parseDotenv(fs.readFileSync(envFileLocation, 'utf8'));
1208+
envVariables = Object.entries(envObject || {}).map(([key, value]) => ({ key, value }));
1209+
}
1210+
} catch (error) {
1211+
// Handle parsing errors gracefully
1212+
envVariables = [];
1213+
}
1214+
await Promise.all(envVariables.map(async variable => {
1215+
await sitesCreateVariable({
1216+
siteId: site['$id'],
1217+
key: variable.key,
1218+
value: variable.value,
1219+
parseOutput: false,
1220+
secret: false
1221+
});
1222+
}));
1223+
}
12261224

12271225
if (code === false) {
12281226
successfullyPushed++;
@@ -1475,7 +1473,6 @@ const pushFunction = async ({ functionId, async, code, withVariables } = { retur
14751473
entrypoint: func.entrypoint,
14761474
commands: func.commands,
14771475
scopes: func.scopes,
1478-
vars: JSON.stringify(func.vars),
14791476
parseOutput: false
14801477
});
14811478

@@ -1524,19 +1521,25 @@ const pushFunction = async ({ functionId, async, code, withVariables } = { retur
15241521
});
15251522
}));
15261523

1527-
let result = await awaitPools.wipeVariables(func['$id']);
1528-
if (!result) {
1529-
updaterRow.fail({ errorMessage: `Variable deletion timed out.` })
1530-
return;
1524+
const envFileLocation = `${func['path']}/.env`;
1525+
let envVariables = [];
1526+
try {
1527+
if (fs.existsSync(envFileLocation)) {
1528+
const envObject = parseDotenv(fs.readFileSync(envFileLocation, 'utf8'));
1529+
envVariables = Object.entries(envObject || {}).map(([key, value]) => ({ key, value }));
1530+
}
1531+
} catch (error) {
1532+
// Handle parsing errors gracefully
1533+
envVariables = [];
15311534
}
1532-
1533-
// Deploy local variables
1534-
await Promise.all((func['vars'] ?? []).map(async variable => {
1535+
await Promise.all(envVariables.map(async variable => {
15351536
await functionsCreateVariable({
15361537
functionId: func['$id'],
1537-
key: variable['key'],
1538-
value: variable['value'],
1539-
parseOutput: false
1538+
variableId: ID.unique(),
1539+
key: variable.key,
1540+
value: variable.value,
1541+
parseOutput: false,
1542+
secret: false
15401543
});
15411544
}));
15421545
}
@@ -2065,6 +2068,7 @@ push
20652068
.option(`-f, --site-id <site-id>`, `ID of site to run`)
20662069
.option(`-A, --async`, `Don't wait for sites deployments status`)
20672070
.option("--no-code", "Don't push the site's code")
2071+
.option("--with-variables", `Push site variables.`)
20682072
.action(actionRunner(pushSite));
20692073

20702074
push

0 commit comments

Comments
 (0)