@freestyle-sh/with-dev-server 0.1.6 → 0.1.8

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,7 +1,33 @@
1
1
  import { VmPtySessionLike } from '@freestyle-sh/with-pty';
2
- import { VmSpec, VmWithInstance, VmWith } from 'freestyle-sandboxes';
2
+ import { VmWith, VmWithInstance, VmSpec } from 'freestyle-sandboxes';
3
3
 
4
- declare const createSnapshotSpec: (templateRepo: string, workdir: string, devCommand?: string, devCommandPty?: VmPtySessionLike) => VmSpec;
4
+ type DevServerOptions = {
5
+ templateRepo?: string;
6
+ repo?: string;
7
+ workdir?: string;
8
+ port?: number;
9
+ installCommand?: string;
10
+ devCommand?: string;
11
+ runtime?: VmWith<VmWithInstance> & {
12
+ installServiceName(): string;
13
+ };
14
+ devCommandPty?: VmPtySessionLike;
15
+ env?: Record<string, string>;
16
+ };
17
+ type DevServerResolvedOptions = {
18
+ templateRepo?: string;
19
+ repo?: string;
20
+ workdir: string;
21
+ port: number;
22
+ installCommand: string;
23
+ devCommand: string;
24
+ runtime: VmWith<VmWithInstance> & {
25
+ installServiceName(): string;
26
+ };
27
+ devCommandPty?: VmPtySessionLike;
28
+ env?: Record<string, string>;
29
+ };
30
+ declare const createSnapshotSpec: (options: DevServerResolvedOptions) => VmSpec;
5
31
  declare class VmDevServerInstance extends VmWithInstance {
6
32
  options: {
7
33
  workdir: string;
@@ -36,21 +62,12 @@ declare class VmDevServerInstance extends VmWithInstance {
36
62
  }>;
37
63
  }
38
64
  declare class VmDevServer extends VmWith<VmDevServerInstance> {
39
- templateRepo?: string;
40
- repo?: string;
41
- workdir: string;
42
- devCommand?: string;
43
- devCommandPty?: VmPtySessionLike;
65
+ options: DevServerResolvedOptions;
44
66
  createInstance(): VmDevServerInstance;
45
- constructor(options: {
46
- templateRepo?: string;
47
- repo?: string;
48
- workdir?: string;
49
- devCommand?: string;
50
- devCommandPty?: VmPtySessionLike;
51
- });
67
+ constructor(options: DevServerOptions);
52
68
  configureSnapshotSpec(spec: VmSpec): VmSpec | Promise<VmSpec>;
53
69
  configureSpec(spec: VmSpec): VmSpec | Promise<VmSpec>;
54
70
  }
55
71
 
56
72
  export { VmDevServer, VmDevServerInstance, createSnapshotSpec };
73
+ export type { DevServerOptions };
package/dist/index.js CHANGED
@@ -1,55 +1,60 @@
1
1
  import { VmNodeJs } from '@freestyle-sh/with-nodejs';
2
2
  import { VmSpec, VmWithInstance, VmWith } from 'freestyle-sandboxes';
3
3
 
4
- const createSnapshotSpec = (templateRepo, workdir, devCommand, devCommandPty) => {
5
- const resolvedDevCommand = devCommandPty ? devCommandPty.wrapServiceCommand(devCommand ?? "npm run dev", workdir) : devCommand ?? "npm run dev";
4
+ const createSnapshotSpec = (options) => {
5
+ const resolvedDevCommand = options.devCommandPty ? options.devCommandPty.wrapServiceCommand(options.devCommand, options.workdir) : options.devCommand;
6
6
  let newSpec = new VmSpec({
7
7
  with: {
8
- nodejs: new VmNodeJs({})
8
+ runtime: options.runtime
9
9
  },
10
10
  git: {
11
11
  repos: [
12
12
  {
13
- repo: templateRepo,
14
- path: workdir
13
+ repo: options.templateRepo,
14
+ path: options.workdir
15
15
  }
16
16
  ]
17
17
  },
18
18
  systemd: {
19
19
  services: [
20
20
  {
21
- name: "npm-install",
22
- bash: "npm install",
21
+ name: "dev-server-install",
22
+ bash: `set -e
23
+ ${options.installCommand}`,
23
24
  mode: "oneshot",
24
- workdir
25
+ workdir: options.workdir,
26
+ after: [options.runtime.installServiceName()],
27
+ requires: [options.runtime.installServiceName()],
28
+ env: options.env
25
29
  },
26
30
  {
27
- name: "npm-dev",
31
+ name: "dev-server",
28
32
  bash: resolvedDevCommand,
29
- after: ["npm-install"],
30
- requires: ["npm-install"],
31
- workdir
33
+ after: ["dev-server-install"],
34
+ requires: ["dev-server-install"],
35
+ workdir: options.workdir,
36
+ env: options.env
32
37
  },
33
38
  {
34
- name: "curl-test",
39
+ name: "dev-server-health",
35
40
  bash: `
36
41
  set -e
37
- timeout 10 bash -c 'while ! curl http://localhost:3000; do
42
+ timeout 10 bash -c 'while ! curl http://localhost:${options.port}; do
38
43
  echo "Retrying..."
39
44
  sleep 1
40
45
  done'
41
46
  `,
42
47
  mode: "oneshot",
43
- after: ["npm-dev"],
44
- requires: ["npm-dev"],
45
- workdir,
48
+ after: ["dev-server"],
49
+ requires: ["dev-server"],
50
+ workdir: options.workdir,
46
51
  timeoutSec: 10
47
52
  }
48
53
  ]
49
54
  }
50
55
  });
51
- if (devCommandPty) {
52
- newSpec = devCommandPty.applyToSpec(newSpec);
56
+ if (options.devCommandPty) {
57
+ newSpec = options.devCommandPty.applyToSpec(newSpec);
53
58
  }
54
59
  return newSpec;
55
60
  };
@@ -63,7 +68,7 @@ class VmDevServerInstance extends VmWithInstance {
63
68
  return `'${value.replace(/'/g, "'\\''")}'`;
64
69
  }
65
70
  buildJournalctlCommand(options) {
66
- const unit = options.unit ?? "npm-dev";
71
+ const unit = options.unit ?? "dev-server";
67
72
  const parts = ["journalctl", "-u", this.shellEscape(unit), "--no-pager"];
68
73
  if (options.output) {
69
74
  parts.push("-o", this.shellEscape(options.output));
@@ -84,7 +89,7 @@ class VmDevServerInstance extends VmWithInstance {
84
89
  }
85
90
  async getLogs(options) {
86
91
  const pty = this.options.devCommandPty;
87
- const shouldUsePtyLogs = pty && (options?.unit === void 0 || options.unit === "npm-dev");
92
+ const shouldUsePtyLogs = pty && (options?.unit === void 0 || options.unit === "dev-server");
88
93
  if (shouldUsePtyLogs) {
89
94
  return await this.vm.exec({
90
95
  command: pty.captureOutputCommand({
@@ -102,7 +107,7 @@ class VmDevServerInstance extends VmWithInstance {
102
107
  }
103
108
  async *streamLogs(options) {
104
109
  const pty = this.options.devCommandPty;
105
- const shouldUsePtyLogs = pty && (options?.unit === void 0 || options.unit === "npm-dev");
110
+ const shouldUsePtyLogs = pty && (options?.unit === void 0 || options.unit === "dev-server");
106
111
  if (shouldUsePtyLogs) {
107
112
  const pollIntervalMs2 = options?.pollIntervalMs ?? 1e3;
108
113
  let previousOutput = "";
@@ -176,52 +181,46 @@ class VmDevServerInstance extends VmWithInstance {
176
181
  }
177
182
  }
178
183
  async restart() {
179
- return await this.vm.exec("systemctl restart npm-dev");
184
+ return await this.vm.exec("systemctl restart dev-server");
180
185
  }
181
186
  }
182
187
  class VmDevServer extends VmWith {
183
- templateRepo;
184
- repo;
185
- workdir;
186
- devCommand;
187
- devCommandPty;
188
+ options;
188
189
  createInstance() {
189
190
  return new VmDevServerInstance({
190
- workdir: this.workdir,
191
- devCommandPty: this.devCommandPty
191
+ workdir: this.options.workdir,
192
+ devCommandPty: this.options.devCommandPty
192
193
  });
193
194
  }
194
195
  constructor(options) {
195
196
  super();
196
- this.templateRepo = options.templateRepo;
197
- this.repo = options.repo;
198
- this.workdir = options.workdir ?? "/repo";
199
- this.devCommand = options.devCommand;
200
- this.devCommandPty = options.devCommandPty;
197
+ this.options = {
198
+ ...options,
199
+ workdir: options.workdir ?? "/repo",
200
+ port: options.port ?? 3e3,
201
+ installCommand: options.installCommand ?? "npm install",
202
+ devCommand: options.devCommand ?? "npm run dev",
203
+ runtime: options.runtime ?? new VmNodeJs({})
204
+ };
201
205
  }
202
206
  configureSnapshotSpec(spec) {
203
- if (this.templateRepo) {
207
+ if (this.options.templateRepo) {
204
208
  const composed = this.composeSpecs(
205
209
  spec,
206
- createSnapshotSpec(
207
- this.templateRepo,
208
- this.workdir,
209
- this.devCommand,
210
- this.devCommandPty
211
- )
210
+ createSnapshotSpec(this.options)
212
211
  );
213
212
  return composed;
214
213
  }
215
214
  return spec;
216
215
  }
217
216
  configureSpec(spec) {
218
- if (this.repo) {
217
+ if (this.options.repo) {
219
218
  const newSpec = new VmSpec({
220
219
  git: {
221
220
  repos: [
222
221
  {
223
- repo: this.repo,
224
- path: this.workdir
222
+ repo: this.options.repo,
223
+ path: this.options.workdir
225
224
  }
226
225
  ]
227
226
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@freestyle-sh/with-dev-server",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "private": false,
5
5
  "dependencies": {
6
6
  "freestyle-sandboxes": "^0.1.28",