@@ -12,6 +12,7 @@ const ID = require("../id");
12
12
const { localConfig, globalConfig } = require("../config");
13
13
const {
14
14
questionsCreateFunction,
15
+ questionsCreateFunctionSelectTemplate,
15
16
questionsCreateBucket,
16
17
questionsCreateMessagingTopic,
17
18
questionsCreateCollection,
@@ -133,6 +134,8 @@ const initFunction = async () => {
133
134
134
135
const functionId = answers.id === 'unique()' ? ID.unique() : answers.id;
135
136
const functionDir = path.join(functionFolder, functionId);
137
+ const templatesDir = path.join(functionFolder, `${functionId}-templates`);
138
+ const runtimeDir = path.join(templatesDir, answers.runtime.name);
136
139
137
140
if (fs.existsSync(functionDir)) {
138
141
throw new Error(`( ${functionId} ) already exists in the current directory. Please choose another name.`);
@@ -156,21 +159,24 @@ const initFunction = async () => {
156
159
})
157
160
158
161
fs.mkdirSync(functionDir, "777");
162
+ fs.mkdirSync(templatesDir, "777");
159
163
160
- let gitInitCommands = "git clone -b v3 --single-branch --depth 1 --sparse https://github.com/{{ sdk .gitUserName }}/functions-starter ."; // depth prevents fetching older commits reducing the amount fetched
164
+ let gitInitCommands = "git clone --single-branch --depth 1 --sparse https://github.com/{{ sdk .gitUserName }}/templates ."; // depth prevents fetching older commits reducing the amount fetched
165
+
166
+ let gitPullCommands = `git sparse-checkout add ${answers.runtime.name}`;
161
167
162
- let gitPullCommands = `git sparse-checkout add ${answers.runtime.id}`;
163
168
164
169
/* Force use CMD as powershell does not support && */
165
170
if (process.platform === 'win32') {
166
171
gitInitCommands = 'cmd /c "' + gitInitCommands + '"';
167
172
gitPullCommands = 'cmd /c "' + gitPullCommands + '"';
168
173
}
169
174
175
+ log('Loading templates...');
170
176
/* Execute the child process but do not print any std output */
171
177
try {
172
- childProcess.execSync(gitInitCommands, { stdio: 'pipe', cwd: functionDir });
173
- childProcess.execSync(gitPullCommands, { stdio: 'pipe', cwd: functionDir });
178
+ childProcess.execSync(gitInitCommands, { stdio: 'pipe', cwd: templatesDir });
179
+ childProcess.execSync(gitPullCommands, { stdio: 'pipe', cwd: templatesDir });
174
180
} catch (error) {
175
181
/* Specialised errors with recommended actions to take */
176
182
if (error.message.includes('error: unknown option')) {
@@ -182,7 +188,20 @@ const initFunction = async () => {
182
188
}
183
189
}
184
190
185
- fs.rmSync(path.join(functionDir, ".git"), { recursive: true });
191
+ fs.rmSync(path.join(templatesDir, ".git"), { recursive: true });
192
+ const templates = ['Starter'];
193
+ templates.push(...fs.readdirSync(runtimeDir, { withFileTypes: true })
194
+ .filter(item => item.isDirectory() && item.name !== 'starter')
195
+ .map(dirent => dirent.name));
196
+
197
+ let selected = { template: 'starter' };
198
+
199
+ if (templates.length > 1) {
200
+ selected = await inquirer.prompt(questionsCreateFunctionSelectTemplate(templates))
201
+ }
202
+
203
+
204
+
186
205
const copyRecursiveSync = (src, dest) => {
187
206
let exists = fs.existsSync(src);
188
207
let stats = exists && fs.statSync(src);
@@ -199,9 +218,9 @@ const initFunction = async () => {
199
218
fs.copyFileSync(src, dest);
200
219
}
201
220
};
202
- copyRecursiveSync(path.join(functionDir, answers.runtime.id ), functionDir);
221
+ copyRecursiveSync(path.join(runtimeDir, selected.template ), functionDir);
203
222
204
- fs.rmSync(`${functionDir}/${answers.runtime.id}` , { recursive: true, force: true });
223
+ fs.rmSync(templatesDir , { recursive: true, force: true });
205
224
206
225
const readmePath = path.join(process.cwd(), 'functions', functionId, 'README.md');
207
226
const readmeFile = fs.readFileSync(readmePath).toString();
0 commit comments