|
| 1 | +import { spawn } from "child_process"; |
| 2 | + |
| 3 | +import { note, spinner } from "@clack/prompts"; |
| 4 | +import picocolors from "picocolors"; |
| 5 | +import { t } from "i18next"; |
| 6 | +import { |
| 7 | + logSimpleErrorMessage, |
| 8 | + logSimpleInfoMessage, |
| 9 | +} from "../functions/terminalHelpers"; |
| 10 | +import removeDockerContainer from "./removeDocker"; |
| 11 | + |
| 12 | +/** Handles Magento 2 Docker Image installation */ |
| 13 | +const installMagentoImage = async ( |
| 14 | + magentoDirName: string, |
| 15 | + magentoDomainName: string, |
| 16 | + writeLog: (message: string) => void |
| 17 | +): Promise<any> => { |
| 18 | + const options = { |
| 19 | + cwd: magentoDirName, |
| 20 | + }; |
| 21 | + |
| 22 | + const sp = spinner(); |
| 23 | + |
| 24 | + return new Promise((resolve) => { |
| 25 | + const curl = spawn( |
| 26 | + "curl", |
| 27 | + [ |
| 28 | + "-s", |
| 29 | + "https://raw.githubusercontent.com/markshust/docker-magento/master/lib/onelinesetup", |
| 30 | + ], |
| 31 | + options |
| 32 | + ); |
| 33 | + const bash = spawn("bash", ["-s", "--", magentoDomainName], options); |
| 34 | + |
| 35 | + let stdout = ""; |
| 36 | + |
| 37 | + note(t("command.generate_store.magento.note_long")); |
| 38 | + |
| 39 | + sp.start( |
| 40 | + picocolors.cyan(t("command.generate_store.progress.docker_start")) |
| 41 | + ); |
| 42 | + |
| 43 | + curl.stdout.pipe(bash.stdin); |
| 44 | + |
| 45 | + bash.stdout.on("data", (data) => { |
| 46 | + if ( |
| 47 | + data.toString().toLowerCase().includes("system") && |
| 48 | + data.toString().toLowerCase().includes("password") |
| 49 | + ) { |
| 50 | + sp.stop( |
| 51 | + picocolors.yellow(t("command.generate_store.magento.password")) |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + if (data.toString().includes("Restarting containers to apply updates")) { |
| 56 | + sp.start( |
| 57 | + picocolors.cyan(t("command.generate_store.progress.docker_start")) |
| 58 | + ); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + bash.stderr.on("data", async (data) => { |
| 63 | + stdout += data.toString(); |
| 64 | + if (stdout.includes("port is already allocated")) { |
| 65 | + sp.stop(); |
| 66 | + logSimpleErrorMessage(t("command.generate_store.magento.port_busy")); |
| 67 | + // delete the directory |
| 68 | + await removeDockerContainer(magentoDirName); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + bash.on("exit", async (code) => { |
| 73 | + if (code === 0) { |
| 74 | + sp.stop( |
| 75 | + picocolors.green(t("command.generate_store.progress.docker_end")) |
| 76 | + ); |
| 77 | + resolve(1); |
| 78 | + } else { |
| 79 | + sp.stop( |
| 80 | + picocolors.red(t("command.generate_store.progress.docker_failed")) |
| 81 | + ); |
| 82 | + |
| 83 | + if ( |
| 84 | + stdout.includes('Project directory "/var/www/html/." is not empty') |
| 85 | + ) { |
| 86 | + note(t("command.generate_store.magento.image_exists")); |
| 87 | + } |
| 88 | + // create a log file |
| 89 | + writeLog(stdout); |
| 90 | + |
| 91 | + logSimpleInfoMessage(t("command.generate_store.magento.failed_log")); |
| 92 | + } |
| 93 | + }); |
| 94 | + }); |
| 95 | +}; |
| 96 | + |
| 97 | +export default installMagentoImage; |
0 commit comments