Skip to content

Commit

Permalink
Merge pull request #895 from appwrite/fix-cli-qa
Browse files Browse the repository at this point in the history
Feat: CLI QA improvements
  • Loading branch information
TorstenDittmann committed Jul 8, 2024
2 parents 591e07b + 8432002 commit b9cd545
Show file tree
Hide file tree
Showing 13 changed files with 484 additions and 420 deletions.
62 changes: 31 additions & 31 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion templates/cli/index.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const { login, logout, whoami, migrate, register } = require("./lib/commands/gen
const { init } = require("./lib/commands/init");
const { pull } = require("./lib/commands/pull");
const { run } = require("./lib/commands/run");
const { push } = require("./lib/commands/push");
const { push, deploy } = require("./lib/commands/push");
{% else %}
const { migrate } = require("./lib/commands/generic");
{% endif %}
Expand Down Expand Up @@ -67,6 +67,7 @@ program
.addCommand(init)
.addCommand(pull)
.addCommand(push)
.addCommand(deploy)
.addCommand(run)
.addCommand(logout)
{% endif %}
Expand Down
22 changes: 17 additions & 5 deletions templates/cli/lib/commands/generic.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { Command } = require("commander");
const Client = require("../client");
const { sdkForConsole } = require("../sdks");
const { globalConfig, localConfig } = require("../config");
const { actionRunner, success, parseBool, commandDescriptions, error, parse, log, drawTable, cliConfig } = require("../parser");
const { actionRunner, success, parseBool, commandDescriptions, error, parse, hint, log, drawTable, cliConfig } = require("../parser");
const ID = require("../id");
{% if sdk.test != "true" %}
const { questionsLogin, questionsLogout, questionsListFactors, questionsMfaChallenge } = require("../questions");
Expand All @@ -15,8 +15,20 @@ const loginCommand = async ({ email, password, endpoint, mfa, code }) => {
const oldCurrent = globalConfig.getCurrentSession();
let configEndpoint = endpoint ?? DEFAULT_ENDPOINT;

if(globalConfig.getCurrentSession() !== '') {
log('You are currently signed in as ' + globalConfig.getEmail());

if(globalConfig.getSessions().length === 1) {
hint('You can sign in and manage multiple accounts with Appwrite CLI');
}
}

const answers = email && password ? { email, password } : await inquirer.prompt(questionsLogin);

if(!answers.method) {
answers.method = 'login';
}

if (answers.method === 'select') {
const accountId = answers.accountId;

Expand Down Expand Up @@ -88,15 +100,15 @@ const loginCommand = async ({ email, password, endpoint, mfa, code }) => {
}
}

success("Signed in as user with ID: " + account.$id);
log("Next you can create or link to your project using 'appwrite init project'");
success("Successfully signed in as " + account.email);
hint("Next you can create or link to your project using 'appwrite init project'");
};

const whoami = new Command("whoami")
.description(commandDescriptions['whoami'])
.action(actionRunner(async () => {
if (globalConfig.getEndpoint() === '' || globalConfig.getCookie() === '') {
error("No user is signed in. To sign in, run: appwrite login ");
error("No user is signed in. To sign in, run 'appwrite login'");
return;
}

Expand All @@ -110,7 +122,7 @@ const whoami = new Command("whoami")
parseOutput: false
});
} catch (error) {
error("No user is signed in. To sign in, run: appwrite login");
error("No user is signed in. To sign in, run 'appwrite login'");
return;
}

Expand Down
41 changes: 26 additions & 15 deletions templates/cli/lib/commands/init.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { storageCreateBucket } = require("./storage");
const { messagingCreateTopic } = require("./messaging");
const { functionsCreate } = require("./functions");
const { databasesCreateCollection } = require("./databases");
const { pullResources } = require("./pull");
const ID = require("../id");
const { localConfig, globalConfig } = require("../config");
const {
Expand All @@ -18,10 +19,11 @@ const {
questionsCreateMessagingTopic,
questionsCreateCollection,
questionsInitProject,
questionsInitProjectAutopull,
questionsInitResources,
questionsCreateTeam
} = require("../questions");
const { success, log, error, actionRunner, commandDescriptions } = require("../parser");
const { cliConfig, success, log, hint, error, actionRunner, commandDescriptions } = require("../parser");
const { accountGet } = require("./account");
const { sdkForConsole } = require("../sdks");

Expand Down Expand Up @@ -56,7 +58,7 @@ const initProject = async ({ organizationId, projectId, projectName } = {}) => {
sdk: client
});
} catch (e) {
error('Error Session not found. Please run `appwrite login` to create a session');
error("Error Session not found. Please run 'appwrite login' to create a session");
process.exit(1);
}

Expand Down Expand Up @@ -104,11 +106,17 @@ const initProject = async ({ organizationId, projectId, projectName } = {}) => {

success(`Project successfully ${answers.start === 'existing' ? 'linked' : 'created'}. Details are now stored in appwrite.json file.`);

log("Next you can use 'appwrite init' to create resources in your project, or use 'appwrite pull' and 'appwite push' to synchronize your project.")

if(answers.start === 'existing') {
log("Since you connected to an existing project, we highly recommend to run 'appwrite pull all' to synchronize all of your existing resources.");
answers = await inquirer.prompt(questionsInitProjectAutopull);
if(answers.autopull) {
cliConfig.all = true;
await pullResources();
} else {
log("You can run 'appwrite pull all' to synchronize all of your existing resources.");
}
}

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 initBucket = async () => {
Expand Down Expand Up @@ -210,22 +218,24 @@ const initFunction = async () => {
log(`Installation command for this runtime not found. You will be asked to configure the install command when you first push the function.`);
}


fs.mkdirSync(functionDir, "777");
fs.mkdirSync(templatesDir, "777");
const repo = "https://github.com/{{ sdk.gitUserName }}/templates";
const api = `https://api.github.com/repos/{{ sdk.gitUserName }}/templates/contents/${answers.runtime.name}`
const templates = ['starter'];
let selected = undefined;

try {
const res = await fetch(api);
templates.push(...(await res.json()).map((template) => template.name));

selected = await inquirer.prompt(questionsCreateFunctionSelectTemplate(templates))
} catch {
// Not a problem will go with directory pulling
log('Loading templates...');
if(answers.template === 'starter') {
selected = { template: 'starter' };
} else {
try {
const res = await fetch(api);
const templates = [];
templates.push(...(await res.json()).map((template) => template.name));
selected = await inquirer.prompt(questionsCreateFunctionSelectTemplate(templates));
} catch {
// Not a problem will go with directory pulling
log('Loading templates...');
}
}

const sparse = (selected ? `${answers.runtime.name}/${selected.template}` : answers.runtime.name).toLowerCase();
Expand Down Expand Up @@ -257,6 +267,7 @@ const initFunction = async () => {

fs.rmSync(path.join(templatesDir, ".git"), { recursive: true });
if (!selected) {
const templates = [];
templates.push(...fs.readdirSync(runtimeDir, { withFileTypes: true })
.filter(item => item.isDirectory() && item.name !== 'starter')
.map(dirent => dirent.name));
Expand Down
Loading

0 comments on commit b9cd545

Please sign in to comment.