Skip to content

Commit fb8e08a

Browse files
committed
feat: Init to local only & database creation
1 parent 406ef22 commit fb8e08a

File tree

2 files changed

+78
-63
lines changed

2 files changed

+78
-63
lines changed

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

Lines changed: 51 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const initProject = async ({ organizationId, projectId, projectName } = {}) => {
4444

4545
if (organizationId && projectId && projectName) {
4646
answers = {
47-
project: { id: projectId, name: projectName},
47+
project: { id: projectId, name: projectName },
4848
organization: { id: organizationId },
4949
start: 'existing'
5050
}
@@ -86,62 +86,59 @@ const initProject = async ({ organizationId, projectId, projectName } = {}) => {
8686
}
8787

8888
const initBucket = async () => {
89-
let response = {}
9089
const answers = await inquirer.prompt(questionsCreateBucket)
9190

92-
try {
93-
response = await storageCreateBucket({
94-
bucketId: answers.id,
95-
name: answers.bucket,
96-
fileSecurity: answers.fileSecurity.toLowerCase() === 'yes',
97-
enabled: true,
98-
parseOutput: false
99-
})
100-
101-
localConfig.addBucket(response);
102-
success();
103-
} catch (e) {
104-
error(e.getMessage ?? 'Unknown error occurred. Please try again');
105-
}
91+
localConfig.addBucket({
92+
$id: answers.id === 'unique()' ? ID.unique() : answers.id,
93+
name: answers.bucket,
94+
fileSecurity: answers.fileSecurity.toLowerCase() === 'yes',
95+
enabled: true,
96+
});
97+
success();
10698
};
10799

108100
const initCollection = async () => {
109-
let response = {}
110101
const answers = await inquirer.prompt(questionsCreateCollection)
102+
const newDatabase = (answers.method ?? '').toLowerCase() !== 'existing';
111103

112-
try {
113-
response = await databasesCreateCollection({
114-
databaseId: answers.database,
115-
collectionId: answers.id,
116-
name: answers.collection,
117-
documentSecurity: answers.documentSecurity.toLowerCase() === 'yes',
118-
enabled: true,
119-
parseOutput: false
120-
})
104+
if (!newDatabase) {
105+
answers.database_id = answers.database.$id;
106+
answers.database_name = answers.database.name;
107+
}
121108

122-
localConfig.addCollection(response);
123-
success();
124-
} catch (e) {
125-
error(e.getMessage ?? 'Unknown error occurred. Please try again');
109+
const databaseId = answers.database_id === 'unique()' ? ID.unique() : answers.database_id;
110+
111+
if (newDatabase || !localConfig.getDatabase(answers.database_id).$id) {
112+
localConfig.addDatabase({
113+
$id: databaseId,
114+
name: answers.database_name,
115+
enabled: true
116+
});
126117
}
118+
119+
localConfig.addCollection({
120+
$id: answers.id === 'unique()' ? ID.unique() : answers.id,
121+
databaseId: databaseId,
122+
name: answers.collection,
123+
documentSecurity: answers.documentSecurity.toLowerCase() === 'yes',
124+
attributes: [],
125+
indexes: [],
126+
enabled: true,
127+
});
128+
129+
success();
127130
};
128131

129132
const initTopic = async () => {
130-
let response = {}
131133
const answers = await inquirer.prompt(questionsCreateMessagingTopic)
132134

133-
try {
134-
response = await messagingCreateTopic({
135-
topicId: answers.id,
136-
name: answers.topic,
137-
parseOutput: false
138-
})
135+
localConfig.addMessagingTopic({
136+
$id: answers.id === 'unique()' ? ID.unique() : answers.id,
137+
name: answers.topic,
139138

140-
localConfig.addMessagingTopic(response);
141-
success();
142-
} catch (e) {
143-
error(e.message ?? 'Unknown error occurred. Please try again');
144-
}
139+
});
140+
141+
success();
145142
};
146143

147144
const initFunction = async () => {
@@ -172,14 +169,7 @@ const initFunction = async () => {
172169
log(`Installation command for this runtime not found. You will be asked to configure the install command when you first push the function.`);
173170
}
174171

175-
let response = await functionsCreate({
176-
functionId,
177-
name: answers.name,
178-
runtime: answers.runtime.id,
179-
entrypoint: answers.runtime.entrypoint || '',
180-
commands: answers.runtime.commands || '',
181-
parseOutput: false
182-
})
172+
183173

184174
fs.mkdirSync(functionDir, "777");
185175
fs.mkdirSync(templatesDir, "777");
@@ -252,17 +242,17 @@ const initFunction = async () => {
252242
fs.writeFileSync(readmePath, newReadmeFile.join('\n'));
253243

254244
let data = {
255-
$id: response['$id'],
256-
name: response.name,
257-
runtime: response.runtime,
258-
execute: response.execute,
259-
events: response.events,
260-
schedule: response.schedule,
261-
timeout: response.timeout,
262-
enabled: response.enabled,
263-
logging: response.logging,
264-
entrypoint: response.entrypoint,
265-
commands: response.commands,
245+
$id: functionId,
246+
name: answers.name,
247+
runtime: answers.runtime.id,
248+
execute: [],
249+
events: [],
250+
schedule: "",
251+
timeout: 15,
252+
enabled: true,
253+
logging: true,
254+
entrypoint: answers.runtime.entrypoint || '',
255+
commands: answers.runtime.commands || '',
266256
ignore: answers.runtime.ignore || null,
267257
path: `functions/${functionId}`,
268258
};

templates/cli/lib/questions.js.twig

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,16 @@ const questionsCreateBucket = [
328328
];
329329

330330
const questionsCreateCollection = [
331+
{
332+
type: "list",
333+
name: "method",
334+
message: "What database would you like to use for your collection",
335+
choices: ["New", "Existing"],
336+
when: async () => {
337+
const res = await databasesList({ queries: [], parseOutput: false });
338+
return res.total !== 0;
339+
}
340+
},
331341
{
332342
type: "list",
333343
name: "database",
@@ -338,7 +348,7 @@ const questionsCreateCollection = [
338348
let choices = databases.map((database, idx) => {
339349
return {
340350
name: `${database.name} (${database.$id})`,
341-
value: database.$id
351+
value: { $id: database.$id, name: database.name }
342352
}
343353
})
344354

@@ -347,7 +357,22 @@ const questionsCreateCollection = [
347357
}
348358

349359
return choices;
350-
}
360+
},
361+
when: (answers) => (answers.method ?? '').toLowerCase() === 'existing'
362+
},
363+
{
364+
type: "input",
365+
name: "database_name",
366+
message: "What would you like to name your database?",
367+
default: "My Awesome Database",
368+
when: (answers) => (answers.method ?? '').toLowerCase() !== 'existing'
369+
},
370+
{
371+
type: "input",
372+
name: "database_id",
373+
message: "What ID would you like to have for your database?",
374+
default: "unique()",
375+
when: (answers) => (answers.method ?? '').toLowerCase() !== 'existing'
351376
},
352377
{
353378
type: "input",

0 commit comments

Comments
 (0)