@freestyle-sh/with-uv 0.2.0 → 0.2.2

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
@@ -1,4 +1,4 @@
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
4
  type UvOptions = {
@@ -12,15 +12,18 @@ type UvResolvedOptions = {
12
12
  declare class VmUv extends VmWith<VmUvInstance> implements VmRunCode<VmRunCodeInstance> {
13
13
  options: UvResolvedOptions;
14
14
  constructor(options?: UvOptions);
15
- configure(existingConfig: CreateVmOptions): CreateVmOptions | Promise<CreateVmOptions>;
15
+ configureSnapshotSpec(spec: VmSpec): VmSpec;
16
16
  createInstance(): VmUvInstance;
17
17
  installServiceName(): string;
18
18
  }
19
19
  declare class VmUvInstance extends VmWithInstance implements VmRunCodeInstance {
20
20
  builder: VmUv;
21
21
  constructor(builder: VmUv);
22
- runCode<Result extends JSONValue = any>({ code, }: {
22
+ runCode<Result extends JSONValue = any>(args: string | {
23
23
  code: string;
24
+ argv?: string[];
25
+ env?: Record<string, string>;
26
+ workdir?: string;
24
27
  }): Promise<RunCodeResponse<Result>>;
25
28
  }
26
29
 
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 VmUv extends VmWith {
4
4
  options;
@@ -9,7 +9,7 @@ class VmUv extends VmWith {
9
9
  pythonVersion: options?.pythonVersion ?? "3.14"
10
10
  };
11
11
  }
12
- configure(existingConfig) {
12
+ configureSnapshotSpec(spec) {
13
13
  const versionArg = this.options.version ? `UV_VERSION="${this.options.version}" ` : "";
14
14
  const installScript = `#!/bin/bash
15
15
  set -e
@@ -20,8 +20,9 @@ ${versionArg}curl -LsSf https://astral.sh/uv/install.sh | sh
20
20
  `;
21
21
  const uvInit = `export PATH="/opt/uv/bin:$PATH"
22
22
  `;
23
- const uvConfig = {
24
- template: new VmTemplate({
23
+ return this.composeSpecs(
24
+ spec,
25
+ new VmSpec({
25
26
  additionalFiles: {
26
27
  "/opt/install-uv.sh": {
27
28
  content: installScript
@@ -42,8 +43,7 @@ ${versionArg}curl -LsSf https://astral.sh/uv/install.sh | sh
42
43
  ]
43
44
  }
44
45
  })
45
- };
46
- return this.compose(existingConfig, uvConfig);
46
+ );
47
47
  }
48
48
  createInstance() {
49
49
  return new VmUvInstance(this);
@@ -58,17 +58,26 @@ class VmUvInstance extends VmWithInstance {
58
58
  super();
59
59
  this.builder = builder;
60
60
  }
61
- async runCode({
62
- code
63
- }) {
61
+ async runCode(args) {
62
+ const options = typeof args === "string" ? { code: args } : args;
63
+ const { code, argv, env, workdir } = options;
64
+ const shellEscape = (value) => `'${value.replace(/'/g, "'\\''")}'`;
65
+ const argvArgs = argv?.map(shellEscape).join(" ");
66
+ const envPrefix = env ? `${Object.entries(env).map(([key, value]) => `${key}=${shellEscape(value)}`).join(" ")} ` : "";
67
+ const cdPrefix = workdir ? `cd ${shellEscape(workdir)} && ` : "";
68
+ const command = `${cdPrefix}${envPrefix}/opt/uv/bin/uv run python -c "${code.replace(/"/g, '\\"')}"${argvArgs ? ` -- ${argvArgs}` : ""}`;
64
69
  const result = await this.vm.exec({
65
- command: `/opt/uv/bin/uv run python -c "${code.replace(/"/g, '\\"')}"`
70
+ command
66
71
  });
67
72
  let parsedResult = void 0;
68
73
  if (result.stdout) {
69
- try {
70
- parsedResult = JSON.parse(result.stdout);
71
- } catch (e) {
74
+ const lines = result.stdout.split(/\r?\n/).map((line) => line.trim()).filter((line) => line.length > 0);
75
+ const lastLine = lines[lines.length - 1];
76
+ if (lastLine) {
77
+ try {
78
+ parsedResult = JSON.parse(lastLine);
79
+ } catch (e) {
80
+ }
72
81
  }
73
82
  }
74
83
  return {
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@freestyle-sh/with-uv",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
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.2"
8
8
  },
9
9
  "type": "module",
10
10
  "main": "./dist/index.js",