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;
@@ -445,8 +417,17 @@ const getObjectChanges = (remote, local, index, what) => {
445
417
446
418
if (remote[index] && local[index]) {
447
419
for (let [service, status] of Object.entries(remote[index])) {
448
- if (status !== local[index][service]) {
449
- changes.push({ group: what, setting: service, remote: chalk.red(status), local: chalk.green(local[index][service]) })
420
+ const localValue = local[index][service];
421
+ let valuesEqual = false;
422
+
423
+ if (Array.isArray(status) && Array.isArray(localValue)) {
424
+ valuesEqual = JSON.stringify(status) === JSON.stringify(localValue);
425
+ } else {
426
+ valuesEqual = status === localValue;
427
+ }
428
+
429
+ if (!valuesEqual) {
430
+ changes.push({ group: what, setting: service, remote: chalk.red(status), local: chalk.green(localValue) })
450
431
}
451
432
}
452
433
}
@@ -508,6 +489,7 @@ const createAttribute = (databaseId, collectionId, attribute) => {
508
489
required: attribute.required,
509
490
xdefault: attribute.default,
510
491
array: attribute.array,
492
+ encrypt: attribute.encrypt,
511
493
parseOutput: false
512
494
})
513
495
@@ -844,10 +826,14 @@ const attributesToCreate = async (remoteAttributes, localAttributes, collection,
844
826
845
827
if (!cliConfig.force) {
846
828
if (deleting.length > 0 && !isIndex) {
847
- log(`Attribute deletion will cause ${chalk.red('loss of data')}`);
829
+ console.log(`${chalk.red('-------------------------------------------------------')}`);
830
+ console.log(`${chalk.red('| WARNING: Attribute deletion may cause loss of data |')}`);
831
+ console.log(`${chalk.red('-------------------------------------------------------')}`);
848
832
}
849
833
if (conflicts.length > 0 && !isIndex) {
850
- log(`Attribute recreation will cause ${chalk.red('loss of data')}`);
834
+ console.log(`${chalk.red('---------------------------------------------------------')}`);
835
+ console.log(`${chalk.red('| WARNING: Attribute recreation may cause loss of data |')}`);
836
+ console.log(`${chalk.red('---------------------------------------------------------')}`);
851
837
}
852
838
853
839
if ((await getConfirmation()) !== true) {
@@ -1042,7 +1028,7 @@ const pushSettings = async () => {
1042
1028
}
1043
1029
}
1044
1030
1045
- const pushSite = async({ siteId, async, code } = { returnOnZero: false }) => {
1031
+ const pushSite = async({ siteId, async, code, withVariables } = { returnOnZero: false }) => {
1046
1032
process.chdir(localConfig.configDirectoryPath)
1047
1033
1048
1034
const siteIds = [];
@@ -1175,7 +1161,6 @@ const pushSite = async({ siteId, async, code } = { returnOnZero: false }) => {
1175
1161
timeout: site.timeout,
1176
1162
enabled: site.enabled,
1177
1163
logging: site.logging,
1178
- vars: JSON.stringify(site.vars),
1179
1164
parseOutput: false
1180
1165
});
1181
1166
@@ -1208,16 +1193,43 @@ const pushSite = async({ siteId, async, code } = { returnOnZero: false }) => {
1208
1193
}
1209
1194
}
1210
1195
1211
- updaterRow.update({ status: 'Creating variables' }).replaceSpinner(SPINNER_ARC);
1196
+ if (withVariables) {
1197
+ updaterRow.update({ status: 'Creating variables' }).replaceSpinner(SPINNER_ARC);
1212
1198
1213
- await Promise.all((site['vars'] ?? []).map(async variable => {
1214
- await sitesCreateVariable({
1199
+ const { variables } = await paginate(sitesListVariables, {
1215
1200
siteId: site['$id'],
1216
- key: variable['key'],
1217
- value: variable['value'],
1218
1201
parseOutput: false
1219
- });
1220
- }));
1202
+ }, 100, 'variables');
1203
+
1204
+ await Promise.all(variables.map(async variable => {
1205
+ await sitesDeleteVariable({
1206
+ siteId: site['$id'],
1207
+ variableId: variable['$id'],
1208
+ parseOutput: false
1209
+ });
1210
+ }));
1211
+
1212
+ const envFileLocation = `${site['path']}/.env`;
1213
+ let envVariables = [];
1214
+ try {
1215
+ if (fs.existsSync(envFileLocation)) {
1216
+ const envObject = parseDotenv(fs.readFileSync(envFileLocation, 'utf8'));
1217
+ envVariables = Object.entries(envObject || {}).map(([key, value]) => ({ key, value }));
1218
+ }
1219
+ } catch (error) {
1220
+ // Handle parsing errors gracefully
1221
+ envVariables = [];
1222
+ }
1223
+ await Promise.all(envVariables.map(async variable => {
1224
+ await sitesCreateVariable({
1225
+ siteId: site['$id'],
1226
+ key: variable.key,
1227
+ value: variable.value,
1228
+ parseOutput: false,
1229
+ secret: false
1230
+ });
1231
+ }));
1232
+ }
1221
1233
1222
1234
if (code === false) {
1223
1235
successfullyPushed++;
@@ -1470,7 +1482,6 @@ const pushFunction = async ({ functionId, async, code, withVariables } = { retur
1470
1482
entrypoint: func.entrypoint,
1471
1483
commands: func.commands,
1472
1484
scopes: func.scopes,
1473
- vars: JSON.stringify(func.vars),
1474
1485
parseOutput: false
1475
1486
});
1476
1487
@@ -1519,19 +1530,25 @@ const pushFunction = async ({ functionId, async, code, withVariables } = { retur
1519
1530
});
1520
1531
}));
1521
1532
1522
- let result = await awaitPools.wipeVariables(func['$id']);
1523
- if (!result) {
1524
- updaterRow.fail({ errorMessage: `Variable deletion timed out.` })
1525
- return;
1533
+ const envFileLocation = `${func['path']}/.env`;
1534
+ let envVariables = [];
1535
+ try {
1536
+ if (fs.existsSync(envFileLocation)) {
1537
+ const envObject = parseDotenv(fs.readFileSync(envFileLocation, 'utf8'));
1538
+ envVariables = Object.entries(envObject || {}).map(([key, value]) => ({ key, value }));
1539
+ }
1540
+ } catch (error) {
1541
+ // Handle parsing errors gracefully
1542
+ envVariables = [];
1526
1543
}
1527
-
1528
- // Deploy local variables
1529
- await Promise.all((func['vars'] ?? []).map(async variable => {
1544
+ await Promise.all(envVariables.map(async variable => {
1530
1545
await functionsCreateVariable({
1531
1546
functionId: func['$id'],
1532
- key: variable['key'],
1533
- value: variable['value'],
1534
- parseOutput: false
1547
+ variableId: ID.unique(),
1548
+ key: variable.key,
1549
+ value: variable.value,
1550
+ parseOutput: false,
1551
+ secret: false
1535
1552
});
1536
1553
}));
1537
1554
}
@@ -2060,6 +2077,7 @@ push
2060
2077
.option(`-f, --site-id <site -id >`, `ID of site to run`)
2061
2078
.option(`-A, --async`, `Don't wait for sites deployments status`)
2062
2079
.option("--no-code", "Don't push the site's code")
2080
+ .option("--with-variables", `Push site variables.`)
2063
2081
.action(actionRunner(pushSite));
2064
2082
2065
2083
push
0 commit comments