Skip to content

Commit 98bfee6

Browse files
committed
feat(cli): Pull function variable
1 parent a7d75d3 commit 98bfee6

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const pullSettings = async () => {
5656
}
5757
}
5858

59-
const pullFunctions = async ({ code }) => {
59+
const pullFunctions = async ({ code, withVariables }) => {
6060
log("Fetching functions ...");
6161
let total = 0;
6262

@@ -83,27 +83,29 @@ const pullFunctions = async ({ code }) => {
8383
const localFunction = localConfig.getFunction(func.$id);
8484

8585
func['path'] = localFunction['path'];
86-
if(!localFunction['path']) {
86+
if (!localFunction['path']) {
8787
func['path'] = `functions/${func.$id}`;
8888
}
89-
89+
if (!withVariables) {
90+
delete func['vars'];
91+
}
9092
localConfig.addFunction(func);
9193

9294
if (!fs.existsSync(func['path'])) {
9395
fs.mkdirSync(func['path'], { recursive: true });
9496
}
9597

96-
if(code === false) {
98+
if (code === false) {
9799
warn("Source code download skipped.");
98-
} else if(!func['deployment']) {
100+
} else if (!func['deployment']) {
99101
warn("Source code download skipped because function doesn't have active deployment.");
100102
} else {
101-
if(allowCodePull === null) {
103+
if (allowCodePull === null) {
102104
const codeAnswer = await inquirer.prompt(questionsPullFunctionsCode);
103105
allowCodePull = codeAnswer.override;
104106
}
105107

106-
if(allowCodePull) {
108+
if (allowCodePull) {
107109
log("Pulling active deployment's code ...");
108110

109111
const compressedFileName = `${func['$id']}-${+new Date()}.tar.gz`
@@ -123,6 +125,16 @@ const pullFunctions = async ({ code }) => {
123125
});
124126

125127
fs.rmSync(compressedFileName);
128+
129+
if (withVariables) {
130+
const envFileLocation = `${func['path']}/.env`
131+
try {
132+
fs.rmSync(envFileLocation);
133+
} catch {
134+
}
135+
136+
fs.writeFileSync(envFileLocation, func['vars'].map(r => `${r.key}=${r.value}\n`).join(''))
137+
}
126138
}
127139
}
128140
}
@@ -170,7 +182,7 @@ const pullCollection = async () => {
170182
parseOutput: false
171183
}, 100, 'collections');
172184

173-
for(const collection of collections) {
185+
for (const collection of collections) {
174186
localConfig.addCollection({
175187
...collection,
176188
'$createdAt': undefined,
@@ -198,7 +210,7 @@ const pullBucket = async () => {
198210

199211
const { buckets } = await paginate(storageListBuckets, { parseOutput: false }, 100, 'buckets');
200212

201-
for(const bucket of buckets) {
213+
for (const bucket of buckets) {
202214
total++;
203215
log(`Pulling bucket ${chalk.bold(bucket['name'])} ...`);
204216
localConfig.addBucket(bucket);
@@ -223,7 +235,7 @@ const pullTeam = async () => {
223235

224236
const { teams } = await paginate(teamsList, { parseOutput: false }, 100, 'teams');
225237

226-
for(const team of teams) {
238+
for (const team of teams) {
227239
total++;
228240
log(`Pulling team ${chalk.bold(team['name'])} ...`);
229241
localConfig.addTeam(team);
@@ -248,7 +260,7 @@ const pullMessagingTopic = async () => {
248260

249261
const { topics } = await paginate(messagingListTopics, { parseOutput: false }, 100, 'topics');
250262

251-
for(const topic of topics) {
263+
for (const topic of topics) {
252264
total++;
253265
log(`Pulling topic ${chalk.bold(topic['name'])} ...`);
254266
localConfig.addMessagingTopic(topic);
@@ -279,6 +291,7 @@ pull
279291
.alias("functions")
280292
.description("Pulling your {{ spec.title|caseUcfirst }} cloud function")
281293
.option("--no-code", "Don't pull the function's code")
294+
.option("--with-variables", `Pull function variables. ${chalk.red('recommend for testing purposes only')}`)
282295
.action(actionRunner(pullFunctions))
283296

284297
pull

templates/cli/lib/config.js.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const _path = require("path");
44
const process = require("process");
55
const JSONbig = require("json-bigint")({ storeAsString: false });
66

7-
const KeysFunction = new Set(["path", "$id", "execute", "name", "enabled", "logging", "runtime", "scopes", "events", "schedule", "timeout", "entrypoint", "commands"]);
7+
const KeysFunction = new Set(["path", "$id", "execute", "name", "enabled", "logging", "runtime", "scopes", "events", "schedule", "timeout", "entrypoint", "commands", "vars"]);
88
const KeysDatabase = new Set(["$id", "name", "enabled"]);
99
const KeysCollection = new Set(["$id", "$permissions", "databaseId", "name", "enabled", "documentSecurity", "attributes", "indexes"]);
1010
const KeysStorage = new Set(["$id", "$permissions", "fileSecurity", "name", "enabled", "maximumFileSize", "allowedFileExtensions", "compression", "encryption", "antivirus"]);

0 commit comments

Comments
 (0)