diff --git a/templates/cli/lib/commands/init.js.twig b/templates/cli/lib/commands/init.js.twig index 037d649cb..c7a179e33 100644 --- a/templates/cli/lib/commands/init.js.twig +++ b/templates/cli/lib/commands/init.js.twig @@ -20,6 +20,7 @@ const { questionsCreateCollection, questionsInitProject, questionsInitProjectAutopull, + questionsCreateOrganization, questionsInitResources, questionsCreateTeam } = require("../questions"); @@ -119,6 +120,27 @@ const initProject = async ({ organizationId, projectId, projectName } = {}) => { hint("Next you can use 'appwrite init' to create resources in your project, or use 'appwrite pull' and 'appwrite push' to synchronize your project.") } +const initOrganization = async () => { + const answers = await inquirer.prompt(questionsCreateOrganization) + + const sdk = await sdkForConsole(); + + localConfig.addTeam({ + $id: answers.id === 'unique()' ? ID.unique() : answers.id, + name: answers.bucket, + }); + + await sdk.forConsole.billing.createOrganization( + organizationId: answers.id === 'unique()' ? ID.unique() : answers.id, + name: answers.name, + billingPlan: answers.plan, + paymentMethodId: ID.unique(), + billingAddressId: ID.unique(), + ); + success(); + log("Next you can create projects, teams, collections, functions, messaging, and buckets."); +}; + const initBucket = async () => { const answers = await inquirer.prompt(questionsCreateBucket) @@ -338,6 +360,13 @@ init .option("--projectName ", "{{ spec.title|caseUcfirst }} project ID") .action(actionRunner(initProject)); +init + .command("organization") + .alias("organizations") + .description("Init a new {{ spec.title|caseUcfirst }} organization") + .option("--organizationId ", "{{ spec.title|caseUcfirst }} organization ID") + .action(actionRunner(initOrganization)); + init .command("function") .alias("functions") diff --git a/templates/cli/lib/config.js.twig b/templates/cli/lib/config.js.twig index 2c43f5338..8980c6b1c 100644 --- a/templates/cli/lib/config.js.twig +++ b/templates/cli/lib/config.js.twig @@ -558,6 +558,10 @@ class Global extends Config { this.setTo(Global.PREFERENCE_PROJECT, project); } + setOrganization(organization) { + this.setTo(Global.PREFERENCE_ORGANIZATION, organization); + } + getKey() { if (!this.hasFrom(Global.PREFERENCE_KEY)) { return ""; diff --git a/templates/cli/lib/questions.js.twig b/templates/cli/lib/questions.js.twig index 96aa3900a..c057af192 100644 --- a/templates/cli/lib/questions.js.twig +++ b/templates/cli/lib/questions.js.twig @@ -219,6 +219,75 @@ const questionsInitProjectAutopull = [ `Would you like to pull all resources from project you just linked?` }, ]; +const questionsInitOrganization = [ + { + type: "list", + name: "start", + message: "How would you like to start?", + choices: [ + { + name: "Create new organization", + value: "new" + }, + { + name: "Link directory to an existing organization", + value: "existing" + } + ] + }, + { + type: "search-list", + name: "organization", + message: "Choose your organization", + choices: async () => { + let client = await sdkForConsole(true); + const { teams } = await paginate(teamsList, { parseOutput: false, sdk: client }, 100, 'teams'); + + let choices = teams.map((team, idx) => { + return { + name: `${team.name} (${team['$id']})`, + value: team['$id'] + } + }) + + if (choices.length == 0) { + throw new Error(`No organizations found. Please create a new organization at ${globalConfig.getEndpoint().replace('/v1', '/console/onboarding')}`) + } + + return choices; + }, + when: (answer) => answer.start === 'existing' + }, + { + type: "list", + name: "plan", + message: "What's the billing plan you want for your organization?", + choices: [ + { + name: "Free plan", + value: "free" + }, + { + name: "Pro plan", + value: "pro" + } + ] + }, + { + type: "input", + name: "name", + message: "What would you like to name your organization?", + default: "My Awesome Organization", + when: (answer) => answer.start !== 'existing' + }, + { + type: "input", + name: "id", + message: "What ID would you like to have for your organization?", + default: "unique()", + when: (answer) => answer.start !== 'existing' + } +]; const questionsPullResources = [ { type: "list", @@ -843,6 +912,7 @@ const questionsRunFunctions = [ module.exports = { questionsInitProject, questionsInitProjectAutopull, + questionsInitOrganization, questionsCreateFunction, questionsCreateFunctionSelectTemplate, questionsCreateBucket,