From 88b97a9a1fa463cca964b1da320daabb8717a38c Mon Sep 17 00:00:00 2001 From: Binyamin Yawitz <316103+byawitz@users.noreply.github.com> Date: Wed, 19 Jun 2024 08:41:22 -0400 Subject: [PATCH] chore: Module to commonjs --- templates/cli/lib/emulation/docker.js.twig | 35 ++++++++++++++-------- templates/cli/lib/emulation/utils.js.twig | 24 ++++++++++----- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/templates/cli/lib/emulation/docker.js.twig b/templates/cli/lib/emulation/docker.js.twig index fa0b942c5..08544ad97 100644 --- a/templates/cli/lib/emulation/docker.js.twig +++ b/templates/cli/lib/emulation/docker.js.twig @@ -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', @@ -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("-"); @@ -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("-"); @@ -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}`); }); @@ -95,10 +95,10 @@ 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."); @@ -106,8 +106,8 @@ export async function dockerStart(func, variables, port) { 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("-"); @@ -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 @@ -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(); @@ -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); - } -} \ No newline at end of file + } +} + +module.exports = { + dockerStop, + dockerPull, + dockerBuild, + dockerStart, + dockerCleanup, + dockerStopActive, +} diff --git a/templates/cli/lib/emulation/utils.js.twig b/templates/cli/lib/emulation/utils.js.twig index a7011f3eb..eefa096b6 100644 --- a/templates/cli/lib/emulation/utils.js.twig +++ b/templates/cli/lib/emulation/utils.js.twig @@ -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', @@ -15,7 +17,7 @@ export const runtimeNames = { 'bun': 'Bun' }; -export const systemTools = { +const systemTools = { 'node': { isCompiled: false, startCommand: "node src/server.js", @@ -78,7 +80,7 @@ export const systemTools = { }, }; -export const JwtManager = { +const JwtManager = { userJwt: null, functionJwt: null, @@ -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 @@ -127,7 +129,7 @@ export const JwtManager = { } }; -export const Queue = { +const Queue = { files: [], locked: false, events: new EventEmitter(), @@ -161,4 +163,12 @@ export const Queue = { this.debounce = null; }, 300); } -}; \ No newline at end of file +}; + +module.exports = { + openRuntimesVersion, + runtimeNames, + systemTools, + JwtManager, + Queue +}