Skip to content

Commit f6de79c

Browse files
committed
feat: v0.4.0
1 parent 5975a37 commit f6de79c

File tree

6 files changed

+81
-20
lines changed

6 files changed

+81
-20
lines changed

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ const IMAGE_NAME = "hello-world";
1717
const CONTAINER_NAME = "hello";
1818

1919
const vm = factory();
20-
if (!(await vm.checkVM())) {
21-
await vm.initVM();
22-
}
2320

21+
vm.on(events.VM_INIT_START, () => {
22+
console.log("VM_INIT_START");
23+
});
24+
vm.on(events.VM_INIT_OUTPUT, (data) => {
25+
console.log(data);
26+
});
27+
vm.on(events.VM_INIT_END, () => {
28+
console.log("VM_INIT_END");
29+
});
2430
vm.on(events.IMAGE_PULL_START, () => {});
2531
vm.on(events.IMAGE_PULL_OUTPUT, (data) => {
2632
console.log(data);
@@ -32,6 +38,10 @@ vm.on(events.CONTAINER_RUN_OUTPUT, (data) => {
3238
console.log(data);
3339
});
3440

41+
if (!(await vm.checkVM())) {
42+
await vm.initVM();
43+
}
44+
3545
await vm.pullImage(IMAGE_NAME);
3646

3747
const images = await vm.getImages();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nerdctl",
3-
"version": "0.3.7",
3+
"version": "0.4.0",
44
"main": "dist/index.js",
55
"types": "dist/index.d.ts",
66
"description": "Node wrapper for nerdctl",

src/index.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ export function factory(path: string = process.cwd()): BaseBackend {
2727

2828
// const vm = factory();
2929

30-
// console.log(await vm.checkVM());
31-
32-
// if (!(await vm.checkVM())) {
33-
// await vm.initVM();
34-
// }
35-
30+
// vm.on(events.VM_INIT_START, () => {
31+
// console.log("VM_INIT_START");
32+
// });
33+
// vm.on(events.VM_INIT_OUTPUT, (data) => {
34+
// console.log(data);
35+
// });
36+
// vm.on(events.VM_INIT_END, () => {
37+
// console.log("VM_INIT_END");
38+
// });
3639
// vm.on(events.IMAGE_PULL_START, () => {});
3740
// vm.on(events.IMAGE_PULL_OUTPUT, (data) => {
3841
// console.log(data);
@@ -44,6 +47,10 @@ export function factory(path: string = process.cwd()): BaseBackend {
4447
// console.log(data);
4548
// });
4649

50+
// if (!(await vm.checkVM())) {
51+
// await vm.initVM();
52+
// }
53+
4754
// await vm.pullImage(IMAGE_NAME);
4855

4956
// const images = await vm.getImages();

src/utils/logging.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export class Log extends EventEmitter {
4545
this.fdPromise = new Promise((resolve) => {
4646
this.stream.on("open", resolve);
4747
});
48+
4849
this.console =
4950
process.env.NODE_ENV === "test"
5051
? globalThis.console
@@ -109,8 +110,11 @@ export class Log extends EventEmitter {
109110
message: any,
110111
optionalParameters: any[]
111112
) {
112-
this.emit("log", message, method);
113-
this.console[method](`%s: ${message}`, new Date(), ...optionalParameters);
113+
this.console[method](
114+
`%s: ----> ${message}`,
115+
new Date(),
116+
...optionalParameters
117+
);
114118
}
115119

116120
/**

src/vms/base.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as events from "@/constants/events";
2-
31
import { ImageResult, RemoveImageCommandFlags } from "@/types/images";
42
import {
53
RemoveCommandFlags,
@@ -25,9 +23,6 @@ export default abstract class BaseBackend extends EventEmitter {
2523
super();
2624
this.path = path;
2725
this.log = new Log(APP_NAME, join(this.resourcePath, "logs"));
28-
this.log.on("log", (message: string, type: string) => {
29-
this.emit(events.VM_INIT_OUTPUT, message, type);
30-
});
3126
}
3227

3328
protected get resourcePath() {

src/vms/lima.ts

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,56 @@ export default class LimaBackend extends BaseBackend {
104104
})();
105105
}
106106

107-
protected async lima(...args: string[]): Promise<void> {
107+
protected async lima(
108+
command: string,
109+
...args: string[]
110+
): Promise<ChildResultType> {
108111
try {
109-
await childProcess.spawnFile(this.limactl, args, {
112+
const child = await childProcess.spawn(this.limactl, [command, ...args], {
110113
env: this.limaEnv,
111-
stdio: this.log,
114+
});
115+
116+
const result = { code: 0, stdout: "", stderr: "" };
117+
118+
return await new Promise((resolve, reject) => {
119+
child.stdout?.on("data", (data: Buffer) => {
120+
const dataString = data.toString();
121+
result.stdout += dataString;
122+
this.emit(events.VM_INIT_OUTPUT, dataString);
123+
});
124+
child.stderr?.on("data", (data: Buffer) => {
125+
let dataString = data.toString();
126+
result.stderr += dataString;
127+
this.emit(events.VM_INIT_OUTPUT, dataString);
128+
});
129+
child.on("exit", (code, signal) => {
130+
if (result.stderr) {
131+
}
132+
if (code === 0) {
133+
resolve({ ...result, code });
134+
} else if (signal) {
135+
reject(
136+
Object.create(result, {
137+
code: { value: -1 },
138+
signal: { value: signal },
139+
[childProcess.ErrorCommand]: {
140+
enumerable: false,
141+
value: child.spawnargs,
142+
},
143+
})
144+
);
145+
} else {
146+
reject(
147+
Object.create(result, {
148+
code: { value: code },
149+
[childProcess.ErrorCommand]: {
150+
enumerable: false,
151+
value: child.spawnargs,
152+
},
153+
})
154+
);
155+
}
156+
});
112157
});
113158
} catch (ex) {
114159
console.error(`+ limactl ${args.join(" ")}`);

0 commit comments

Comments
 (0)