@freestyle-sh/with-dev-server 0.1.3 → 0.1.5
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 +5 -0
- package/dist/index.d.ts +8 -1
- package/dist/index.js +62 -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,13 +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,
|
|
36
|
+
devCommand: "npm run dev",
|
|
37
|
+
devCommandPty: devPty,
|
|
33
38
|
}),
|
|
34
39
|
},
|
|
35
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) => 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;
|
|
@@ -36,11 +39,15 @@ declare class VmDevServer extends VmWith<VmDevServerInstance> {
|
|
|
36
39
|
templateRepo?: string;
|
|
37
40
|
repo?: string;
|
|
38
41
|
workdir: string;
|
|
42
|
+
devCommand?: string;
|
|
43
|
+
devCommandPty?: VmPtySessionLike;
|
|
39
44
|
createInstance(): VmDevServerInstance;
|
|
40
45
|
constructor(options: {
|
|
41
46
|
templateRepo?: string;
|
|
42
47
|
repo?: string;
|
|
43
48
|
workdir?: string;
|
|
49
|
+
devCommand?: string;
|
|
50
|
+
devCommandPty?: VmPtySessionLike;
|
|
44
51
|
});
|
|
45
52
|
configureSnapshotSpec(spec: VmSpec): VmSpec | Promise<VmSpec>;
|
|
46
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) => {
|
|
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) => {
|
|
|
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,9 +48,13 @@ 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 {
|
|
57
|
+
options;
|
|
53
58
|
constructor(options) {
|
|
54
59
|
super();
|
|
55
60
|
this.options = options;
|
|
@@ -78,6 +83,16 @@ class VmDevServerInstance extends VmWithInstance {
|
|
|
78
83
|
return parts.join(" ");
|
|
79
84
|
}
|
|
80
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
|
+
}
|
|
81
96
|
const command = this.buildJournalctlCommand({
|
|
82
97
|
unit: options?.unit,
|
|
83
98
|
lines: options?.lines ?? 200,
|
|
@@ -86,6 +101,38 @@ class VmDevServerInstance extends VmWithInstance {
|
|
|
86
101
|
return await this.vm.exec({ command });
|
|
87
102
|
}
|
|
88
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
|
+
}
|
|
89
136
|
const pollIntervalMs = options?.pollIntervalMs ?? 1e3;
|
|
90
137
|
let cursor = void 0;
|
|
91
138
|
let first = true;
|
|
@@ -136,9 +183,12 @@ class VmDevServer extends VmWith {
|
|
|
136
183
|
templateRepo;
|
|
137
184
|
repo;
|
|
138
185
|
workdir;
|
|
186
|
+
devCommand;
|
|
187
|
+
devCommandPty;
|
|
139
188
|
createInstance() {
|
|
140
189
|
return new VmDevServerInstance({
|
|
141
|
-
workdir: this.workdir
|
|
190
|
+
workdir: this.workdir,
|
|
191
|
+
devCommandPty: this.devCommandPty
|
|
142
192
|
});
|
|
143
193
|
}
|
|
144
194
|
constructor(options) {
|
|
@@ -146,12 +196,19 @@ class VmDevServer extends VmWith {
|
|
|
146
196
|
this.templateRepo = options.templateRepo;
|
|
147
197
|
this.repo = options.repo;
|
|
148
198
|
this.workdir = options.workdir ?? "/repo";
|
|
199
|
+
this.devCommand = options.devCommand;
|
|
200
|
+
this.devCommandPty = options.devCommandPty;
|
|
149
201
|
}
|
|
150
202
|
configureSnapshotSpec(spec) {
|
|
151
203
|
if (this.templateRepo) {
|
|
152
204
|
const composed = this.composeSpecs(
|
|
153
205
|
spec,
|
|
154
|
-
createSnapshotSpec(
|
|
206
|
+
createSnapshotSpec(
|
|
207
|
+
this.templateRepo,
|
|
208
|
+
this.workdir,
|
|
209
|
+
this.devCommand,
|
|
210
|
+
this.devCommandPty
|
|
211
|
+
)
|
|
155
212
|
);
|
|
156
213
|
return composed;
|
|
157
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.5",
|
|
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.7",
|
|
8
|
+
"@freestyle-sh/with-pty": "^0.0.2"
|
|
8
9
|
},
|
|
9
10
|
"type": "module",
|
|
10
11
|
"main": "./dist/index.js",
|