@@ -3,6 +3,7 @@ const path = require("path");
3
3
const childProcess = require('child_process');
4
4
const { Command } = require("commander");
5
5
const inquirer = require("inquirer");
6
+ const { fetch } = require("undici");
6
7
const { projectsCreate, projectsGet } = require("./projects");
7
8
const { storageCreateBucket } = require("./storage");
8
9
const { messagingCreateTopic } = require("./messaging");
@@ -172,10 +173,26 @@ const initFunction = async () => {
172
173
173
174
fs.mkdirSync(functionDir, "777");
174
175
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;
175
192
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
177
194
178
- let gitPullCommands = `git sparse-checkout add ${answers.runtime.name }`;
195
+ let gitPullCommands = `git sparse-checkout add ${sparse }`;
179
196
180
197
181
198
/* Force use CMD as powershell does not support && */
@@ -184,7 +201,6 @@ const initFunction = async () => {
184
201
gitPullCommands = 'cmd /c "' + gitPullCommands + '"';
185
202
}
186
203
187
- log('Loading templates...');
188
204
/* Execute the child process but do not print any std output */
189
205
try {
190
206
childProcess.execSync(gitInitCommands, { stdio: 'pipe', cwd: templatesDir });
@@ -201,18 +217,17 @@ const initFunction = async () => {
201
217
}
202
218
203
219
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
+ }
213
229
}
214
230
215
-
216
231
const copyRecursiveSync = (src, dest) => {
217
232
let exists = fs.existsSync(src);
218
233
let stats = exists && fs.statSync(src);
0 commit comments