From cd5915d6ea662db4caf939eb9c8ddaba9b453ca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sat, 27 Jul 2024 11:37:44 +0000 Subject: [PATCH] RC updage --- lib/commands/push.js | 4 ++-- lib/commands/run.js | 30 +++++++++++++------------ lib/emulation/docker.js | 50 ++++++++++++++++++++--------------------- 3 files changed, 43 insertions(+), 41 deletions(-) diff --git a/lib/commands/push.js b/lib/commands/push.js index c0c8bfe..ea3dd21 100644 --- a/lib/commands/push.js +++ b/lib/commands/push.js @@ -341,7 +341,7 @@ const awaitPools = { } const approveChanges = async (resource, resourceGetFunction, keys, resourceName, resourcePlural) => { - log('Checking for changes'); + log('Checking for changes ...'); const changes = []; await Promise.all(resource.map(async (localResource) => { @@ -881,7 +881,7 @@ const pushSettings = async () => { const remoteSettings = localConfig.createSettingsObject(response ?? {}); const localSettings = localConfig.getProject().projectSettings ?? {}; - log('Checking for changes'); + log('Checking for changes ...'); const changes = []; changes.push(...(getObjectChanges(remoteSettings, localSettings, 'services', 'Service'))); diff --git a/lib/commands/run.js b/lib/commands/run.js index a44eb11..71baf0c 100644 --- a/lib/commands/run.js +++ b/lib/commands/run.js @@ -17,7 +17,9 @@ const { systemHasCommand, isPortTaken, getAllFiles } = require('../utils'); const { runtimeNames, systemTools, JwtManager, Queue } = require('../emulation/utils'); const { dockerStop, dockerCleanup, dockerStart, dockerBuild, dockerPull } = require('../emulation/docker'); -const runFunction = async ({ port, functionId, noVariables, noReload, userId } = {}) => { +const runFunction = async ({ port, functionId, variables, reload, userId } = {}) => { + console.log(variables); + console.log(reload); // Selection if(!functionId) { const answers = await inquirer.prompt(questionsRunFunctions[0]); @@ -113,9 +115,9 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } = } const userVariables = {}; - const variables = {}; + const allVariables = {}; - if(!noVariables) { + if(variables) { try { const { variables: remoteVariables } = await paginate(functionsListVariables, { functionId: func['$id'], @@ -123,7 +125,7 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } = }, 100, 'variables'); remoteVariables.forEach((v) => { - variables[v.key] = v.value; + allVariables[v.key] = v.value; userVariables[v.key] = v.value; }); } catch(err) { @@ -137,18 +139,18 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } = const env = parseDotenv(fs.readFileSync(envPath).toString() ?? ''); Object.keys(env).forEach((key) => { - variables[key] = env[key]; + allVariables[key] = env[key]; userVariables[key] = env[key]; }); } - variables['APPWRITE_FUNCTION_API_ENDPOINT'] = globalConfig.getFrom('endpoint'); - variables['APPWRITE_FUNCTION_ID'] = func.$id; - variables['APPWRITE_FUNCTION_NAME'] = func.name; - variables['APPWRITE_FUNCTION_DEPLOYMENT'] = ''; // TODO: Implement when relevant - variables['APPWRITE_FUNCTION_PROJECT_ID'] = localConfig.getProject().projectId; - variables['APPWRITE_FUNCTION_RUNTIME_NAME'] = runtimeNames[runtimeName] ?? ''; - variables['APPWRITE_FUNCTION_RUNTIME_VERSION'] = func.runtime; + allVariables['APPWRITE_FUNCTION_API_ENDPOINT'] = globalConfig.getFrom('endpoint'); + allVariables['APPWRITE_FUNCTION_ID'] = func.$id; + allVariables['APPWRITE_FUNCTION_NAME'] = func.name; + allVariables['APPWRITE_FUNCTION_DEPLOYMENT'] = ''; // TODO: Implement when relevant + allVariables['APPWRITE_FUNCTION_PROJECT_ID'] = localConfig.getProject().projectId; + allVariables['APPWRITE_FUNCTION_RUNTIME_NAME'] = runtimeNames[runtimeName] ?? ''; + allVariables['APPWRITE_FUNCTION_RUNTIME_VERSION'] = func.runtime; try { await JwtManager.setup(userId, func.scopes ?? []); @@ -162,7 +164,7 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } = headers['x-appwrite-event'] = ''; headers['x-appwrite-user-id'] = userId ?? ''; headers['x-appwrite-user-jwt'] = JwtManager.userJwt ?? ''; - variables['OPEN_RUNTIMES_HEADERS'] = JSON.stringify(headers); + allVariables['OPEN_RUNTIMES_HEADERS'] = JSON.stringify(headers); if(Object.keys(userVariables).length > 0) { drawTable(Object.keys(userVariables).map((key) => ({ @@ -180,7 +182,7 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } = process.stdout.write(chalk.white(`${data}\n`)); }); - if(!noReload) { + if(reload) { const ignorer = ignore(); ignorer.add('.appwrite'); ignorer.add('code.tar.gz'); diff --git a/lib/emulation/docker.js b/lib/emulation/docker.js index 0e4348d..179d905 100644 --- a/lib/emulation/docker.js +++ b/lib/emulation/docker.js @@ -51,6 +51,31 @@ async function dockerBuild(func, variables) { const id = func.$id; + const ignorer = ignore(); + ignorer.add('.appwrite'); + if (func.ignore) { + ignorer.add(func.ignore); + } else if (fs.existsSync(path.join(functionDir, '.gitignore'))) { + ignorer.add(fs.readFileSync(path.join(functionDir, '.gitignore')).toString()); + } + + const files = getAllFiles(functionDir).map((file) => path.relative(functionDir, file)).filter((file) => !ignorer.ignores(file)); + const tmpBuildPath = path.join(functionDir, '.appwrite/tmp-build'); + if (!fs.existsSync(tmpBuildPath)) { + fs.mkdirSync(tmpBuildPath, { recursive: true }); + } + + for(const f of files) { + const filePath = path.join(tmpBuildPath, f); + const fileDir = path.dirname(filePath); + if (!fs.existsSync(fileDir)) { + fs.mkdirSync(fileDir, { recursive: true }); + } + + const sourcePath = path.join(functionDir, f); + fs.copyFileSync(sourcePath, filePath); + } + const params = [ 'run' ]; params.push('--name', id); params.push('-v', `${tmpBuildPath}/:/mnt/code:rw`); @@ -131,31 +156,6 @@ async function dockerStart(func, variables, port) { // Pack function files const functionDir = path.join(process.cwd(), func.path); - const ignorer = ignore(); - ignorer.add('.appwrite'); - if (func.ignore) { - ignorer.add(func.ignore); - } else if (fs.existsSync(path.join(functionDir, '.gitignore'))) { - ignorer.add(fs.readFileSync(path.join(functionDir, '.gitignore')).toString()); - } - - const files = getAllFiles(functionDir).map((file) => path.relative(functionDir, file)).filter((file) => !ignorer.ignores(file)); - const tmpBuildPath = path.join(functionDir, '.appwrite/tmp-build'); - if (!fs.existsSync(tmpBuildPath)) { - fs.mkdirSync(tmpBuildPath, { recursive: true }); - } - - for(const f of files) { - const filePath = path.join(tmpBuildPath, f); - const fileDir = path.dirname(filePath); - if (!fs.existsSync(fileDir)) { - fs.mkdirSync(fileDir, { recursive: true }); - } - - const sourcePath = path.join(functionDir, f); - fs.copyFileSync(sourcePath, filePath); - } - const runtimeChunks = func.runtime.split("-"); const runtimeVersion = runtimeChunks.pop(); const runtimeName = runtimeChunks.join("-");