Skip to content

Commit

Permalink
refactor: promisify instead promisify-child-process
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Dec 20, 2024
1 parent 9a66b74 commit 56e2024
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 50 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
43 changes: 18 additions & 25 deletions src/env/env.js
Original file line number Diff line number Diff line change
@@ -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! =====");
Expand Down Expand Up @@ -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! =====");
}
Expand All @@ -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! =====");
}
Expand All @@ -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);
Expand All @@ -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}`;
Expand Down
3 changes: 1 addition & 2 deletions src/init/init.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
8 changes: 3 additions & 5 deletions src/test/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exec } from "promisify-child-process";
import { spawn } from "child_process";
import { print } from "../utils/utils.js";

async function run() {
Expand All @@ -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 };
4 changes: 4 additions & 0 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
@@ -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)),
Expand Down
19 changes: 4 additions & 15 deletions tests/cli.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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");
});
Expand All @@ -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,
Expand All @@ -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));
});
});
3 changes: 1 addition & 2 deletions tests/util.mjs
Original file line number Diff line number Diff line change
@@ -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");

Expand Down

0 comments on commit 56e2024

Please sign in to comment.