Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat cli g2 fixes #886

Merged
merged 19 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function getSSLPage($url) {
$platform = 'console';
// $platform = 'server';

$spec = getSSLPage("https://raw.githubusercontent.com/appwrite/appwrite/1.5.x/app/config/specs/swagger2-latest-{$platform}.json");
$spec = getSSLPage("https://raw.githubusercontent.com/appwrite/appwrite/1.6.x/app/config/specs/swagger2-latest-{$platform}.json");

if(empty($spec)) {
throw new Exception('Failed to fetch spec from Appwrite server');
Expand Down
3 changes: 2 additions & 1 deletion templates/cli/index.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const { commandDescriptions, cliConfig } = require("./lib/parser");
const { client } = require("./lib/commands/generic");
const inquirer = require("inquirer");
{% if sdk.test != "true" %}
const { login, logout, whoami, migrate } = require("./lib/commands/generic");
const { login, logout, whoami, migrate, register } = require("./lib/commands/generic");
const { init } = require("./lib/commands/init");
const { pull } = require("./lib/commands/pull");
const { run } = require("./lib/commands/run");
Expand Down Expand Up @@ -62,6 +62,7 @@ program
.showSuggestionAfterError()
{% if sdk.test != "true" %}
.addCommand(whoami)
.addCommand(register)
.addCommand(login)
.addCommand(init)
.addCommand(pull)
Expand Down
10 changes: 10 additions & 0 deletions templates/cli/lib/commands/generic.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ const loginCommand = async ({ email, password, endpoint, mfa, code }) => {
} else {
globalConfig.removeSession(id);
globalConfig.setCurrentSession(oldCurrent);
if(endpoint !== DEFAULT_ENDPOINT && error.response === 'user_invalid_credentials'){
log('Use the --endpoint option for self-hosted instances')
}
throw error;
}
}
Expand Down Expand Up @@ -128,6 +131,12 @@ const whoami = new Command("whoami")
drawTable(data)
}));

const register = new Command("register")
.description(commandDescriptions['register'])
.action(actionRunner(async () => {
log('Visit https://cloud.appwrite.io/register to create an account')
}));

const login = new Command("login")
.description(commandDescriptions['login'])
.option(`--email [email]`, `User email`)
Expand Down Expand Up @@ -299,6 +308,7 @@ module.exports = {
{% if sdk.test != "true" %}
loginCommand,
whoami,
register,
login,
logout,
{% endif %}
Expand Down
11 changes: 6 additions & 5 deletions templates/cli/lib/commands/init.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ const initProject = async ({ organizationId, projectId, projectName } = {}) => {
answers.project = {};
answers.organization = {};

answers.organization.id = organizationId ?? (await inquirer.prompt(questionsInitProject[2])).organization;
answers.organization = organizationId ?? (await inquirer.prompt(questionsInitProject[2])).organization;
answers.project.name = projectName ?? (await inquirer.prompt(questionsInitProject[3])).project;
answers.project.id = projectId ?? (await inquirer.prompt(questionsInitProject[4])).id;
answers.project = projectId ?? (await inquirer.prompt(questionsInitProject[4])).id;

try {
await projectsGet({ projectId, parseOutput: false });
} catch (e) {
if (e.code === 404) {
answers.start = 'new';
answers.id = answers.project.id;
answers.id = answers.project;
answers.project = answers.project.name;
} else {
throw e;
Expand All @@ -74,13 +74,13 @@ const initProject = async ({ organizationId, projectId, projectName } = {}) => {
response = await projectsCreate({
projectId: answers.id,
name: answers.project,
teamId: answers.organization.id,
teamId: answers.organization,
parseOutput: false
})

localConfig.setProject(response['$id']);
} else {
localConfig.setProject(answers.project.id);
localConfig.setProject(answers.project);
}

success();
Expand Down Expand Up @@ -273,6 +273,7 @@ const initFunction = async () => {

localConfig.addFunction(data);
success();
log(`* Use 'appwrite dev function' for local development\n${' '.repeat(7)}* Use 'appwrite push functions' to push the function to your {{ spec.title|caseUcfirst }} instance`)
}

const init = new Command("init")
Expand Down
8 changes: 4 additions & 4 deletions templates/cli/lib/commands/run.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } =
}

if(!port) {
let portFound = fale;
let portFound = false;
port = 3000;
while(port < 3100) {
const taken = await isPortTaken(port);
Expand Down Expand Up @@ -194,7 +194,7 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } =
await dockerStart(func, variables, port);
} else {
log('Hot-swapping function files ...');

const functionPath = path.join(process.cwd(), func.path);
const hotSwapPath = path.join(functionPath, '.appwrite/hot-swap');
const buildPath = path.join(functionPath, '.appwrite/build.tar.gz');
Expand Down Expand Up @@ -226,7 +226,7 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } =
const filePath = path.join(hotSwapPath, f);
if (fs.existsSync(filePath)) {
fs.rmSync(filePath, { force: true });
}
}

const fileDir = path.dirname(filePath);
if (!fs.existsSync(fileDir)) {
Expand All @@ -244,7 +244,7 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } =
cwd: hotSwapPath,
file: buildPath
}, ['.']);

fs.rmSync(hotSwapPath, { recursive: true, force: true });

await dockerStart(func, variables, port);
Expand Down
11 changes: 8 additions & 3 deletions templates/cli/lib/config.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,16 @@ class Local extends Config {
};
}

setProject(projectId, projectName = '', projectSettings = {}) {
setProject(projectId, projectName = '', projectSettings = undefined) {
this.set("projectId", projectId);

if (projectName !== '') {
this.set("projectName", projectName);
}

if (projectSettings === undefined) {
return;
}

const settings = {
services: {
Expand Down Expand Up @@ -414,8 +417,10 @@ class Global extends Config {
return this.get(Global.PREFERENCE_CURRENT);
}

setCurrentSession(endpoint) {
this.set(Global.PREFERENCE_CURRENT, endpoint);
setCurrentSession(session) {
if (session !== undefined) {
this.set(Global.PREFERENCE_CURRENT, session);
}
}

getSessionIds() {
Expand Down
8 changes: 8 additions & 0 deletions templates/cli/lib/emulation/docker.js.twig
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
const childProcess = require('child_process');
const { localConfig } = require("../config");
const path = require('path');
const fs = require('fs');
const { log,success } = require("../parser");
const { openRuntimesVersion, systemTools } = require("./utils");
const ID = require("../id");

const activeDockerIds = {};

async function dockerStop(id) {
Expand Down
3 changes: 3 additions & 0 deletions templates/cli/lib/emulation/utils.js.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const EventEmitter = require('node:events');
const { projectsCreateJWT } = require('../commands/projects');
const { localConfig } = require("../config");


const openRuntimesVersion = 'v3';

Expand Down
1 change: 1 addition & 0 deletions templates/cli/lib/parser.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ const commandDescriptions = {
"login": `The login command allows you to authenticate and manage a user account.`,
"logout": `The logout command allows you to logout of your {{ spec.title|caseUcfirst }} account.`,
"whoami": `The whoami command gives information about the currently logged in user.`,
"register": `Outputs the link to create an {{ spec.title|caseUcfirst }} account..`,
"console" : `The console command allows gives you access to the APIs used by the Appwrite console.`,
"assistant": `The assistant command allows you to interact with the Appwrite Assistant AI`,
"messaging": `The messaging command allows you to send messages.`,
Expand Down
14 changes: 4 additions & 10 deletions templates/cli/lib/questions.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,12 @@ const questionsInitProject = [
let choices = teams.map((team, idx) => {
return {
name: `${team.name} (${team['$id']})`,
value: {
name: team.name,
id: team['$id']
}
value: team['$id']
}
})

if (choices.length == 0) {
throw new Error("No organizations found. Please create a new organization.")
throw new Error(`No organizations found. Please create a new organization at ${globalConfig.getEndpoint().replace('/v1', '/console/onboarding')}`)
}

return choices;
Expand Down Expand Up @@ -199,10 +196,7 @@ const questionsInitProject = [
let choices = projects.map((project) => {
return {
name: `${project.name} (${project['$id']})`,
value: {
name: project.name,
id: project['$id']
}
value: project['$id']
}
})

Expand Down Expand Up @@ -781,7 +775,7 @@ const questionsRunFunctions = [
choices: () => {
let functions = localConfig.getFunctions();
if (functions.length === 0) {
throw new Error("No functions found in the current directory.");
throw new Error("No functions found in the current directory. Use 'appwrite init function' to create one");
}
let choices = functions.map((func, idx) => {
return {
Expand Down
Loading