Skip to content

Commit cade026

Browse files
committed
Fetch and create variables for sites
1 parent 6dc94ff commit cade026

File tree

3 files changed

+31
-33
lines changed

3 files changed

+31
-33
lines changed

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

+21
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,26 @@ const initSite = async () => {
428428
newReadmeFile[0] = `# ${answers.key}`;
429429
newReadmeFile.splice(1, 2);
430430
fs.writeFileSync(readmePath, newReadmeFile.join('\n'));
431+
432+
let vars = (templateDetails.variables ?? []).map(variable => {
433+
let value = variable.value;
434+
const replacements = {
435+
'{apiEndpoint}': globalConfig.getEndpoint(),
436+
'{projectId}': localConfig.getProject().projectId,
437+
'{projectName}': localConfig.getProject().projectName,
438+
};
439+
440+
for (const placeholder in replacements) {
441+
if (value?.includes(placeholder)) {
442+
value = value.replace(placeholder, replacements[placeholder]);
443+
}
444+
}
445+
446+
return {
447+
key: variable.name,
448+
value: value
449+
};
450+
});
431451

432452
let data = {
433453
$id: siteId,
@@ -445,6 +465,7 @@ const initSite = async () => {
445465
logging: true,
446466
ignore: answers.framework.ignore || null,
447467
path: `sites/${siteName}`,
468+
vars: vars
448469
};
449470

450471
if (!data.buildRuntime) {

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

+9-32
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const { proxyCreateFunctionRule, proxyCreateSiteRule, proxyListRules } = require
1212
const { consoleVariables } = require('./console');
1313
const { sdkForConsole } = require('../sdks')
1414
const { functionsGet, functionsCreate, functionsUpdate, functionsCreateDeployment, functionsGetDeployment, functionsListVariables, functionsDeleteVariable, functionsCreateVariable } = require('./functions');
15-
const { sitesGet, sitesCreate, sitesUpdate, sitesCreateDeployment, sitesGetDeployment, sitesListVariables, sitesCreateVariable } = require('./sites');
15+
const { sitesGet, sitesCreate, sitesUpdate, sitesCreateDeployment, sitesGetDeployment, sitesCreateVariable } = require('./sites');
1616
const {
1717
databasesGet,
1818
databasesCreate,
@@ -1011,7 +1011,7 @@ const pushSettings = async () => {
10111011
}
10121012
}
10131013

1014-
const pushSite = async({ siteId, async, code, withVariables } = { returnOnZero: false }) => {
1014+
const pushSite = async({ siteId, async, code } = { returnOnZero: false }) => {
10151015
process.chdir(localConfig.configDirectoryPath)
10161016

10171017
const siteIds = [];
@@ -1177,38 +1177,16 @@ const pushSite = async({ siteId, async, code, withVariables } = { returnOnZero:
11771177
}
11781178
}
11791179

1180-
if (withVariables) {
1181-
updaterRow.update({ status: 'Updating variables' }).replaceSpinner(SPINNER_ARC);
1180+
updaterRow.update({ status: 'Creating variables' }).replaceSpinner(SPINNER_ARC);
11821181

1183-
const { variables } = await paginate(sitesListVariables, {
1182+
await Promise.all((site['vars'] ?? []).map(async variable => {
1183+
await sitesCreateVariable({
11841184
siteId: site['$id'],
1185+
key: variable['key'],
1186+
value: variable['value'],
11851187
parseOutput: false
1186-
}, 100, 'variables');
1187-
1188-
await Promise.all(variables.map(async variable => {
1189-
await sitesDeleteVariable({
1190-
siteId: site['$id'],
1191-
variableId: variable['$id'],
1192-
parseOutput: false
1193-
});
1194-
}));
1195-
1196-
let result = await awaitPools.wipeVariables(site['$id']);
1197-
if (!result) {
1198-
updaterRow.fail({ errorMessage: `Variable deletion timed out.` })
1199-
return;
1200-
}
1201-
1202-
// Deploy local variables
1203-
await Promise.all((site['vars'] ?? []).map(async variable => {
1204-
await sitesCreateVariable({
1205-
siteId: site['$id'],
1206-
key: variable['key'],
1207-
value: variable['value'],
1208-
parseOutput: false
1209-
});
1210-
}));
1211-
}
1188+
});
1189+
}));
12121190

12131191
if (code === false) {
12141192
successfullyPushed++;
@@ -2051,7 +2029,6 @@ push
20512029
.option(`-f, --site-id <site-id>`, `ID of site to run`)
20522030
.option(`-A, --async`, `Don't wait for sites deployments status`)
20532031
.option("--no-code", "Don't push the site's code")
2054-
.option("--with-variables", `Push site variables.`)
20552032
.action(actionRunner(pushSite));
20562033

20572034
push

templates/cli/lib/config.js.twig

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const process = require("process");
55
const JSONbig = require("json-bigint")({ storeAsString: false });
66

77
const KeysVars = new Set(["key", "value"]);
8-
const KeysSite = new Set(["path", "$id", "name", "enabled", "logging", "timeout", "framework", "buildRuntime", "adapter", "installCommand", "buildCommand", "outputDirectory", "fallbackFile", "specification"]);
8+
const KeysSite = new Set(["path", "$id", "name", "enabled", "logging", "timeout", "framework", "buildRuntime", "adapter", "installCommand", "buildCommand", "outputDirectory", "fallbackFile", "specification", "vars"]);
99
const KeysFunction = new Set(["path", "$id", "execute", "name", "enabled", "logging", "runtime", "specification", "scopes", "events", "schedule", "timeout", "entrypoint", "commands", "vars"]);
1010
const KeysDatabase = new Set(["$id", "name", "enabled"]);
1111
const KeysCollection = new Set(["$id", "$permissions", "databaseId", "name", "enabled", "documentSecurity", "attributes", "indexes"]);

0 commit comments

Comments
 (0)