From 56e2024b59965e4a3960532ae46519a6138e31e3 Mon Sep 17 00:00:00 2001 From: Denis Davidyuk Date: Fri, 20 Dec 2024 21:44:24 +0100 Subject: [PATCH] refactor: promisify instead promisify-child-process --- package.json | 1 - src/env/env.js | 43 ++++++++++++++++++------------------------- src/init/init.js | 3 +-- src/test/test.js | 8 +++----- src/utils/utils.js | 4 ++++ tests/cli.test.mjs | 19 ++++--------------- tests/util.mjs | 3 +-- 7 files changed, 31 insertions(+), 50 deletions(-) diff --git a/package.json b/package.json index b520e966..ff07dc3d 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,6 @@ "dependencies": { "@aeternity/aepp-sdk": "^14.0.0", "commander": "^12.1.0", - "promisify-child-process": "^4.1.2", "prompts": "^2.4.2" }, "devDependencies": { diff --git a/src/env/env.js b/src/env/env.js index 50efb7b4..4e3400f2 100644 --- a/src/env/env.js +++ b/src/env/env.js @@ -1,20 +1,13 @@ -import { spawn, exec } from "promisify-child-process"; -import { print, printError } from "../utils/utils.js"; - -let dockerComposeCmd = "docker compose"; +import { print, printError, exec } from "../utils/utils.js"; async function getDockerCompose() { - const dockerSpaceCompose = await spawn("docker", ["compose"]).catch(() => ({ - code: 1, - })); - if (dockerSpaceCompose.code === 0) return; - const dockerMinusCompose = await spawn("docker-compose").catch(() => ({ - code: 1, - })); - - if (dockerMinusCompose.code === 0) { - dockerComposeCmd = "docker-compose"; - return; + if (getDockerCompose._cmd) return getDockerCompose._cmd; + + for (const cmd of ["docker compose", "docker-compose"]) { + if (await exec(cmd).catch(() => false)) { + getDockerCompose._cmd = cmd; + return cmd; + } } throw new Error("===== docker compose is not installed! ====="); @@ -66,8 +59,8 @@ async function stopEnv(running) { print("===== stopping env ====="); - await getDockerCompose(); - await exec(`${dockerComposeCmd} down -v`); + const cmd = await getDockerCompose(); + await exec(`${cmd} down -v`); print("===== Env was successfully stopped! ====="); } @@ -80,8 +73,8 @@ async function restartEnv(running) { print("===== restarting env ====="); - await getDockerCompose(); - await exec(`${dockerComposeCmd} restart`); + const cmd = await getDockerCompose(); + await exec(`${cmd} restart`); print("===== env was successfully restarted! ====="); } @@ -98,9 +91,9 @@ async function startEnv(option) { print(`using versions as specified: ${versionTags}`); else print("using versions from docker-compose.yml"); - await getDockerCompose(); - await exec(`${versionTags} ${dockerComposeCmd} pull`); - await exec(`${versionTags} ${dockerComposeCmd} up -d --wait`); + const cmd = await getDockerCompose(); + await exec(`${versionTags} ${cmd} pull`); + await exec(`${versionTags} ${cmd} up -d --wait`); const isRunning = await isEnvRunning(); await printInfo(isRunning, true); @@ -119,9 +112,9 @@ async function printInfo(running, imagesOnly = false) { } async function getInfo(cwd = "./", imagesOnly = false) { - await getDockerCompose(); - const ps = await exec(`${dockerComposeCmd} ps`, { cwd }); - const images = await exec(`${dockerComposeCmd} images`, { cwd }); + const cmd = await getDockerCompose(); + const ps = await exec(`${cmd} ps`, { cwd }); + const images = await exec(`${cmd} images`, { cwd }); if (ps && images && ps.stdout && images.stdout) { return imagesOnly ? images.stdout : `${ps.stdout}\n${images.stdout}`; diff --git a/src/init/init.js b/src/init/init.js index ddf5b205..280feb2e 100644 --- a/src/init/init.js +++ b/src/init/init.js @@ -1,7 +1,6 @@ import fs from "fs"; import path from "path"; -import { exec } from "promisify-child-process"; -import { print } from "../utils/utils.js"; +import { print, exec } from "../utils/utils.js"; import { copyFolderRecursiveSync, deleteWithPrompt, diff --git a/src/test/test.js b/src/test/test.js index f537c115..42221edf 100644 --- a/src/test/test.js +++ b/src/test/test.js @@ -1,4 +1,4 @@ -import { exec } from "promisify-child-process"; +import { spawn } from "child_process"; import { print } from "../utils/utils.js"; async function run() { @@ -10,11 +10,9 @@ async function run() { async function test() { print("===== Starting Tests ====="); - const child = exec("npm test"); + const proc = spawn("npm", ["test"], { stdio: "inherit" }); - child.stdout.on("data", (out) => process.stdout.write(out)); - child.stderr.on("data", (err) => process.stderr.write(err)); - await child; + await new Promise((resolve) => proc.on("close", resolve)); } export default { run }; diff --git a/src/utils/utils.js b/src/utils/utils.js index 611d4d61..a3d2563f 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -1,4 +1,8 @@ import { readFile } from "fs/promises"; +import { promisify } from "util"; +import { exec as execCb } from "child_process"; + +export const exec = promisify(execCb); const config = JSON.parse( await readFile(new URL("../config/config.json", import.meta.url)), diff --git a/tests/cli.test.mjs b/tests/cli.test.mjs index ab141f4b..88a889a6 100644 --- a/tests/cli.test.mjs +++ b/tests/cli.test.mjs @@ -11,24 +11,21 @@ describe("command line usage", () => { afterAll(() => cleanLocal()); it("help", async () => { - const res = await exec("aeproject help", { cwd }); - assert.equal(res.code, 0); + await exec("aeproject help", { cwd }); }); it("version", async () => { const res = await exec("aeproject --version", { cwd }); - assert.equal(res.code, 0); assert.include(res.stdout, version); }); for (const folder of [null, "testprojectfolder"]) { // eslint-disable-next-line vitest/valid-title it(folder ? `init ${folder}` : "init", async () => { - const res = await exec( + await exec( folder ? `aeproject init ${folder}` : "aeproject init", { cwd }, ); - assert.equal(res.code, 0); // link to use local aeproject utils await linkLocalLib(folder); @@ -48,8 +45,7 @@ describe("command line usage", () => { } it("env", async () => { - const res = await exec("aeproject env", { cwd }); - assert.equal(res.code, 0); + await exec("aeproject env", { cwd }); assert.isTrue(await isEnvRunning(cwd)); // don't run for all gh-action matrix tests @@ -75,14 +71,11 @@ describe("command line usage", () => { it("env --info", async () => { const res = await exec("aeproject env --info", { cwd }); - assert.equal(res.code, 0); - print(res.stdout); }); it("test", async () => { const res = await exec("aeproject test", { cwd }); - assert.equal(res.code, 0); assert.equal(res.stderr, ""); assert.include(res.stdout, "2 passing"); }); @@ -94,7 +87,6 @@ describe("command line usage", () => { // link to use local aeproject utils await linkLocalLib(); - assert.equal(res.code, 0); assert.equal(res.stderr, ""); assert.include( res.stdout, @@ -115,21 +107,18 @@ describe("command line usage", () => { ); const resEnv = await exec("aeproject env", { cwd }); - assert.equal(resEnv.code, 0); assert.isTrue(await isEnvRunning(cwd)); assert.include(resEnv.stdout, "aeternity/aeternity latest"); assert.include(resEnv.stdout, "aeternity/aesophia_http latest"); const resTest = await exec("aeproject test", { cwd }); - assert.equal(resTest.code, 0); assert.include(resTest.stdout, "2 passing"); } else console.log("skipping next test for auxiliary test run"); }); it("env --stop", async () => { - const res = await exec("aeproject env --stop", { cwd }); - assert.equal(res.code, 0); + await exec("aeproject env --stop", { cwd }); assert.isFalse(await isEnvRunning(cwd)); }); }); diff --git a/tests/util.mjs b/tests/util.mjs index a2778d33..7844439d 100644 --- a/tests/util.mjs +++ b/tests/util.mjs @@ -1,7 +1,6 @@ import path from "path"; - -import { exec as execP } from "promisify-child-process"; import fs from "fs"; +import { exec as execP } from "../src/utils/utils"; export const cwd = path.join(process.cwd(), ".testdir");