Skip to content

Commit cf290e2

Browse files
Merge pull request #857 from appwrite/feat-functions-templates
feat(cli): Creating function from all available templates
2 parents e587222 + 64726b2 commit cf290e2

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const ID = require("../id");
1212
const { localConfig, globalConfig } = require("../config");
1313
const {
1414
questionsCreateFunction,
15+
questionsCreateFunctionSelectTemplate,
1516
questionsCreateBucket,
1617
questionsCreateMessagingTopic,
1718
questionsCreateCollection,
@@ -133,6 +134,8 @@ const initFunction = async () => {
133134

134135
const functionId = answers.id === 'unique()' ? ID.unique() : answers.id;
135136
const functionDir = path.join(functionFolder, functionId);
137+
const templatesDir = path.join(functionFolder, `${functionId}-templates`);
138+
const runtimeDir = path.join(templatesDir, answers.runtime.name);
136139

137140
if (fs.existsSync(functionDir)) {
138141
throw new Error(`( ${functionId} ) already exists in the current directory. Please choose another name.`);
@@ -156,21 +159,24 @@ const initFunction = async () => {
156159
})
157160

158161
fs.mkdirSync(functionDir, "777");
162+
fs.mkdirSync(templatesDir, "777");
159163

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}`;
161167

162-
let gitPullCommands = `git sparse-checkout add ${answers.runtime.id}`;
163168

164169
/* Force use CMD as powershell does not support && */
165170
if (process.platform === 'win32') {
166171
gitInitCommands = 'cmd /c "' + gitInitCommands + '"';
167172
gitPullCommands = 'cmd /c "' + gitPullCommands + '"';
168173
}
169174

175+
log('Loading templates...');
170176
/* Execute the child process but do not print any std output */
171177
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 });
174180
} catch (error) {
175181
/* Specialised errors with recommended actions to take */
176182
if (error.message.includes('error: unknown option')) {
@@ -182,7 +188,20 @@ const initFunction = async () => {
182188
}
183189
}
184190

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+
186205
const copyRecursiveSync = (src, dest) => {
187206
let exists = fs.existsSync(src);
188207
let stats = exists && fs.statSync(src);
@@ -199,9 +218,9 @@ const initFunction = async () => {
199218
fs.copyFileSync(src, dest);
200219
}
201220
};
202-
copyRecursiveSync(path.join(functionDir, answers.runtime.id), functionDir);
221+
copyRecursiveSync(path.join(runtimeDir, selected.template), functionDir);
203222

204-
fs.rmSync(`${functionDir}/${answers.runtime.id}`, { recursive: true, force: true });
223+
fs.rmSync(templatesDir, { recursive: true, force: true });
205224

206225
const readmePath = path.join(process.cwd(), 'functions', functionId, 'README.md');
207226
const readmeFile = fs.readFileSync(readmePath).toString();

templates/cli/lib/questions.js.twig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ const questionsCreateFunction = [
263263
name: `${runtime.name} (${runtime['$id']})`,
264264
value: {
265265
id: runtime['$id'],
266+
name: runtime['$id'].split('-')[0],
266267
entrypoint: getEntrypoint(runtime['$id']),
267268
ignore: getIgnores(runtime['$id']),
268269
commands: getInstallCommand(runtime['$id'])
@@ -274,6 +275,23 @@ const questionsCreateFunction = [
274275
}
275276
];
276277

278+
const questionsCreateFunctionSelectTemplate = (templates) => {
279+
return [
280+
{
281+
type: "list",
282+
name: "template",
283+
message: "What template would you like to use?",
284+
choices: templates.map((template) => {
285+
const name =`${template[0].toUpperCase()}${template.split('').slice(1).join('')}`.replace(/[-_]/g,' ');
286+
287+
return { value: template, name }
288+
})
289+
}
290+
];
291+
};
292+
293+
294+
277295
const questionsCreateBucket = [
278296
{
279297
type: "input",
@@ -617,6 +635,7 @@ const questionsMfaChallenge = [
617635
module.exports = {
618636
questionsInitProject,
619637
questionsCreateFunction,
638+
questionsCreateFunctionSelectTemplate,
620639
questionsCreateBucket,
621640
questionsCreateCollection,
622641
questionsCreateMessagingTopic,

0 commit comments

Comments
 (0)