Fix composerize

This commit is contained in:
Louis Lam 2023-11-11 21:29:09 +08:00
parent caa82bbad5
commit 83d58cd363
2 changed files with 35 additions and 9 deletions

View file

@ -3,6 +3,9 @@ import { DockgeServer } from "../dockge-server";
import { callbackError, checkLogin, DockgeSocket, ValidationError } from "../util-server"; import { callbackError, checkLogin, DockgeSocket, ValidationError } from "../util-server";
import { Stack } from "../stack"; import { Stack } from "../stack";
// @ts-ignore
import composerize from "composerize";
export class DockerSocketHandler extends SocketHandler { export class DockerSocketHandler extends SocketHandler {
create(socket : DockgeSocket, server : DockgeServer) { create(socket : DockgeSocket, server : DockgeServer) {
@ -217,6 +220,25 @@ export class DockerSocketHandler extends SocketHandler {
callbackError(e, callback); callbackError(e, callback);
} }
}); });
// composerize
socket.on("composerize", async (dockerRunCommand : unknown, callback) => {
try {
checkLogin(socket);
if (typeof(dockerRunCommand) !== "string") {
throw new ValidationError("dockerRunCommand must be a string");
}
const composeTemplate = composerize(dockerRunCommand);
callback({
ok: true,
composeTemplate,
});
} catch (e) {
callbackError(e, callback);
}
});
} }
saveStack(socket : DockgeSocket, server : DockgeServer, name : unknown, composeYAML : unknown, isAdd : unknown) : Stack { saveStack(socket : DockgeSocket, server : DockgeServer, name : unknown, composeYAML : unknown, isAdd : unknown) : Stack {

View file

@ -34,7 +34,6 @@
</template> </template>
<script> <script>
import * as convertDockerRunToCompose from "composerize";
import { statusNameShort } from "../../../backend/util-common"; import { statusNameShort } from "../../../backend/util-common";
export default { export default {
@ -112,15 +111,20 @@ export default {
}, },
convertDockerRun() { convertDockerRun() {
try { if (this.dockerRunCommand.trim() === "docker run") {
if (this.dockerRunCommand.trim() === "docker run") { throw new Error("Please enter a docker run command");
throw new Error("Please enter a docker run command");
}
this.$root.composeTemplate = convertDockerRunToCompose(this.dockerRunCommand);
this.$router.push("/compose");
} catch (e) {
this.$root.toastError(e.message);
} }
// composerize is working in dev, but after "vite build", it is not working
// So pass to backend to do the conversion
this.$root.getSocket().emit("composerize", this.dockerRunCommand, (res) => {
if (res.ok) {
this.$root.composeTemplate = res.composeTemplate;
this.$router.push("/compose");
} else {
this.$root.toastRes(res);
}
});
}, },
/** /**