1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const { parse: parseDotenv } = require('dotenv');
1
4
const chalk = require('chalk');
2
5
const inquirer = require("inquirer");
3
6
const JSONbig = require("json-bigint")({ storeAsString: false });
@@ -12,7 +15,7 @@ const { proxyCreateFunctionRule, proxyCreateSiteRule, proxyListRules } = require
12
15
const { consoleVariables } = require('./console');
13
16
const { sdkForConsole } = require('../sdks')
14
17
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');
16
19
const {
17
20
databasesGet,
18
21
databasesCreate,
@@ -149,37 +152,6 @@ const awaitPools = {
149
152
iteration + 1
150
153
);
151
154
},
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
- },
183
155
deleteAttributes: async (databaseId, collectionId, attributeKeys, iteration = 1) => {
184
156
if (iteration > pollMaxDebounces) {
185
157
return false;
@@ -1047,7 +1019,7 @@ const pushSettings = async () => {
1047
1019
}
1048
1020
}
1049
1021
1050
- const pushSite = async({ siteId, async, code } = { returnOnZero: false }) => {
1022
+ const pushSite = async({ siteId, async, code, withVariables } = { returnOnZero: false }) => {
1051
1023
process.chdir(localConfig.configDirectoryPath)
1052
1024
1053
1025
const siteIds = [];
@@ -1180,7 +1152,6 @@ const pushSite = async({ siteId, async, code } = { returnOnZero: false }) => {
1180
1152
timeout: site.timeout,
1181
1153
enabled: site.enabled,
1182
1154
logging: site.logging,
1183
- vars: JSON.stringify(site.vars),
1184
1155
parseOutput: false
1185
1156
});
1186
1157
@@ -1213,16 +1184,43 @@ const pushSite = async({ siteId, async, code } = { returnOnZero: false }) => {
1213
1184
}
1214
1185
}
1215
1186
1216
- updaterRow.update({ status: 'Creating variables' }).replaceSpinner(SPINNER_ARC);
1187
+ if (withVariables) {
1188
+ updaterRow.update({ status: 'Creating variables' }).replaceSpinner(SPINNER_ARC);
1217
1189
1218
- await Promise.all((site['vars'] ?? []).map(async variable => {
1219
- await sitesCreateVariable({
1190
+ const { variables } = await paginate(sitesListVariables, {
1220
1191
siteId: site['$id'],
1221
- key: variable['key'],
1222
- value: variable['value'],
1223
1192
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
+ }
1226
1224
1227
1225
if (code === false) {
1228
1226
successfullyPushed++;
@@ -1475,7 +1473,6 @@ const pushFunction = async ({ functionId, async, code, withVariables } = { retur
1475
1473
entrypoint: func.entrypoint,
1476
1474
commands: func.commands,
1477
1475
scopes: func.scopes,
1478
- vars: JSON.stringify(func.vars),
1479
1476
parseOutput: false
1480
1477
});
1481
1478
@@ -1524,19 +1521,25 @@ const pushFunction = async ({ functionId, async, code, withVariables } = { retur
1524
1521
});
1525
1522
}));
1526
1523
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 = [];
1531
1534
}
1532
-
1533
- // Deploy local variables
1534
- await Promise.all((func['vars'] ?? []).map(async variable => {
1535
+ await Promise.all(envVariables.map(async variable => {
1535
1536
await functionsCreateVariable({
1536
1537
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
1540
1543
});
1541
1544
}));
1542
1545
}
@@ -2065,6 +2068,7 @@ push
2065
2068
.option(`-f, --site-id <site -id >`, `ID of site to run`)
2066
2069
.option(`-A, --async`, `Don't wait for sites deployments status`)
2067
2070
.option("--no-code", "Don't push the site's code")
2071
+ .option("--with-variables", `Push site variables.`)
2068
2072
.action(actionRunner(pushSite));
2069
2073
2070
2074
push
0 commit comments