Skip to content

Commit 57984ba

Browse files
Merge pull request #877 from appwrite/feat-function-init-improvments
Feat function init improvments
2 parents 2db03b5 + 7ed643e commit 57984ba

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

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

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const path = require("path");
33
const childProcess = require('child_process');
44
const { Command } = require("commander");
55
const inquirer = require("inquirer");
6+
const { fetch } = require("undici");
67
const { projectsCreate, projectsGet } = require("./projects");
78
const { storageCreateBucket } = require("./storage");
89
const { messagingCreateTopic } = require("./messaging");
@@ -172,10 +173,26 @@ const initFunction = async () => {
172173

173174
fs.mkdirSync(functionDir, "777");
174175
fs.mkdirSync(templatesDir, "777");
176+
const repo = "https://github.com/{{ sdk.gitUserName }}/templates";
177+
const api = `https://api.github.com/repos/{{ sdk.gitUserName }}/templates/contents/${answers.runtime.name}`
178+
const templates = ['Starter'];
179+
let selected = undefined;
180+
181+
try {
182+
const res = await fetch(api);
183+
templates.push(...(await res.json()).map((template) => template.name));
184+
185+
selected = await inquirer.prompt(questionsCreateFunctionSelectTemplate(templates))
186+
} catch {
187+
// Not a problem will go with directory pulling
188+
log('Loading templates...');
189+
}
190+
191+
const sparse = selected ? `${answers.runtime.name}/${selected.template}` : answers.runtime.name;
175192

176-
let gitInitCommands = "git clone --single-branch --depth 1 --sparse https://github.com/{{ sdk.gitUserName }}/templates ."; // depth prevents fetching older commits reducing the amount fetched
193+
let gitInitCommands = `git clone --single-branch --depth 1 --sparse ${repo} .`; // depth prevents fetching older commits reducing the amount fetched
177194

178-
let gitPullCommands = `git sparse-checkout add ${answers.runtime.name}`;
195+
let gitPullCommands = `git sparse-checkout add ${sparse}`;
179196

180197

181198
/* Force use CMD as powershell does not support && */
@@ -184,7 +201,6 @@ const initFunction = async () => {
184201
gitPullCommands = 'cmd /c "' + gitPullCommands + '"';
185202
}
186203

187-
log('Loading templates...');
188204
/* Execute the child process but do not print any std output */
189205
try {
190206
childProcess.execSync(gitInitCommands, { stdio: 'pipe', cwd: templatesDir });
@@ -201,18 +217,17 @@ const initFunction = async () => {
201217
}
202218

203219
fs.rmSync(path.join(templatesDir, ".git"), { recursive: true });
204-
const templates = ['Starter'];
205-
templates.push(...fs.readdirSync(runtimeDir, { withFileTypes: true })
206-
.filter(item => item.isDirectory() && item.name !== 'starter')
207-
.map(dirent => dirent.name));
208-
209-
let selected = { template: 'starter' };
210-
211-
if (templates.length > 1) {
212-
selected = await inquirer.prompt(questionsCreateFunctionSelectTemplate(templates))
220+
if (!selected) {
221+
templates.push(...fs.readdirSync(runtimeDir, { withFileTypes: true })
222+
.filter(item => item.isDirectory() && item.name !== 'starter')
223+
.map(dirent => dirent.name));
224+
selected = { template: 'starter' };
225+
226+
if (templates.length > 1) {
227+
selected = await inquirer.prompt(questionsCreateFunctionSelectTemplate(templates))
228+
}
213229
}
214230

215-
216231
const copyRecursiveSync = (src, dest) => {
217232
let exists = fs.existsSync(src);
218233
let stats = exists && fs.statSync(src);

0 commit comments

Comments
 (0)