@freestyle-sh/with-deno 0.0.1 → 0.0.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,23 +1,66 @@
1
- import { VmWith, VmWithInstance, VmSpec } from 'freestyle-sandboxes';
1
+ import { VmWith, VmWithInstance, VmSpec, VmBaseImage } from 'freestyle-sandboxes';
2
2
  import { VmJavaScriptRuntimeInstance, JSONValue, RunCodeResponse, InstallOptions, InstallResult, VmJavaScriptRuntime } from '@freestyle-sh/with-type-js';
3
3
 
4
4
  type DenoOptions = {
5
5
  version?: string;
6
6
  workdir?: string;
7
- deleteAfterSuccess?: boolean;
8
7
  };
9
8
  type DenoResolvedOptions = {
10
9
  version?: string;
11
10
  workdir?: string;
12
- deleteAfterSuccess: boolean;
13
11
  };
14
12
  declare class VmDeno extends VmWith<VmDenoInstance> implements VmJavaScriptRuntime<VmJavaScriptRuntimeInstance> {
15
13
  options: DenoResolvedOptions;
14
+ workspaces: DenoWorkspace[];
16
15
  constructor(options?: DenoOptions);
17
- configureSnapshotSpec(spec: VmSpec): VmSpec;
16
+ configureBaseImage(image: VmBaseImage): VmBaseImage | Promise<VmBaseImage>;
17
+ configureSpec(spec: VmSpec): VmSpec;
18
+ workspace(options: {
19
+ path: string;
20
+ install?: boolean;
21
+ }): DenoWorkspace;
18
22
  createInstance(): VmDenoInstance;
19
23
  installServiceName(): string;
20
24
  }
25
+ declare class DenoWorkspace extends VmWith<DenoWorkspaceInstance> {
26
+ options: {
27
+ path: string;
28
+ install?: boolean;
29
+ };
30
+ env?: Record<string, string>;
31
+ constructor(options: {
32
+ path: string;
33
+ install?: boolean;
34
+ }, env?: Record<string, string>);
35
+ task(name: string, options?: {
36
+ env?: Record<string, string>;
37
+ serviceName?: string;
38
+ }): DenoWorkspaceTask;
39
+ getInstallServiceName(): string;
40
+ configureSpec(spec: VmSpec): VmSpec;
41
+ createInstance(): DenoWorkspaceInstance;
42
+ }
43
+ declare class DenoWorkspaceInstance extends VmWithInstance {
44
+ }
45
+ declare class DenoWorkspaceTask extends VmWith<DenoWorkspaceTaskInstance> {
46
+ name: string;
47
+ workspace: DenoWorkspace;
48
+ env?: Record<string, string>;
49
+ serviceName?: string;
50
+ constructor(name: string, workspace: DenoWorkspace, env?: Record<string, string>, serviceName?: string);
51
+ getServiceName(): string;
52
+ configureSpec(spec: VmSpec): VmSpec;
53
+ createInstance(): DenoWorkspaceTaskInstance;
54
+ }
55
+ declare class DenoWorkspaceTaskInstance extends VmWithInstance {
56
+ name: string;
57
+ workspace: DenoWorkspace;
58
+ env?: Record<string, string>;
59
+ serviceName?: string;
60
+ constructor(name: string, workspace: DenoWorkspace, env?: Record<string, string>, serviceName?: string);
61
+ getServiceName(): string;
62
+ logs(): Promise<string[] | undefined>;
63
+ }
21
64
  declare class VmDenoInstance extends VmWithInstance implements VmJavaScriptRuntimeInstance {
22
65
  builder: VmDeno;
23
66
  constructor(builder: VmDeno);
@@ -30,4 +73,4 @@ declare class VmDenoInstance extends VmWithInstance implements VmJavaScriptRunti
30
73
  install(options?: InstallOptions): Promise<InstallResult>;
31
74
  }
32
75
 
33
- export { VmDeno };
76
+ export { DenoWorkspace, DenoWorkspaceInstance, DenoWorkspaceTask, DenoWorkspaceTaskInstance, VmDeno };
package/dist/index.js CHANGED
@@ -1,53 +1,40 @@
1
- import { VmWith, VmSpec, VmWithInstance } from 'freestyle-sandboxes';
1
+ import { VmWith, VmWithInstance } from 'freestyle-sandboxes';
2
2
 
3
3
  class VmDeno extends VmWith {
4
4
  options;
5
+ workspaces = [];
5
6
  constructor(options) {
6
7
  super();
7
8
  this.options = {
8
9
  version: options?.version,
9
- workdir: options?.workdir,
10
- deleteAfterSuccess: options?.deleteAfterSuccess ?? true
10
+ workdir: options?.workdir
11
11
  };
12
12
  }
13
- configureSnapshotSpec(spec) {
13
+ configureBaseImage(image) {
14
14
  const versionArg = this.options.version ? ` v${this.options.version}` : "";
15
- const installScript = `#!/bin/bash
16
- set -e
17
- export DENO_INSTALL="/opt/deno"
18
- curl -fsSL https://deno.land/install.sh | sh -s -- --yes${versionArg}
19
- $DENO_INSTALL/bin/deno --version
20
- `;
21
- const denoInit = `export DENO_INSTALL="/opt/deno"
22
- export PATH="$DENO_INSTALL/bin:$PATH"
23
- `;
24
- return this.composeSpecs(
25
- spec,
26
- new VmSpec({
27
- additionalFiles: {
28
- "/opt/install-deno.sh": {
29
- content: installScript
30
- },
31
- "/etc/profile.d/deno.sh": {
32
- content: denoInit
33
- }
34
- },
35
- systemd: {
36
- services: [
37
- {
38
- name: "install-deno",
39
- mode: "oneshot",
40
- deleteAfterSuccess: this.options.deleteAfterSuccess,
41
- env: {
42
- HOME: "/root"
43
- },
44
- exec: ["bash /opt/install-deno.sh"],
45
- timeoutSec: 300
46
- }
47
- ]
48
- }
49
- })
50
- );
15
+ return image.runCommands(`
16
+ apt-get update
17
+ apt-get install -y --no-install-recommends ca-certificates curl unzip
18
+ rm -rf /var/lib/apt/lists/*
19
+ curl -fsSL https://deno.land/install.sh | DENO_INSTALL="/opt/deno" sh -s -- --yes${versionArg}
20
+ /opt/deno/bin/deno --version`);
21
+ }
22
+ configureSpec(spec) {
23
+ spec.systemdService({
24
+ name: "install-deno",
25
+ mode: "oneshot",
26
+ env: {
27
+ HOME: "/root"
28
+ },
29
+ exec: ["/opt/deno/bin/deno --version"],
30
+ timeoutSec: 30
31
+ });
32
+ return spec;
33
+ }
34
+ workspace(options) {
35
+ const workspace = new DenoWorkspace(options);
36
+ this.workspaces.push(workspace);
37
+ return workspace;
51
38
  }
52
39
  createInstance() {
53
40
  return new VmDenoInstance(this);
@@ -56,6 +43,112 @@ export PATH="$DENO_INSTALL/bin:$PATH"
56
43
  return "install-deno.service";
57
44
  }
58
45
  }
46
+ class DenoWorkspace extends VmWith {
47
+ options;
48
+ env;
49
+ constructor(options, env) {
50
+ super();
51
+ this.options = options;
52
+ this.env = env;
53
+ }
54
+ task(name, options) {
55
+ return new DenoWorkspaceTask(
56
+ name,
57
+ this,
58
+ {
59
+ ...this.env,
60
+ ...options?.env
61
+ },
62
+ options?.serviceName
63
+ );
64
+ }
65
+ getInstallServiceName() {
66
+ return `deno-install-${this.options.path.replace(/\//g, "-")}`;
67
+ }
68
+ configureSpec(spec) {
69
+ if (this.options.install) {
70
+ spec.systemdService({
71
+ name: this.getInstallServiceName(),
72
+ mode: "oneshot",
73
+ bash: "/opt/deno/bin/deno install",
74
+ workdir: this.options.path,
75
+ env: {
76
+ HOME: "/root",
77
+ DENO_DIR: "/root/.cache/deno",
78
+ ...this.env
79
+ },
80
+ user: "root"
81
+ });
82
+ }
83
+ return spec;
84
+ }
85
+ createInstance() {
86
+ return new DenoWorkspaceInstance();
87
+ }
88
+ }
89
+ class DenoWorkspaceInstance extends VmWithInstance {
90
+ }
91
+ class DenoWorkspaceTask extends VmWith {
92
+ name;
93
+ workspace;
94
+ env;
95
+ serviceName;
96
+ constructor(name, workspace, env, serviceName) {
97
+ super();
98
+ this.name = name;
99
+ this.workspace = workspace;
100
+ this.env = env;
101
+ this.serviceName = serviceName;
102
+ }
103
+ getServiceName() {
104
+ return this.serviceName ?? `deno-workspace-${this.workspace.options.path.replace(/\//g, "-")}-task-${this.name}`;
105
+ }
106
+ configureSpec(spec) {
107
+ spec.systemdService({
108
+ name: this.getServiceName(),
109
+ bash: `/opt/deno/bin/deno task ${this.name}`,
110
+ workdir: this.workspace.options.path,
111
+ after: [this.workspace.getInstallServiceName()],
112
+ requires: [this.workspace.getInstallServiceName()],
113
+ env: {
114
+ HOME: "/root",
115
+ DENO_DIR: "/root/.cache/deno",
116
+ ...this.env
117
+ },
118
+ user: "root"
119
+ });
120
+ return spec;
121
+ }
122
+ createInstance() {
123
+ return new DenoWorkspaceTaskInstance(
124
+ this.name,
125
+ this.workspace,
126
+ this.env,
127
+ this.serviceName
128
+ );
129
+ }
130
+ }
131
+ class DenoWorkspaceTaskInstance extends VmWithInstance {
132
+ name;
133
+ workspace;
134
+ env;
135
+ serviceName;
136
+ constructor(name, workspace, env, serviceName) {
137
+ super();
138
+ this.name = name;
139
+ this.workspace = workspace;
140
+ this.env = env;
141
+ this.serviceName = serviceName;
142
+ }
143
+ getServiceName() {
144
+ return this.serviceName ?? `deno-workspace-${this.workspace.options.path.replace(/\//g, "-")}-task-${this.name}`;
145
+ }
146
+ logs() {
147
+ return this.vm.exec({
148
+ command: `journalctl -u ${this.getServiceName()} --no-pager -n 30`
149
+ }).then((result) => result.stdout?.trim().split("\n"));
150
+ }
151
+ }
59
152
  class VmDenoInstance extends VmWithInstance {
60
153
  builder;
61
154
  constructor(builder) {
@@ -107,7 +200,9 @@ class VmDenoInstance extends VmWithInstance {
107
200
  if (!options?.deps) {
108
201
  command = `${cdPrefix}/opt/deno/bin/deno install`;
109
202
  } else {
110
- const deps = Array.isArray(options.deps) ? options.deps.map(prefixDep) : Object.entries(options.deps).map(([pkg, ver]) => `${prefixDep(pkg)}@${ver}`);
203
+ const deps = Array.isArray(options.deps) ? options.deps.map(prefixDep) : Object.entries(options.deps).map(
204
+ ([pkg, ver]) => `${prefixDep(pkg)}@${ver}`
205
+ );
111
206
  const devFlag = options.dev ? " --dev" : "";
112
207
  command = `${cdPrefix}/opt/deno/bin/deno add${devFlag} ${deps.join(" ")}`;
113
208
  }
@@ -121,4 +216,4 @@ class VmDenoInstance extends VmWithInstance {
121
216
  }
122
217
  }
123
218
 
124
- export { VmDeno };
219
+ export { DenoWorkspace, DenoWorkspaceInstance, DenoWorkspaceTask, DenoWorkspaceTaskInstance, VmDeno };
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@freestyle-sh/with-deno",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "private": false,
5
5
  "dependencies": {
6
- "freestyle-sandboxes": "^0.1.14",
6
+ "freestyle-sandboxes": "^0.1.41",
7
7
  "@freestyle-sh/with-type-js": "0.2.8"
8
8
  },
9
9
  "devDependencies": {