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 #882

Merged
merged 4 commits into from
Jun 19, 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 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 } = require("./lib/commands/generic");
const { login, logout, whoami, migrate } = require("./lib/commands/generic");
const { init } = require("./lib/commands/init");
const { pull } = require("./lib/commands/pull");
const { run } = require("./lib/commands/run");
Expand Down
2 changes: 1 addition & 1 deletion templates/cli/lib/commands/pull.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ pull
.command("function")
.alias("functions")
.description("Pulling your {{ spec.title|caseUcfirst }} cloud function")
.action(actionRunner(pullFunction))
.action(actionRunner(pullFunctions))

pull
.command("collection")
Expand Down
5 changes: 5 additions & 0 deletions templates/cli/lib/commands/push.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,11 @@ const pushFunction = async ({ functionId, async, returnOnZero } = { returnOnZero
logging: func.logging,
entrypoint: func.entrypoint,
commands: func.commands,
providerRepositoryId: func.providerRepositoryId ?? "",
installationId: func.installationId ?? '',
providerBranch: func.providerBranch ?? '',
providerRootDirectory: func.providerRootDirectory ?? '',
providerSilentMode: func.providerSilentMode ?? false,
vars: JSON.stringify(response.vars),
parseOutput: false
});
Expand Down
35 changes: 22 additions & 13 deletions templates/cli/lib/emulation/docker.js.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const activeDockerIds = {};

export async function dockerStop(id) {
async function dockerStop(id) {
delete activeDockerIds[id];
const stopProcess = childProcess.spawn('docker', ['rm', '--force', id], {
stdio: 'pipe',
Expand All @@ -9,7 +9,7 @@ export async function dockerStop(id) {
await new Promise((res) => { stopProcess.on('close', res) });
}

export async function dockerPull(func) {
async function dockerPull(func) {
log('Pulling Docker image of function runtime ...');

const runtimeChunks = func.runtime.split("-");
Expand All @@ -29,7 +29,7 @@ export async function dockerPull(func) {
await new Promise((res) => { pullProcess.on('close', res) });
}

export async function dockerBuild(func, variables) {
async function dockerBuild(func, variables) {
log('Building function using Docker ...');

const runtimeChunks = func.runtime.split("-");
Expand Down Expand Up @@ -63,7 +63,7 @@ export async function dockerBuild(func, variables) {
buildProcess.stdout.on('data', (data) => {
process.stdout.write(`\n${data}`);
});

buildProcess.stderr.on('data', (data) => {
process.stderr.write(`\n${data}`);
});
Expand Down Expand Up @@ -95,19 +95,19 @@ export async function dockerBuild(func, variables) {
const tempPath = path.join(process.cwd(), func.path, 'code.tar.gz');
if (fs.existsSync(tempPath)) {
fs.rmSync(tempPath, { force: true });
}
}
}

export async function dockerStart(func, variables, port) {
async function dockerStart(func, variables, port) {
log('Starting function using Docker ...');

log("Permissions, events, CRON and timeouts dont apply when running locally.");

log('💡 Hint: Function automatically restarts when you edit your code.');

success(`Visit http://localhost:${port}/ to execute your function.`);


const runtimeChunks = func.runtime.split("-");
const runtimeVersion = runtimeChunks.pop();
const runtimeName = runtimeChunks.join("-");
Expand Down Expand Up @@ -136,7 +136,7 @@ export async function dockerStart(func, variables, port) {
params.push('-v', `${functionDir}/.appwrite/errors.txt:/mnt/logs/dev_errors.log:rw`);
params.push('-v', `${functionDir}/.appwrite/build.tar.gz:/mnt/code/code.tar.gz:ro`);
params.push(imageName, 'sh', '-c', `helpers/start.sh "${tool.startCommand}"`);

childProcess.spawn('docker', params, {
stdio: 'pipe',
pwd: functionDir
Expand All @@ -145,7 +145,7 @@ export async function dockerStart(func, variables, port) {
activeDockerIds[id] = true;
}

export async function dockerCleanup() {
async function dockerCleanup() {
await dockerStop();

const functions = localConfig.getFunctions();
Expand All @@ -162,9 +162,18 @@ export async function dockerCleanup() {
}
}

export async function dockerStopActive() {
async function dockerStopActive() {
const ids = Object.keys(activeDockerIds);
for await (const id of ids) {
await dockerStop(id);
}
}
}
}

module.exports = {
dockerStop,
dockerPull,
dockerBuild,
dockerStart,
dockerCleanup,
dockerStopActive,
}
24 changes: 17 additions & 7 deletions templates/cli/lib/emulation/utils.js.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export const openRuntimesVersion = 'v3';
const EventEmitter = require('node:events');

export const runtimeNames = {
const openRuntimesVersion = 'v3';

const runtimeNames = {
'node': 'Node.js',
'php': 'PHP',
'ruby': 'Ruby',
Expand All @@ -15,7 +17,7 @@ export const runtimeNames = {
'bun': 'Bun'
};

export const systemTools = {
const systemTools = {
'node': {
isCompiled: false,
startCommand: "node src/server.js",
Expand Down Expand Up @@ -78,7 +80,7 @@ export const systemTools = {
},
};

export const JwtManager = {
const JwtManager = {
userJwt: null,
functionJwt: null,

Expand Down Expand Up @@ -115,7 +117,7 @@ export const JwtManager = {
});
this.userJwt = userResponse.jwt;
}

const functionResponse = await projectsCreateJWT({
projectId: localConfig.getProject().projectId,
// TODO: Once we have endpoint for this, use it
Expand All @@ -127,7 +129,7 @@ export const JwtManager = {
}
};

export const Queue = {
const Queue = {
files: [],
locked: false,
events: new EventEmitter(),
Expand Down Expand Up @@ -161,4 +163,12 @@ export const Queue = {
this.debounce = null;
}, 300);
}
};
};

module.exports = {
openRuntimesVersion,
runtimeNames,
systemTools,
JwtManager,
Queue
}
Loading