Skip to content

Commit

Permalink
feat(dev-cli): auto-set memory-limit flag based on module requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterFarber committed Aug 28, 2024
1 parent 17f5917 commit 02237ea
Showing 1 changed file with 52 additions and 3 deletions.
55 changes: 52 additions & 3 deletions dev-cli/src/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import { Command, basename, resolve } from '../deps.js'
import { hostArgs, tagsArg } from '../utils.js'
import { VERSION } from '../versions.js'
import { parse } from "jsr:@std/yaml";

function walletArgs (wallet) {
function walletArgs(wallet) {
/**
* Use wallet in pwd by default
*/
Expand All @@ -23,7 +24,7 @@ function walletArgs (wallet) {
]
}

function contractSourceArgs (contractWasmPath) {
function contractSourceArgs(contractWasmPath) {
/**
* Use contract.wasm in pwd by default
*/
Expand All @@ -42,14 +43,62 @@ function contractSourceArgs (contractWasmPath) {
]
}

/**
* Retrieves the memory limit based on the configuration preset or custom memory limit.
* @returns {string} The memory limit
*/
async function GetMemoryLimit() {
let memory_limit = "256-mb";

const configPath = `${Deno.cwd()}/config.yml`;
let config = null;
try {
config = parse(await Deno.readTextFile(configPath));
} catch {
return memory_limit;
}

if (config) {
switch (config.preset) {
case "xs":
memory_limit = "64-mb";
break;
case "sm":
memory_limit = "128-mb";
break;
case "md":
memory_limit = "256-mb";
break;
case "lg":
memory_limit = "256-mb";
break;
case "xl":
memory_limit = "512-mb";
break;
case "xxl":
memory_limit = "4096-mb";
break;
default:
memory_limit = "256-mb";
break;
}
}
if (config?.memory_limit) {
memory_limit = `${config.memory_limit}-b`;
}
return memory_limit;
}

/**
* TODO:
* - Validate existence of wallet
* - Validate existence of contract wasm
* - allow using environment variables to set things like path to wallet
* - require confirmation and bypass with --yes
*/
export async function publish ({ wallet, host, tag, value }, contractWasmPath) {
export async function publish({ wallet, host, tag, value }, contractWasmPath) {
tag.push("Memory-Limit");
value.push(await GetMemoryLimit());
const cmdArgs = [
...walletArgs(wallet),
...hostArgs(host),
Expand Down

0 comments on commit 02237ea

Please sign in to comment.