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