@freestyle-sh/with-ruby 0.2.0 → 0.2.1

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/README.md CHANGED
@@ -57,6 +57,33 @@ type RunCodeResponse<Result> = {
57
57
  };
58
58
  ```
59
59
 
60
+ ### `vm.ruby.install(options?)`
61
+
62
+ Installs gems via `gem install` or `bundle install`.
63
+
64
+ **Returns:** `Promise<InstallResult>`
65
+
66
+ ```typescript
67
+ // Install specific gems
68
+ await vm.ruby.install({ deps: ["nokogiri", "colorize"] });
69
+
70
+ // Install from Gemfile (bundle install)
71
+ await vm.ruby.install();
72
+ await vm.ruby.install({ directory: "/app" });
73
+ ```
74
+
75
+ ```typescript
76
+ type InstallOptions =
77
+ | { deps: string[] }
78
+ | { directory?: string; deps?: undefined };
79
+
80
+ type InstallResult = {
81
+ success: boolean;
82
+ stdout?: string;
83
+ stderr?: string;
84
+ };
85
+ ```
86
+
60
87
  ## Documentation
61
88
 
62
89
  - [Freestyle Documentation](https://docs.freestyle.sh)
package/dist/index.d.ts CHANGED
@@ -1,6 +1,17 @@
1
- import { VmWith, VmWithInstance, CreateVmOptions } from 'freestyle-sandboxes';
1
+ import { VmWith, VmWithInstance, VmSpec } from 'freestyle-sandboxes';
2
2
  import { VmRunCodeInstance, JSONValue, RunCodeResponse, VmRunCode } from '@freestyle-sh/with-type-run-code';
3
3
 
4
+ type InstallResult = {
5
+ success: boolean;
6
+ stdout?: string;
7
+ stderr?: string;
8
+ };
9
+ type InstallOptions = {
10
+ deps: string[];
11
+ } | {
12
+ directory?: string;
13
+ deps?: undefined;
14
+ };
4
15
  type RubyOptions = {
5
16
  version?: string;
6
17
  };
@@ -10,16 +21,18 @@ type RubyResolvedOptions = {
10
21
  declare class VmRuby extends VmWith<RubyRuntimeInstance> implements VmRunCode<VmRunCodeInstance> {
11
22
  options: RubyResolvedOptions;
12
23
  constructor(options?: RubyOptions);
13
- configure(existingConfig: CreateVmOptions): CreateVmOptions | Promise<CreateVmOptions>;
24
+ configureSnapshotSpec(spec: VmSpec): VmSpec;
14
25
  createInstance(): RubyRuntimeInstance;
15
26
  installServiceName(): string;
16
27
  }
17
28
  declare class RubyRuntimeInstance extends VmWithInstance implements VmRunCodeInstance {
18
29
  builder: VmRuby;
19
30
  constructor(builder: VmRuby);
20
- runCode<Result extends JSONValue = any>({ code, }: {
31
+ runCode<Result extends JSONValue = any>(args: string | {
21
32
  code: string;
22
33
  }): Promise<RunCodeResponse<Result>>;
34
+ install(options?: InstallOptions): Promise<InstallResult>;
23
35
  }
24
36
 
25
37
  export { VmRuby };
38
+ export type { InstallOptions, InstallResult };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { VmWith, VmTemplate, VmWithInstance } from 'freestyle-sandboxes';
1
+ import { VmWith, VmSpec, VmWithInstance } from 'freestyle-sandboxes';
2
2
 
3
3
  class VmRuby extends VmWith {
4
4
  options;
@@ -8,7 +8,7 @@ class VmRuby extends VmWith {
8
8
  version: options?.version ?? "3.4.8"
9
9
  };
10
10
  }
11
- configure(existingConfig) {
11
+ configureSnapshotSpec(spec) {
12
12
  const installScript = `#!/bin/bash
13
13
  set -e
14
14
  curl -sSL https://get.rvm.io | bash -s -- head
@@ -17,8 +17,9 @@ rvm install ${this.options.version}
17
17
  rvm use ${this.options.version} --default
18
18
  ruby --version
19
19
  `;
20
- const rubyConfig = {
21
- template: new VmTemplate({
20
+ return this.composeSpecs(
21
+ spec,
22
+ new VmSpec({
22
23
  aptDeps: ["curl", "gnupg2", "ca-certificates"],
23
24
  additionalFiles: {
24
25
  "/opt/install-ruby.sh": {
@@ -37,8 +38,7 @@ ruby --version
37
38
  ]
38
39
  }
39
40
  })
40
- };
41
- return this.compose(existingConfig, rubyConfig);
41
+ );
42
42
  }
43
43
  createInstance() {
44
44
  return new RubyRuntimeInstance(this);
@@ -53,9 +53,8 @@ class RubyRuntimeInstance extends VmWithInstance {
53
53
  super();
54
54
  this.builder = builder;
55
55
  }
56
- async runCode({
57
- code
58
- }) {
56
+ async runCode(args) {
57
+ const code = typeof args === "string" ? args : args.code;
59
58
  const result = await this.vm.exec({
60
59
  command: `/usr/local/rvm/rubies/ruby-${this.builder.options.version}/bin/ruby -e "${code.replace(/"/g, '\\"')}"`
61
60
  });
@@ -73,6 +72,22 @@ class RubyRuntimeInstance extends VmWithInstance {
73
72
  statusCode: result.statusCode ?? -1
74
73
  };
75
74
  }
75
+ async install(options) {
76
+ const gemPath = `/usr/local/rvm/rubies/ruby-${this.builder.options.version}/bin/gem`;
77
+ let command;
78
+ if (!options?.deps) {
79
+ const cdPrefix = options?.directory ? `cd ${options.directory} && ` : "";
80
+ command = `${cdPrefix}bundle install`;
81
+ } else {
82
+ command = `${gemPath} install ${options.deps.join(" ")}`;
83
+ }
84
+ const result = await this.vm.exec({ command });
85
+ return {
86
+ success: result.statusCode === 0,
87
+ stdout: result.stdout ?? void 0,
88
+ stderr: result.stderr ?? void 0
89
+ };
90
+ }
76
91
  }
77
92
 
78
93
  export { VmRuby };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@freestyle-sh/with-ruby",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "private": false,
5
5
  "dependencies": {
6
- "freestyle-sandboxes": "^0.1.2",
7
- "@freestyle-sh/with-type-run-code": "^0.2.0"
6
+ "freestyle-sandboxes": "^0.1.8",
7
+ "@freestyle-sh/with-type-run-code": "^0.2.1"
8
8
  },
9
9
  "type": "module",
10
10
  "main": "./dist/index.js",