@freestyle-sh/with-dev-server 0.1.4 → 0.1.6
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 +4 -0
- package/dist/index.d.ts +6 -1
- package/dist/index.js +59 -5
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -13,6 +13,7 @@ npm install @freestyle-sh/with-nodejs freestyle-sandboxes
|
|
|
13
13
|
```typescript
|
|
14
14
|
import { freestyle, VmSpec } from "freestyle-sandboxes";
|
|
15
15
|
import { VmDevServer } from "../src/index";
|
|
16
|
+
import { VmPtySession } from "@freestyle-sh/with-pty";
|
|
16
17
|
|
|
17
18
|
const TEMPLATE_REPO = "https://github.com/freestyle-sh/freestyle-next";
|
|
18
19
|
|
|
@@ -23,14 +24,17 @@ const { repoId } = await freestyle.git.repos.create({
|
|
|
23
24
|
});
|
|
24
25
|
|
|
25
26
|
const domain = `${repoId}.style.dev`;
|
|
27
|
+
const devPty = new VmPtySession({ sessionId: "dev-server" });
|
|
26
28
|
|
|
27
29
|
const { vm } = await freestyle.vms.create({
|
|
28
30
|
snapshot: new VmSpec({
|
|
29
31
|
with: {
|
|
32
|
+
devPty,
|
|
30
33
|
devServer: new VmDevServer({
|
|
31
34
|
workdir: "/repo",
|
|
32
35
|
templateRepo: TEMPLATE_REPO,
|
|
33
36
|
devCommand: "npm run dev",
|
|
37
|
+
devCommandPty: devPty,
|
|
34
38
|
}),
|
|
35
39
|
},
|
|
36
40
|
}),
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
+
import { VmPtySessionLike } from '@freestyle-sh/with-pty';
|
|
1
2
|
import { VmSpec, VmWithInstance, VmWith } from 'freestyle-sandboxes';
|
|
2
3
|
|
|
3
|
-
declare const createSnapshotSpec: (templateRepo: string, workdir: string, devCommand?: string) => VmSpec;
|
|
4
|
+
declare const createSnapshotSpec: (templateRepo: string, workdir: string, devCommand?: string, devCommandPty?: VmPtySessionLike) => VmSpec;
|
|
4
5
|
declare class VmDevServerInstance extends VmWithInstance {
|
|
5
6
|
options: {
|
|
6
7
|
workdir: string;
|
|
8
|
+
devCommandPty?: VmPtySessionLike;
|
|
7
9
|
};
|
|
8
10
|
constructor(options: {
|
|
9
11
|
workdir: string;
|
|
12
|
+
devCommandPty?: VmPtySessionLike;
|
|
10
13
|
});
|
|
11
14
|
private shellEscape;
|
|
12
15
|
private buildJournalctlCommand;
|
|
@@ -37,12 +40,14 @@ declare class VmDevServer extends VmWith<VmDevServerInstance> {
|
|
|
37
40
|
repo?: string;
|
|
38
41
|
workdir: string;
|
|
39
42
|
devCommand?: string;
|
|
43
|
+
devCommandPty?: VmPtySessionLike;
|
|
40
44
|
createInstance(): VmDevServerInstance;
|
|
41
45
|
constructor(options: {
|
|
42
46
|
templateRepo?: string;
|
|
43
47
|
repo?: string;
|
|
44
48
|
workdir?: string;
|
|
45
49
|
devCommand?: string;
|
|
50
|
+
devCommandPty?: VmPtySessionLike;
|
|
46
51
|
});
|
|
47
52
|
configureSnapshotSpec(spec: VmSpec): VmSpec | Promise<VmSpec>;
|
|
48
53
|
configureSpec(spec: VmSpec): VmSpec | Promise<VmSpec>;
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
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) => {
|
|
5
|
-
const
|
|
4
|
+
const createSnapshotSpec = (templateRepo, workdir, devCommand, devCommandPty) => {
|
|
5
|
+
const resolvedDevCommand = devCommandPty ? devCommandPty.wrapServiceCommand(devCommand ?? "npm run dev", workdir) : devCommand ?? "npm run dev";
|
|
6
|
+
let newSpec = new VmSpec({
|
|
6
7
|
with: {
|
|
7
8
|
nodejs: new VmNodeJs({})
|
|
8
9
|
},
|
|
@@ -24,7 +25,7 @@ const createSnapshotSpec = (templateRepo, workdir, devCommand) => {
|
|
|
24
25
|
},
|
|
25
26
|
{
|
|
26
27
|
name: "npm-dev",
|
|
27
|
-
bash:
|
|
28
|
+
bash: resolvedDevCommand,
|
|
28
29
|
after: ["npm-install"],
|
|
29
30
|
requires: ["npm-install"],
|
|
30
31
|
workdir
|
|
@@ -47,6 +48,9 @@ done'
|
|
|
47
48
|
]
|
|
48
49
|
}
|
|
49
50
|
});
|
|
51
|
+
if (devCommandPty) {
|
|
52
|
+
newSpec = devCommandPty.applyToSpec(newSpec);
|
|
53
|
+
}
|
|
50
54
|
return newSpec;
|
|
51
55
|
};
|
|
52
56
|
class VmDevServerInstance extends VmWithInstance {
|
|
@@ -79,6 +83,16 @@ class VmDevServerInstance extends VmWithInstance {
|
|
|
79
83
|
return parts.join(" ");
|
|
80
84
|
}
|
|
81
85
|
async getLogs(options) {
|
|
86
|
+
const pty = this.options.devCommandPty;
|
|
87
|
+
const shouldUsePtyLogs = pty && (options?.unit === void 0 || options.unit === "npm-dev");
|
|
88
|
+
if (shouldUsePtyLogs) {
|
|
89
|
+
return await this.vm.exec({
|
|
90
|
+
command: pty.captureOutputCommand({
|
|
91
|
+
lines: options?.lines ?? 200,
|
|
92
|
+
includeEscape: true
|
|
93
|
+
})
|
|
94
|
+
});
|
|
95
|
+
}
|
|
82
96
|
const command = this.buildJournalctlCommand({
|
|
83
97
|
unit: options?.unit,
|
|
84
98
|
lines: options?.lines ?? 200,
|
|
@@ -87,6 +101,38 @@ class VmDevServerInstance extends VmWithInstance {
|
|
|
87
101
|
return await this.vm.exec({ command });
|
|
88
102
|
}
|
|
89
103
|
async *streamLogs(options) {
|
|
104
|
+
const pty = this.options.devCommandPty;
|
|
105
|
+
const shouldUsePtyLogs = pty && (options?.unit === void 0 || options.unit === "npm-dev");
|
|
106
|
+
if (shouldUsePtyLogs) {
|
|
107
|
+
const pollIntervalMs2 = options?.pollIntervalMs ?? 1e3;
|
|
108
|
+
let previousOutput = "";
|
|
109
|
+
const sleep2 = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
110
|
+
while (true) {
|
|
111
|
+
if (options?.signal?.aborted) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const result = await this.vm.exec({
|
|
115
|
+
command: pty.captureOutputCommand({
|
|
116
|
+
lines: options?.lines ?? 200,
|
|
117
|
+
includeEscape: true
|
|
118
|
+
})
|
|
119
|
+
});
|
|
120
|
+
if (result.statusCode && result.statusCode !== 0) {
|
|
121
|
+
const error = result.stderr ?? "Failed to read PTY logs";
|
|
122
|
+
throw new Error(error);
|
|
123
|
+
}
|
|
124
|
+
const output = result.stdout ?? "";
|
|
125
|
+
const delta = output.startsWith(previousOutput) ? output.slice(previousOutput.length) : output;
|
|
126
|
+
for (const line of delta.split(/\r?\n/)) {
|
|
127
|
+
if (!line) {
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
yield line;
|
|
131
|
+
}
|
|
132
|
+
previousOutput = output;
|
|
133
|
+
await sleep2(pollIntervalMs2);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
90
136
|
const pollIntervalMs = options?.pollIntervalMs ?? 1e3;
|
|
91
137
|
let cursor = void 0;
|
|
92
138
|
let first = true;
|
|
@@ -138,9 +184,11 @@ class VmDevServer extends VmWith {
|
|
|
138
184
|
repo;
|
|
139
185
|
workdir;
|
|
140
186
|
devCommand;
|
|
187
|
+
devCommandPty;
|
|
141
188
|
createInstance() {
|
|
142
189
|
return new VmDevServerInstance({
|
|
143
|
-
workdir: this.workdir
|
|
190
|
+
workdir: this.workdir,
|
|
191
|
+
devCommandPty: this.devCommandPty
|
|
144
192
|
});
|
|
145
193
|
}
|
|
146
194
|
constructor(options) {
|
|
@@ -149,12 +197,18 @@ class VmDevServer extends VmWith {
|
|
|
149
197
|
this.repo = options.repo;
|
|
150
198
|
this.workdir = options.workdir ?? "/repo";
|
|
151
199
|
this.devCommand = options.devCommand;
|
|
200
|
+
this.devCommandPty = options.devCommandPty;
|
|
152
201
|
}
|
|
153
202
|
configureSnapshotSpec(spec) {
|
|
154
203
|
if (this.templateRepo) {
|
|
155
204
|
const composed = this.composeSpecs(
|
|
156
205
|
spec,
|
|
157
|
-
createSnapshotSpec(
|
|
206
|
+
createSnapshotSpec(
|
|
207
|
+
this.templateRepo,
|
|
208
|
+
this.workdir,
|
|
209
|
+
this.devCommand,
|
|
210
|
+
this.devCommandPty
|
|
211
|
+
)
|
|
158
212
|
);
|
|
159
213
|
return composed;
|
|
160
214
|
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@freestyle-sh/with-dev-server",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"private": false,
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"freestyle-sandboxes": "^0.1.
|
|
7
|
-
"@freestyle-sh/with-nodejs": "^0.2.
|
|
6
|
+
"freestyle-sandboxes": "^0.1.28",
|
|
7
|
+
"@freestyle-sh/with-nodejs": "^0.2.8",
|
|
8
|
+
"@freestyle-sh/with-pty": "^0.0.3"
|
|
8
9
|
},
|
|
9
10
|
"type": "module",
|
|
10
11
|
"main": "./dist/index.js",
|