Skip to content

Commit 57ee004

Browse files
committed
feat: remove shelljs
1 parent 8abeff1 commit 57ee004

File tree

6 files changed

+4044
-95
lines changed

6 files changed

+4044
-95
lines changed

package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nerdctl",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"main": "dist/index.js",
55
"types": "dist/index.d.ts",
66
"description": "Node wrapper for nerdctl",
@@ -26,7 +26,6 @@
2626
"change-case": "^4.1.2",
2727
"lodash": "^4.17.21",
2828
"node-fetch": "2",
29-
"shelljs": "^0.8.5",
3029
"ts-node-dev": "^1.1.8",
3130
"yaml": "^2.0.0"
3231
},
@@ -36,7 +35,6 @@
3635
"@types/lodash": "^4.14.181",
3736
"@types/node": "^17.0.23",
3837
"@types/node-fetch": "^2.6.1",
39-
"@types/shelljs": "^0.8.11",
4038
"cross-env": "^7.0.3",
4139
"cz-conventional-changelog": "3.3.0",
4240
"dotenv": "^16.0.0",
@@ -50,9 +48,7 @@
5048
"typedoc-plugin-missing-exports": "^0.22.6",
5149
"typescript": "^4.6.3"
5250
},
53-
"peerDependencies": {
54-
"shelljs": "^0.8.5"
55-
},
51+
"peerDependencies": {},
5652
"config": {
5753
"commitizen": {
5854
"path": "./node_modules/cz-conventional-changelog"

src/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,22 @@ export function factory(vm?: string, instance?: string): BaseBackend {
1919
throw new Error(`OS "${platform}" is not supported.`);
2020
}
2121
}
22+
23+
// async function test() {
24+
// const vm = factory();
25+
// await vm.checkVM();
26+
27+
// const IMAGE = "hello-world";
28+
29+
// await vm.pullImage(IMAGE, (data) => {
30+
// console.log(data);
31+
// });
32+
33+
// await vm.run(IMAGE, { rm: true }, (data) => {
34+
// console.log(data);
35+
// });
36+
37+
// console.log(vm.removeImage(IMAGE));
38+
// }
39+
40+
// test();

src/vms/base.ts

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { Architecture, ProcessCallback } from "@/types";
2-
import { ExecOptions, ShellString, exec, which } from "shelljs";
32
import { ImageResult, RemoveImageCommandFlags } from "@/types/images";
43
import {
54
LogsCommandFlags,
65
RemoveCommandFlags,
76
RunCommandFlags,
87
StopCommandFlags,
98
} from "@/types/container";
9+
import { exec, execSync } from "child_process";
1010

11-
import { ChildProcess } from "child_process";
1211
import { GlobalFlags } from "@/types/global";
1312
import { LoginCommandFlags } from "@/types/registry";
1413
import { paramCase } from "change-case";
@@ -44,7 +43,7 @@ export default abstract class BaseBackend {
4443
command: string,
4544
callback?: ProcessCallback
4645
): Promise<boolean> {
47-
const child = (await this.exec(command)) as ChildProcess;
46+
const child = exec(command);
4847

4948
return await new Promise((resolve) => {
5049
child?.stdout?.on("data", (data) => {
@@ -62,22 +61,12 @@ export default abstract class BaseBackend {
6261
});
6362
}
6463

65-
protected async execSync(
66-
command: string,
67-
options?: Omit<ExecOptions, "async">
68-
): Promise<ShellString> {
69-
return exec(command, {
70-
silent: true,
71-
async: false,
72-
...options,
73-
}) as ShellString;
64+
protected execSync(command: string): string {
65+
return execSync(command).toString();
7466
}
7567

76-
protected async exec(
77-
command: string,
78-
options?: Omit<ExecOptions, "async">
79-
): Promise<ChildProcess> {
80-
return exec(command, { silent: true, async: true, ...options });
68+
protected which(command: string) {
69+
return this.execSync(`which ${command}`);
8170
}
8271

8372
protected mergeFlags(flags?: GlobalFlags): string {
@@ -111,7 +100,7 @@ export default abstract class BaseBackend {
111100

112101
//#region VMs
113102
async checkVM(): Promise<boolean> {
114-
return !!which(this.vm);
103+
return !!this.which(this.vm);
115104
}
116105
async checkInstance(): Promise<boolean> {
117106
return true;
@@ -125,21 +114,18 @@ export default abstract class BaseBackend {
125114
//#endregion
126115

127116
//#region registry
128-
async login(
129-
flags?: LoginCommandFlags,
130-
server?: string
131-
): Promise<ShellString> {
117+
login(flags?: LoginCommandFlags, server?: string): string {
132118
const command = `${this.container} login ${this.mergeFlags(
133119
flags
134120
)} ${server}`;
135121

136-
return await this.execSync(command);
122+
return this.execSync(command);
137123
}
138124

139-
async logout(server?: string): Promise<ShellString> {
125+
logout(server?: string): string {
140126
const command = `${this.container} logout ${server}`;
141127

142-
return await this.execSync(command);
128+
return this.execSync(command);
143129
}
144130
//#endregion
145131

@@ -154,32 +140,26 @@ export default abstract class BaseBackend {
154140
return await this.fork(command, callback);
155141
}
156142

157-
async stop(
158-
container: string | string[],
159-
flags?: StopCommandFlags
160-
): Promise<ShellString> {
143+
stop(container: string | string[], flags?: StopCommandFlags): string {
161144
const containers = Array.isArray(container)
162145
? container.join(" ")
163146
: container;
164147
const command = `${this.container} stop ${this.mergeFlags(
165148
flags
166149
)} ${containers}`;
167150

168-
return await this.execSync(command);
151+
return this.execSync(command);
169152
}
170153

171-
async remove(
172-
container: string | string[],
173-
flags?: RemoveCommandFlags
174-
): Promise<ShellString> {
154+
remove(container: string | string[], flags?: RemoveCommandFlags): string {
175155
const containers = Array.isArray(container)
176156
? container.join(" ")
177157
: container;
178158
const command = `${this.container} rm ${this.mergeFlags(
179159
flags
180160
)} ${containers}`;
181161

182-
return await this.execSync(command);
162+
return this.execSync(command);
183163
}
184164

185165
async logs(container: string, flags?: LogsCommandFlags): Promise<boolean> {
@@ -199,9 +179,7 @@ export default abstract class BaseBackend {
199179
}
200180

201181
async getImages(): Promise<ImageResult[]> {
202-
const child = (await this.exec(
203-
`${this.container} images --format "{{json .}}"`
204-
)) as ChildProcess;
182+
const child = exec(`${this.container} images --format "{{json .}}"`);
205183

206184
return new Promise((resolve, reject) => {
207185
if (!!child.exitCode) reject(null);
@@ -219,14 +197,14 @@ export default abstract class BaseBackend {
219197
});
220198
}
221199

222-
async removeImage(
200+
removeImage(
223201
image: string | string[],
224202
flags?: RemoveImageCommandFlags
225-
): Promise<ShellString> {
203+
): string {
226204
const images = Array.isArray(image) ? image.join(" ") : image;
227205
const command = `${this.container} rmi ${this.mergeFlags(flags)} ${images}`;
228206

229-
return await this.execSync(command);
207+
return this.execSync(command);
230208
}
231209
//#endregion
232210
}

src/vms/lima.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import BaseBackend from "./base";
2-
import { ChildProcess } from "child_process";
32
import { LimaListResult } from "@/types/lima";
43
import { ProcessCallback } from "@/types";
4+
import { exec } from "child_process";
55
import { isM1 } from "@/utils";
66

77
export default class LimaBackend extends BaseBackend {
88
async checkInstance(): Promise<boolean> {
9-
const listChild = (await this.exec(
10-
`${this.vm} list --json`
11-
)) as ChildProcess;
9+
const listChild = exec(`${this.vm} list --json`);
1210
if (!listChild || !listChild.stdout) return false;
1311

1412
const list: LimaListResult[] = await new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)