Skip to content

Commit ed4089a

Browse files
committed
feat: Setting function folder name as unique id or custom
1 parent 7d6b9bf commit ed4089a

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

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.id);
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.id} ) 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.id, '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.id}`,
159+
path: `functions/${functionId}`,
158160
};
159161

160162
localConfig.addFunction(data);

0 commit comments

Comments
 (0)