Skip to content

Commit

Permalink
chore: Module to commonjs
Browse files Browse the repository at this point in the history
  • Loading branch information
byawitz committed Jun 19, 2024
1 parent 6711fbd commit 88b97a9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
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
}

0 comments on commit 88b97a9

Please sign in to comment.