Skip to content

Commit 36a37b8

Browse files
Merge pull request #832 from appwrite/fix-cli-func-folder-name
Fix: CLI folder name (functions)
2 parents af7e4b5 + ed4089a commit 36a37b8

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

src/SDK/Language/CLI.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ public function getFiles(): array
152152
'destination' => 'lib/client.js',
153153
'template' => 'cli/lib/client.js.twig',
154154
],
155+
[
156+
'scope' => 'default',
157+
'destination' => 'lib/id.js',
158+
'template' => 'cli/lib/id.js.twig',
159+
],
155160
[
156161
'scope' => 'default',
157162
'destination' => 'lib/utils.js',

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const { databasesGet, databasesListCollections, databasesList } = require("./dat
1010
const { storageListBuckets } = require("./storage");
1111
const { sdkForConsole } = require("../sdks");
1212
const { localConfig } = require("../config");
13+
const ID = require("../id");
1314
const { paginate } = require("../paginate");
1415
const { questionsInitProject, questionsInitFunction, questionsInitCollection } = require("../questions");
1516
const { success, log, actionRunner, commandDescriptions } = require("../parser");
@@ -63,10 +64,11 @@ const initFunction = async () => {
6364
});
6465
}
6566

66-
const functionDir = path.join(functionFolder, answers.name);
67+
const functionId = answers.id === 'unique()' ? ID.unique() : answers.id;
68+
const functionDir = path.join(functionFolder, functionId);
6769

6870
if (fs.existsSync(functionDir)) {
69-
throw new Error(`( ${answers.name} ) already exists in the current directory. Please choose another name.`);
71+
throw new Error(`( ${functionId} ) already exists in the current directory. Please choose another name.`);
7072
}
7173

7274
if (!answers.runtime.entrypoint) {
@@ -78,7 +80,7 @@ const initFunction = async () => {
7880
}
7981

8082
let response = await functionsCreate({
81-
functionId: answers.id,
83+
functionId,
8284
name: answers.name,
8385
runtime: answers.runtime.id,
8486
entrypoint: answers.runtime.entrypoint || '',
@@ -134,7 +136,7 @@ const initFunction = async () => {
134136

135137
fs.rmSync(`${functionDir}/${answers.runtime.id}`, { recursive: true, force: true });
136138

137-
const readmePath = path.join(process.cwd(), 'functions', answers.name, 'README.md');
139+
const readmePath = path.join(process.cwd(), 'functions', functionId, 'README.md');
138140
const readmeFile = fs.readFileSync(readmePath).toString();
139141
const newReadmeFile = readmeFile.split('\n');
140142
newReadmeFile[0] = `# ${answers.name}`;
@@ -154,7 +156,7 @@ const initFunction = async () => {
154156
entrypoint: response.entrypoint,
155157
commands: response.commands,
156158
ignore: answers.runtime.ignore || null,
157-
path: `functions/${answers.name}`,
159+
path: `functions/${functionId}`,
158160
};
159161

160162
localConfig.addFunction(data);

templates/cli/lib/id.js.twig

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class ID {
2+
// Generate an hex ID based on timestamp
3+
// Recreated from https://www.php.net/manual/en/function.uniqid.php
4+
static #hexTimestamp() {
5+
const now = new Date();
6+
const sec = Math.floor(now.getTime() / 1000);
7+
const msec = now.getMilliseconds();
8+
9+
// Convert to hexadecimal
10+
const hexTimestamp = sec.toString(16) + msec.toString(16).padStart(5, '0');
11+
return hexTimestamp;
12+
}
13+
14+
static custom(id) {
15+
return id
16+
}
17+
18+
static unique(padding = 7) {
19+
// Generate a unique ID with padding to have a longer ID
20+
const baseId = ID.#hexTimestamp();
21+
let randomPadding = '';
22+
for (let i = 0; i < padding; i++) {
23+
const randomHexDigit = Math.floor(Math.random() * 16).toString(16);
24+
randomPadding += randomHexDigit;
25+
}
26+
return baseId + randomPadding;
27+
}
28+
}
29+
30+
module.exports = ID;

0 commit comments

Comments
 (0)