@freestyle-sh/with-ruby 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -30,6 +30,9 @@ declare class RubyRuntimeInstance extends VmWithInstance implements VmRunCodeIns
30
30
  constructor(builder: VmRuby);
31
31
  runCode<Result extends JSONValue = any>(args: string | {
32
32
  code: string;
33
+ argv?: string[];
34
+ env?: Record<string, string>;
35
+ workdir?: string;
33
36
  }): Promise<RunCodeResponse<Result>>;
34
37
  install(options?: InstallOptions): Promise<InstallResult>;
35
38
  }
package/dist/index.js CHANGED
@@ -54,15 +54,25 @@ class RubyRuntimeInstance extends VmWithInstance {
54
54
  this.builder = builder;
55
55
  }
56
56
  async runCode(args) {
57
- const code = typeof args === "string" ? args : args.code;
57
+ const options = typeof args === "string" ? { code: args } : args;
58
+ const { code, argv, env, workdir } = options;
59
+ const shellEscape = (value) => `'${value.replace(/'/g, "'\\''")}'`;
60
+ const argvArgs = argv?.map(shellEscape).join(" ");
61
+ const envPrefix = env ? `${Object.entries(env).map(([key, value]) => `${key}=${shellEscape(value)}`).join(" ")} ` : "";
62
+ const cdPrefix = workdir ? `cd ${shellEscape(workdir)} && ` : "";
63
+ const command = `${cdPrefix}${envPrefix}/usr/local/rvm/rubies/ruby-${this.builder.options.version}/bin/ruby -e "${code.replace(/"/g, '\\"')}"${argvArgs ? ` -- ${argvArgs}` : ""}`;
58
64
  const result = await this.vm.exec({
59
- command: `/usr/local/rvm/rubies/ruby-${this.builder.options.version}/bin/ruby -e "${code.replace(/"/g, '\\"')}"`
65
+ command
60
66
  });
61
67
  let parsedResult = void 0;
62
68
  if (result.stdout) {
63
- try {
64
- parsedResult = JSON.parse(result.stdout);
65
- } catch (e) {
69
+ const lines = result.stdout.split(/\r?\n/).map((line) => line.trim()).filter((line) => line.length > 0);
70
+ const lastLine = lines[lines.length - 1];
71
+ if (lastLine) {
72
+ try {
73
+ parsedResult = JSON.parse(lastLine);
74
+ } catch (e) {
75
+ }
66
76
  }
67
77
  }
68
78
  return {
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@freestyle-sh/with-ruby",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "private": false,
5
5
  "dependencies": {
6
- "freestyle-sandboxes": "^0.1.8",
7
- "@freestyle-sh/with-type-run-code": "^0.2.1"
6
+ "freestyle-sandboxes": "^0.1.13",
7
+ "@freestyle-sh/with-type-run-code": "^0.2.3"
8
8
  },
9
9
  "type": "module",
10
10
  "main": "./dist/index.js",